2015-11-05 73 views
2

亲子我有一个表“节点”,拥有ID,PARENT_ID等等...Symfony的原则 - 不工作

/** 
* Node 
* 
* @ORM\Table(name="nodes", indexes={@ORM\Index(name="parent", columns={"parent_id"}), @ORM\Index(name="path", columns={"path"})}) 
* @ORM\Entity 
*/ 
class Node 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="parent_id", type="integer", nullable=true) 
    */ 
    private $parentId; 

    ... 
} 

现在我想建立亲子relationsship(PARENT_ID是同一个表的id的外键)。基于对关系(http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations)symfony的食谱比如我试图创建它:

/** 
* @ORM\OneToMany(targetEntity="Node", mappedBy="parent") 
*/ 
protected $children; 


/** 
* @ORM\ManyToOne(targetEntity="Node", inversedBy="children") 
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id") 
*/ 
protected $parent; 


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

我的第一个问题是,该命令:

php app/console doctrine:generate:entities AppBundle 

并没有给父母的getter和setter和儿童(没有错误信息,只是正常的“生成...”消息)

所以我自己创造的getter和setter方法:

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

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

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

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

但这似乎不起作用。我不能获取例如节点的父或与孩子们:

$node->getParent(); 
$node->getChildren(); 

两个命令retun空,但数据是正确的。该代码甚至没有试图查询父母或孩子。

+0

你已经看过日志和分析器输出了吗? – chapay

+0

是的。这就是命令行输出和日志的输出:http://pastebin.com/mJU3Fp7h – bernhardh

+0

而剖析器只显示我1查询(查询获取$节点本身)和没有其他查询来获取$ node-> getParent ()或$ node-> getChildren()。 – bernhardh

回答

0

好吧,我发现为什么我有这个(愚蠢的)问题。由于我最初使用表的逆向工程生成了我的实体(http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html),所以我在我的配置文件(自动生成的地方)中使用了原则YML文件。看来,如果你有这样的文件,symfony/doctrine使用它们而不是注释。而已。我只需要删除它们。 facepalm

0

确保您的类的定义是这样的:

/** 
* @ORM\Entity() 
*/ 
class Node 
{ 
    public function __construct() 
    { 
     $this->children= new ArrayCollection(); 
    } 

    /** 
* Set parent 
* 
* @param \Bundle\Entity\Node $parent 
* @return Message 
*/ 
public function setParent(\Bundle\Entity\Node $parent = null) 
{ 
    $this->parent = $parent; 

    return $this; 
} 

/** 
* Get parent 
* 
* @return \Bundle\Entity\Node 
*/ 
public function getParent() 
{ 
    return $this->parent; 
} 

/** 
* Add children 
* 
* @param \Bundle\Entity\Node $children 
* @return Message 
*/ 
public function addReply(\Bundle\Entity\Node $children) 
{ 
    $this->children[] = $children; 

    return $this; 
} 

/** 
* Remove children 
* 
* @param \Bundle\Entity\Node $children 
*/ 
public function removeReply(\Bundle\Entity\Node $children) 
{ 
    $this->children->removeElement($children); 
} 

/** 
* Get children 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getChildren() 
{ 
    return $this->children; 
} 
} 

然后再次尝试实体产生的命令。并且不要忘记更新模式。

+0

我有这个注释已经在那里(更新我的问题,向你展示更多的代码)。该实体本身正在工作。我可以没有问题地获得其他属性(例如getName())。 – bernhardh

+0

在编辑中添加构造函数是否可以解决您的问题?将孩子设置为arraycollection。否则,我没有想法。 –

+0

Ehm。正如你在我的问题中所看到的,我已经在之前做过了(如食谱中所描述的)。 – bernhardh