symfony / doctrine:关于双向多对多关系。我无法得到相关的对象

时间:2011-12-26 20:46:38

标签: 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(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);

    }

然后我装了一些灯具。最后我调用了动作,我得到了这个输出:

object(Doctrine\ORM\PersistentCollection)#109 (9) {
  ["snapshot":"Doctrine\ORM\PersistentCollection":private]=>
  array(0) {
  }
  ["owner":"Doctrine\ORM\PersistentCollection":private]=>
  object(Acme\HelloBundle\Entity\Article)#103 (3) {
    ["id":protected]=>
    int(1)
    ["name":protected]=>
    string(19) "Creditos para todos"
    ["users":protected]=>
    *RECURSION*
  }
  ["association":"Doctrine\ORM\PersistentCollection":private]=>
  array(15) {
    ["fieldName"]=>
    string(5) "users"
    ["joinTable"]=>
    array(0) {
    }
    ["targetEntity"]=>
    string(28) "Acme\HelloBundle\Entity\User"
    ["mappedBy"]=>
    string(8) "articles"
    ["inversedBy"]=>
    NULL
    ["cascade"]=>
    array(0) {
    }
    ["fetch"]=>
    int(2)
    ["type"]=>
    int(8)
    ["isOwningSide"]=>
    bool(false)
    ["sourceEntity"]=>
    string(31) "Acme\HelloBundle\Entity\Article"
    ["isCascadeRemove"]=>
    bool(false)
    ["isCascadePersist"]=>
    bool(false)
    ["isCascadeRefresh"]=>
    bool(false)
    ["isCascadeMerge"]=>
    bool(false)
    ["isCascadeDetach"]=>
    bool(false)
  }
  ["em":"Doctrine\ORM\PersistentCollection":private]=>
  object(Doctrine\ORM\EntityManager)#77 (10) {

我希望将User对象作为输出。

2 个答案:

答案 0 :(得分:2)

您正在收回用户集,因为它是ToMany协会。

简单地遍历集合:

<?php
// ...
$users = $article->getUsers();
foreach($users as $u){
    var_dump($u);
}

答案 1 :(得分:0)

您还可以使用以下方法从对象获取值数组:

$users = $article->getUsers()->getValues();