我正在为我的实体帐户使用DoctrineExtensions树。
我的结果如下:
$repo = $em->getRepository('NRtworksChartOfAccountsBundle:Accounttree');
$arrayTree = $repo->childrenHierarchy();
实体Accounttree是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;
}
}
在我的树枝上
{{ 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
我找不到我做错的事。
帮助?
答案 0 :(得分:0)
Twig中的陈述应如下所示:
{{ account.parent.id }}
查看关于Twig如何处理变量的Twig文档here。特别是“实现”部分很有意思,因为它描述了Twig在幕后的确切内容。
<强>更新强>
使用childrenHierarchy()
获取树时,会得到一个表示树的数组。由于此数组表示形式,每个节点都没有对其父节点的引用。因此在这种情况下不能使用{{ account.parent.id }}
。
通常你不需要像这样的父母,因为你正在遍历树。因此,为了到达某个节点,您必须通过它的父节点。
唯一的问题是当前树的根节点。当此节点是绝对根时,它没有父节点(这里没有问题)。如果它不是绝对根,则将节点高1级传递给childrenHierarchy()
(传递最初传递的节点的父节点)。