Tree StofDoctrineExtension树实体:获取细枝中父级的id

Tree StofDoctrineExtension树实体:获取细枝中父级的id,tree,twig,stofdoctrineextensions,symfony-2.4,Tree,Twig,Stofdoctrineextensions,Symfony 2.4,我正在为我的实体帐户使用DoctrineExtensions树 我的结果如下: $repo = $em->getRepository('NRtworksChartOfAccountsBundle:Accounttree'); $arrayTree = $repo->childrenHierarchy(); 实体Accounttree是单据后面的经典实体: /** * @Gedmo\Tree(type="nested") * @ORM\Table(name="Account

我正在为我的实体帐户使用DoctrineExtensions树

我的结果如下:

 $repo = $em->getRepository('NRtworksChartOfAccountsBundle:Accounttree');
 $arrayTree = $repo->childrenHierarchy();
实体Accounttree是单据后面的经典实体:

 /**
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="Accounttree")
 * use repository for handy tree functions
 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
 */

class Accounttree
{
    /**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */

protected $id;

/**
 * @ORM\Column(type="string", length=100, unique = true)
 */

protected $name;  

 /**
 * @ORM\Column(type="string", length=50)
 */

protected $code;

/**
 * @Gedmo\TreeLeft
 * @ORM\Column(name="lft", type="integer")
 */
protected $lft;

/**
 * @Gedmo\TreeLevel
 * @ORM\Column(name="lvl", type="integer")
 */
protected $lvl;

/**
 * @Gedmo\TreeRight
 * @ORM\Column(name="rgt", type="integer")
 */
protected $rgt;

 /**
 * @Gedmo\TreeRoot
 * @ORM\Column(name="root", type="integer", nullable=true)
 */
protected $root;

/**
 * @Gedmo\TreeParent
 * @ORM\ManyToOne(targetEntity="Accounttree", inversedBy="children")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
 */
protected $parent;

/**
 * @ORM\OneToMany(targetEntity="Accounttree", mappedBy="parent")
 * @ORM\OrderBy({"lft" = "ASC"})
 */
protected $children;


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

    /**
     * Set name
     *
     * @param string $name
     * @return Account
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set code
     *
     * @param string $code
     * @return Account
     */
    public function setCode($code)
    {
        $this->code = $code;

        return $this;
    }

    /**
     * Get code
     *
     * @return string 
     */
    public function getCode()
    {
        return $this->code;
    }

    public function setParent(Accounttree $parent=null)
    {
        $this->parent = $parent;
    }

    public function getParent()
    {
        return $this->parent;
    }
}
在我的小树枝上

{{ account.getParent().getId() }}
给我:

ContextErrorException: Notice: Array to string conversion in /home/eagle1/www/Symfony24/app/cache/dev/classes.php line 4604


    in /home/eagle1/www/Symfony24/app/cache/dev/classes.php line 4604
   at ErrorHandler->handle('8', 'Array to string conversion', '/home/eagle1/www/Symfony24/app/cache/dev/classes.php', '4604', array('object' => array('id' => '2', 'name' => 'Revenue', 'code' => '700000', 'lft' => '2', 'lvl' => '1', 'rgt' => '11', 'root' => '1', '__children' => array(array('id' => '4', 'name' => 'Sales', 'code' => '701000', 'lft' => '3', 'lvl' => '2', 'rgt' => '6', 'root' => '1', '__children' => array(array('id' => '7', 'name' => 'Products', 'code' => '701100', 'lft' => '4', 'lvl' => '3', 'rgt' => '5', 'root' => '1', '__children' => array()))), array('id' => '5', 'name' => 'Discount', 'code' => '702000', 'lft' => '7', 'lvl' => '2', 'rgt' => '8', 'root' => '1', '__children' => array()), array('id' => '6', 'name' => 'Subsidies', 'code' => '703000', 'lft' => '9', 'lvl' => '2', 'rgt' => '10', 'root' => '1', '__children' => array()))), 'item' => 'getParent', 'arguments' => array(), 'type' => 'method', 'isDefinedTest' => false, 'ignoreStrictCheck' => false))
   at sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', 'getParent', 'array', array('id' => '2', 'name' => 'Revenue', 'code' => '700000', 'lft' => '2', 'lvl' => '1', 'rgt' => '11', 'root' => '1', '__children' => array(array('id' => '4', 'name' => 'Sales', 'code' => '701000', 'lft' => '3', 'lvl' => '2', 'rgt' => '6', 'root' => '1', '__children' => array(array('id' => '7', 'name' => 'Products', 'code' => '701100', 'lft' => '4', 'lvl' => '3', 'rgt' => '5', 'root' => '1', '__children' => array()))), array('id' => '5', 'name' => 'Discount', 'code' => '702000', 'lft' => '7', 'lvl' => '2', 'rgt' => '8', 'root' => '1', '__children' => array()), array('id' => '6', 'name' => 'Subsidies', 'code' => '703000', 'lft' => '9', 'lvl' => '2', 'rgt' => '10', 'root' => '1', '__children' => array())))) in /home/eagle1/www/Symfony24/app/cache/dev/classes.php line 4604
及 {{Account.parent.id}

给予

我找不到我做错了什么


帮助?

Twig中的语句应如下所示:

{{ account.parent.id }}
看一看关于Twig如何处理变量的Twig文档。特别是“实现”部分很有趣,因为它准确地描述了细枝在引擎盖下的工作

更新

使用
childrenHierarchy()
获取树时,会得到一个表示该树的数组。由于这种数组表示,每个节点都没有对其父节点的引用。因此在这种情况下不能使用
{{account.parent.id}

通常您不需要这样的父对象,因为您正在遍历树。所以为了到达某个节点,你必须通过它的父节点


唯一的问题是当前树的根节点。当这个节点是绝对根节点时,它没有父节点(所以这里没有问题)。如果不是绝对根,请将节点1级别的更高级别传递给
childrenHierarchy()
(传递最初传递的节点的父节点)。

我尝试过了。问题是在doctrineextension中,我获得了如下树:$repo=$em->getRepository('nTworksChartoFaccountsBundle:Accounttree')$arrayTree=$repo->childrenHierarchy();这样,父元素就不在数组中了:具有键“id、name、code、lft、lvl、rgt、root、_子元素”的数组的键“parent”不存在
{{ account.parent.id }}