$entity = $this->getDoctrine() ->getRepository('AcmeMainBundle:Comment') $a_comment = $entity->getUsers()->getName();
Jeśli wiecie jaki jest problem prosiłbym o wskazówkę dla korekty lub o nowe rozwiązanie.
$entity = $this->getDoctrine() ->getRepository('AcmeMainBundle:Comment') $a_comment = $entity->getUsers()->getName();
class Comment { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string", length=255) */ protected $user_id; /** * @ORM\Column(type="string") */ protected $content; /** * @ORM\Column(type="string", length=255) */ protected $date; /** * @ORM\Column(type="string", length=255) * */ protected $post_id; /** * @ORM\ManyToOne(targetEntity="Users", inversedBy="comment") * @ORM\JoinColumn(name="user_id", referencedColumnName="id") */ protected $users; //.... /** * Set users * * @param \Acme\MainBundle\Entity\Users $users * @return Comment */ public function setUsers(\Acme\MainBundle\Entity\Users $users = null) { $this->users = $users; return $this; } /** * Get users * * @return \Acme\MainBundle\Entity\Users */ public function getUsers() { return $this->users; } }
class Users { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string", length=255) */ protected $email; /** * @ORM\Column(type="string", length=255) */ protected $username; /** * @ORM\Column(type="string", length=255) */ protected $location; /** * @ORM\Column(type="string", length=255) */ protected $name; /** * @ORM\Column(type="string", length=255) */ protected $password; /** * @ORM\Column(type="integer") */ protected $role = 1; /** * @ORM\OneToMany(targetEntity="Comment", mappedBy="users") */ protected $comment; // .... /** * Constructor */ public function __construct() { $this->comment = new ArrayCollection(); } /** * Add comment * * @param \Acme\MainBundle\Entity\Comment $comment * @return Users */ public function addComment(\Acme\MainBundle\Entity\Comment $comment) { $this->comment[] = $comment; return $this; } /** * Remove comment * * @param \Acme\MainBundle\Entity\Comment $comment */ public function removeComment(\Acme\MainBundle\Entity\Comment $comment) { $this->comment->removeElement($comment); } /** * Get comment * * @return \Doctrine\Common\Collections\Collection */ public function getComment() { return $this->comment; } }
// $entity to zwrócony z bazy obiekt Comment $author = $entity->getUser()->getName();
// to jest post $entity = $this->getDoctrine()->getRepository('AcmeMainBundle:Post)->find($postId); $comments = $post->getComments(); // kolekcja komentarzy foreach ($comments as $comment) { echo $comment->getUser()->getUsername() . PHP_EOL; }
$comments = $this->getDoctrine() ->getRepository('AcmeMainBundle:Comment') $ta = $comments->getUser(); foreach($ta as $to){ $tar = $to->getName(); }