StofDoctrineExtension Tree entity: get the id of the parent in twig

◇◆丶佛笑我妖孽 提交于 2019-12-12 01:04:27

问题


I'm using DoctrineExtensions Tree for my entity account.

I'm getting my results with the following:

 $repo = $em->getRepository('NRtworksChartOfAccountsBundle:Accounttree');
 $arrayTree = $repo->childrenHierarchy();

Entity Accounttree is the classic entity following the doc:

 /**
 * @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;
    }
}

in my twig

{{ account.getParent().getId() }}

gives me:

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

and {{Account.parent.id}}

gives

 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 

I can't find what I'm doing wrong.

help ?


回答1:


The statement in Twig should look like this:

{{ account.parent.id }}

Have a look at the Twig documentation here on how Twig treats variables. Especially the "Implementation" part is interesting, as it describes exactly what Twig does under the hood.

update

When fetching a tree with childrenHierarchy(), you get an array representing the tree. Because of this array representation, each node doesn't have a reference to it's parent. So {{ account.parent.id }} cannot be used in this case.

Usually you don't need the parent like this, because you are traversing the tree. So in order to reach a certain node you have to go through it's parent.

The only problem would be the root node of the current tree. When this node is an absolute root, it doesn't have a parent (so no problem here). If it isn't an absolute root, pass a node 1 level higher to childrenHierarchy() (pass the parent of the node you originally passed).



来源:https://stackoverflow.com/questions/21538095/stofdoctrineextension-tree-entity-get-the-id-of-the-parent-in-twig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!