Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay Hierarchical Data From Table In Twig Template
    primarykey
    data
    text
    <p>I am working with Symfony2 and Twig: I have a table that is set up to handle hierarchical data with Doctrine. It is a list of categories with parent->children->sub_children->sub_sub_children etc... Many many variations.</p> <p>I am able to display them on the page like so, but the issue is there is no "indentation" to show which element belongs to what parent or child or child child etc etc. </p> <pre><code> &lt;table class="table" id="tableList"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Category Name&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; {% for productCategory in productCategories %} &lt;tr&gt; &lt;td&gt;{{ productCategory.name }}&lt;/td&gt; &lt;/tr&gt; {% endfor %} &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>I'd like the list to look something like this: </p> <pre><code> Parent Child Child Parent Child Parent Child Child Child </code></pre> <p>Instead of like this: </p> <pre><code> Parent Child Child Child Parent Child Parent etc. etc. </code></pre> <p>Original Entity: </p> <pre><code> &lt;?php namespace WIC\ProductCategoryBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Gedmo\Mapping\Annotation as Gedmo; use WIC\CommonBundle\DoctrineExtensions\Mapping\Annotation as Common; use Symfony\Component\Validator\Constraints as Assert; /** * ProductCategory * * @ORM\Table() * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository") * @Gedmo\Tree(type="nested") * @Common\Loggable(logEntryClass="WIC\ProductCategoryBundle\Entity\ProductCategoryLog") * @Common\Accountable * @Gedmo\SoftDeleteable(fieldName="deletedAt") */ class ProductCategory { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * @ORM\Column(name="name", type="string", length=255, nullable=false) * @Common\Versioned * @Assert\NotBlank(message="Location Name Cannot Be Left Blank.") */ protected $name; /** * @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="ProductCategory", inversedBy="children") * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="SET NULL") */ protected $parent; /** * @ORM\OneToMany(targetEntity="ProductCategory", mappedBy="parent") * @ORM\OrderBy({"lft" = "ASC"}) */ protected $children; /** * @ORM\ManyToMany(targetEntity="WIC\ProductBundle\Entity\Product", mappedBy="productCategories", cascade={"persist"}) */ protected $products; /** * @ORM\ManyToOne(targetEntity="WIC\UserBundle\Entity\User") * @ORM\JoinColumn(name="created_by", referencedColumnName="id") * @Common\Blameable(on="create") */ private $createdBy; /** * @ORM\ManyToOne(targetEntity="WIC\UserBundle\Entity\User") * @ORM\JoinColumn(name="updated_by", referencedColumnName="id") * @Common\Blameable(on="update") */ private $updatedBy; /** * @ORM\ManyToOne(targetEntity="WIC\AccountBundle\Entity\Account", inversedBy="productCategorys", cascade={"remove","persist"}) * @ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false, onDelete="CASCADE") * @Common\Versioned * @Common\Blameable(on="create") */ protected $account; /** * @var datetime $created * * @Common\Timestampable(on="create") * @ORM\Column(type="datetime") */ private $created; /** * @var datetime $updated * * @Common\Timestampable(on="update") * @ORM\Column(type="datetime", nullable=true) */ protected $updated; /** * @ORM\Column(name="deletedAt", type="datetime", nullable=true) */ private $deletedAt; public function __construct() { $this-&gt;products = new ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this-&gt;id; } /** * Set name * * @param string $name * @return ProductCategory */ public function setName($name) { $this-&gt;name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this-&gt;name; } public function setLvl(ProductCategory $lvl = null) { $this-&gt;lvl = $lvl; } public function getLvl() { return $this-&gt;lvl; } public function setParent(ProductCategory $parent = null) { $this-&gt;parent = $parent; } public function getParent() { return $this-&gt;parent; } /** * Set account * * @param \WIC\AccountBundle\Entity\Account $account * @return User */ public function setAccount(\WIC\AccountBundle\Entity\Account $account = null) { $this-&gt;account = $account; return $this; } /** * Get account * * @return \WIC\AccountBundle\Entity\Account */ public function getAccount() { return $this-&gt;account; } /** * Add product * * @param \WIC\ProductBundle\Entity\Product $product * @return Product */ public function addProduct(\WIC\ProductBundle\Entity\Product $product) { $this-&gt;products[] = $product; return $this; } /** * Remove product * * @param \WIC\ProductBundle\Entity\Product $product */ public function removeProduct(\WIC\ProductBundle\Entity\Product $product) { $this-&gt;products-&gt;removeElement($product); } /** * Set products * * @return Product */ public function setProducts($products) { $this-&gt;products = $products; return $this; } /** * Get products * * @return \Doctrine\Common\Collections\Collection */ public function getProducts() { return $this-&gt;products; } /** * Set updated * * @param \DateTime $updated * @return User */ public function setUpdated($updated) { $this-&gt;updated = $updated; return $this; } /** * Get updated * * @return \DateTime */ public function getUpdated() { return $this-&gt;updated; } /** * Set deletedAt * * @param \DateTime $deletedAt * @return User */ public function setDeletedAt($deletedAt) { $this-&gt;deletedAt = $deletedAt; return $this; } /** * Get deletedAt * * @return \DateTime */ public function getDeletedAt() { return $this-&gt;deletedAt; } public function getOptionLabel() { return str_repeat( html_entity_decode('&amp;nbsp;', ENT_QUOTES, 'UTF-8'), ($this-&gt;getLvl() + 0 ) * 3 ) . $this-&gt;getName(); } } </code></pre> <p>Anyone know how to accomplish this? </p> <p>Thanks so much!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload