0

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

我得到以下结果:

 $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}}

 Key "parent" for array with keys "id, name, code, lft, lvl, rgt, root, __children" does not exist in NRtworksChartOfAccountsBundle:ChartOfAccounts:COA2.html.twig at line 10 

我找不到我做错了什么。

帮助 ?

4

1 回答 1

0

Twig 中的语句应如下所示:

{{ account.parent.id }}

在此处查看 Twig 文档,了解 Twig 如何处理变量。尤其是“实现”部分很有趣,因为它准确地描述了 Twig 在幕后所做的事情。

更新

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

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

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

于 2014-02-05T12:44:40.657 回答