symfony/学说:关于双向多对多关系。我能';无法获取相关对象
我有一个多对多关系和一个动作:symfony/学说:关于双向多对多关系。我能';无法获取相关对象,symfony,doctrine,doctrine-orm,Symfony,Doctrine,Doctrine Orm,我有一个多对多关系和一个动作: <?php namespace Acme\HelloBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * @ORM\Entity * @ORM\Table(name="article") */ class Article { /** * @ORM\Id * @ORM\Column
<?php
namespace Acme\HelloBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="article")
*/
class Article
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="articles")
*/
protected $users;
public function __construct()
{
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add users
*
* @param Acme\HelloBundle\Entity\User $users
*/
public function addUser(\Acme\HelloBundle\Entity\User $users)
{
//$users->addArticle($this); // synchronously updating inverse side
$this->users[] = $users;
}
/**
* Get users
*
* @return Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}
<?php
namespace Acme\HelloBundle\Entity;
// use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\ManyToMany(targetEntity="Article", mappedBy="users")
*/
protected $articles;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
public function __construct()
{
$this->articles = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add articles
*
* @param Acme\HelloBundle\Entity\article $articles
*/
public function addArticle(\Acme\HelloBundle\Entity\article $articles)
{
$this->articles[] = $articles;
}
/**
* Get articles
*
* @return Doctrine\Common\Collections\Collection
*/
public function getArticles()
{
return $this->articles;
}
}
/////////////////////////////////
public function indexAction($name)
{
$article = $this->getDoctrine()->getRepository('AcmeHelloBundle:Article')->findOneBy(array());
$users = $article->getUsers();
var_dump($users);
}
我希望一个用户对象作为输出。您将返回一个用户集合,因为它是一个ToMany关联 只需在集合上迭代:
<?php
// ...
$users = $article->getUsers();
foreach($users as $u){
var_dump($u);
}
您还可以使用以下方法从对象获取值数组:
$users = $article->getUsers()->getValues();
$users = $article->getUsers()->getValues();