質問

Noob here. I would say I can see the peak of the curve but I still have a ways to go.

I have an entity (tblxchangecategories) referencing a categories table:

enter image description here

This is related to an entity (tblxchangecategory_hier) referencing a category hierarchy table:

enter image description here

The relationship is self-referencing.

There are 6 major categories that are the parents and the remaining categories (36) are all children.

This is how I've defined their relationship in the entity file (tblxchangecategories):

/**
     * @OneToMany(targetEntity="TblXchangecategories", mappedBy="children")
     **/
    private $parent;

    /**
     * @ManyToOne(targetEntity"TblXchangecategories", inversedBy="parent")
     * @JoinTable(name="tbl_xchangecategory_hier",
     *      joinColumns={@joinColumn(name="hier_parent", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="hier_child", referencedColumnName="id")}
     */
    private $children;

And in the other entitiy file (tblxchangecategory_hier):

/**
     * @var \WorkoutExchange\WXCoreBundle\Entity\TblXchangecategories
     *
     * @ORM\OneToMany(targetEntity="WorkoutExchange\WXCoreBundle\Entity\TblXchangecategories")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="hier_parent", referencedColumnName="id")
     * })
     */
    private $hierParent;

    /**
     * @var \WorkoutExchange\WXCoreBundle\Entity\TblXchangecategories
     *
     * @ORM\ManyToOne(targetEntity="WorkoutExchange\WXCoreBundle\Entity\TblXchangecategories")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="hier_child", referencedColumnName="id")
     * })
     */
    private $hierChild;

Using the following code I'm able to retrieve all of the categories in a flattened array (no relationships):

$categories = $em->getRepository('WorkoutExchangeWXCoreBundle:TblXchangecategories')->findAll();

I can set up the view, so that's no problem, it's getting the data out of the database that's causing me troubles.

I've seen references to the ->getChildren() function in several of my searches but I don't have this function in either of my entity definition files.

What I want is to get all of the entities populated so that when I display them I can use the parents as headers and the children under the parents something like this:

enter image description here

Thanks for any help.

役に立ちましたか?

解決

your relations are wrong, try this instead:

/**
 * @var TblXchangecategories
 *
 * @ORM\ManyToOne(targetEntity="TblXchangecategories")
 * @ORM\JoinColumn(name="hier_parent", referencedColumnName="id")
 * 
 */
private $hierParent;

/**
 * @var TblXchangecategories
 *
 * @ORM\OneToMany(targetEntity="TblXchangecategories", mappedBy="hierParent")
 */
private $hierChildren;

他のヒント

I think if it's posible. You could use existing tree behaviors, they have a lot of useful methods for manage your hierachy. DoctrineExtensions or DoctrineBehaviors from knplabs.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top