主义2多对多的追随者关系不起作用

时间:2013-08-31 16:33:46

标签: doctrine-orm zend-framework2 entity relationship

我已按照此处的示例doctrine 2 documentation制作了实体

    <?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;
    }

}

所以我有2个帐户(ID 1和2)并且使得1跟随(是朋友)2。 该列类似于

user_id follower_id
   2         1

通过使用以下代码,我没有得到任何结果,因为我应该

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

它返回类似:

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) { } } }

如果我使用getFollowing和我尝试过的所有组合,也会发生同样的情况。我错过了什么吗?我的意思是它非常像文档代码,请帮帮我!

我正在使用Zend Framework 2,如果有任何帮助的话。

1 个答案:

答案 0 :(得分:1)

默认情况下,所有关联都是LAZY,这意味着首次访问它时会填充它。 PersistentCollection实际上是一个迭代器,而var_dump不会触发迭代,这就是为什么你看到_intialized属性设置为false_elements的计数是0

您可以使用getArrayCopy或只是遍历集合。

var_dump($followers->getArrayCopy());

或:

foreach ($followers as $follower) {
    var_dump($follower);
}