0

I have followed the example here doctrine 2 documentation and made the entity

    <?php
namespace Account\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Zend\Filter\Null;

/**
 * @ORM\Entity
 * @ORM\Table(name="accounts")
 */
class Account
{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @ORM\Column(length=11)
     */
    private $id;

    // ...... 

    /**
     * @ORM\ManyToMany(targetEntity="Account\Entity\Account", mappedBy="following")
     */
    private $followers;

    /**
     * @ORM\ManyToMany(targetEntity="Account\Entity\Account", inversedBy="followers")
     * @ORM\JoinTable(name="followers",
     *      joinColumns={@ORM\JoinColumn(name="account_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="follower_id", referencedColumnName="id")}
     *      )
     */
    private $following;

    public function __construct(){
        $this->followers = new ArrayCollection();
        $this->following = new ArrayCollection();
    }


    /**
     * @param mixed $followers
     */
    public function setFollowers($followers)
    {
        $this->followers[] = $followers;
    }

    /**
     * @return mixed
     */
    public function getFollowers()
    {
        return $this->followers;
    }

    public function addFollowers($followers){
        foreach($followers as $follower)
            $this->followers->add($follower);
    }

    public function removeFollowers($followers){
        $this->followers->removeElement($followers);
    }

    /**
     * @param mixed $following
     */
    public function setFollowing($following)
    {
        $this->following[] = $following;
    }

    /**
     * @return mixed
     */
    public function getFollowing()
    {
        return $this->following;
    }

    public function addFollowing($followers){
        foreach($followers as $follower)
            $this->following->add($follower);
    }

    public function removeFollowing($followers){
        $this->following->removeElement($followers);
    }


    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

}

So I have 2 accounts (ids 1 and 2) and made it so that 1 follows (is friend to) 2. The column is something like

user_id follower_id
   2         1

By using the following code, I'm not getting any results as I should

$user = $this->entityManager()->getRepository('Account/Entity/Account')->find(1);
$followers = $user->getFollowers();
var_dump($followers);

It returns something like:

object(Doctrine\ORM\PersistentCollection)#357 (9) { ["snapshot":"Doctrine\ORM\PersistentCollection":private]=> array(0) { } ["owner":"Doctrine\ORM\PersistentCollection":private]=> NULL ["association":"Doctrine\ORM\PersistentCollection":private]=> NULL ["em":"Doctrine\ORM\PersistentCollection":private]=> NULL ["backRefFieldName":"Doctrine\ORM\PersistentCollection":private]=> NULL ["typeClass":"Doctrine\ORM\PersistentCollection":private]=> NULL ["isDirty":"Doctrine\ORM\PersistentCollection":private]=> bool(false) ["initialized":"Doctrine\ORM\PersistentCollection":private]=> bool(false) ["coll":"Doctrine\ORM\PersistentCollection":private]=> object(Doctrine\Common\Collections\ArrayCollection)#358 (1) { ["_elements":"Doctrine\Common\Collections\ArrayCollection":private]=> array(0) { } } }

The same happens if I use getFollowing and all the combinations I've tried. Am I missing something? I mean it's pretty much like the documentation code, please help me out!

I'm using Zend Framework 2, if that's of any help.

4

1 回答 1

1

默认情况下,所有关联都是 LAZY 的,这意味着当您第一次访问它时会填充它。PersistentCollection 实际上是一个迭代器并且 avar_dump不会触发迭代,这就是为什么您看到_intialized属性设置为false并且计数为_elementsis的原因0

您可以使用getArrayCopy或简单地遍历集合。

var_dump($followers->getArrayCopy());

或者:

foreach ($followers as $follower) {
    var_dump($follower);
}
于 2013-09-01T07:05:09.387 回答