Doctrine orm 条令2多对多跟随者关系不起作用
我在这里遵循了这个示例并创建了实体Doctrine orm 条令2多对多跟随者关系不起作用,doctrine-orm,zend-framework2,entity,relationship,Doctrine Orm,Zend Framework2,Entity,Relationship,我在这里遵循了这个示例并创建了实体 <?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 { /** * @
<?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;
}
}
通过使用下面的代码,我没有得到我应该得到的任何结果
$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。默认情况下,所有关联都是惰性的,这意味着当您第一次访问它时,它会被填充。PersistentCollection实际上是一个迭代器,
var\u dump
不会触发迭代,这就是为什么将\u initialized
属性设置为false
,并且\u元素的计数为0
您可以使用getArrayCopy
,也可以简单地遍历集合
var_dump($followers->getArrayCopy());
或:
执行第一项操作会导致:致命错误:调用C:\webserver\apache\htdocs\中未定义的方法Doctrine\ORM\PersistentCollection::getArrayCopy(),第二项操作几乎不返回任何内容,即使数据库中存在关系:/。你应该在$followers
上指定的注释在$following
字段上,反之亦然。实际上是我在其他地方输入的错别字造成的,呃!无论如何,谢谢你的帮助,现在我知道默认情况下关联是懒惰的,这我不知道:)如果你能解释是什么导致了这个问题,以及你是如何解决它的,这将对其他人有所帮助。
var_dump($followers->getArrayCopy());
foreach ($followers as $follower) {
var_dump($follower);
}