Symfony2, Doctrine & YML: Category/Subcategory listing

Hello all,

I’m working on a WordPress like system (blog) and it all seems to go well until I needed to do a ordering process on my categories database. I’m using Symfony 2.3 with Doctrine and am using YML as configuration. To be clear; I don’t want to use DQL.

My understanding is I need to have some sort of relationship (many-to-one, one-to-many) declared in my YML config file of the Entity.

My entity and table were created using the php app/console doctrine functions. The idea is as following:

Table

id    parent    name
1     0         home
2     0         work
3     2         office
4     1         music
5     2         projects

And this would be the desired output:

home
    music
work
    office
    projects

And of course, there are many more (sub-)categories.

So I want to have a relationship within the same table where the categories are first ordered by category/subcategory and then by the id.

I’ve read about the relationships but they are always about two tables. One table seems to be a problem for many and also for me. So my question is; how do I do this?

Just for full understanding:

My entity file without the standard setters/getters

namespace BlogBundle\Entity;
class Blog
{
    protected $id;
    protected $parent;
    protected $name;
}

My YAML file for now

BlogBundle\Entity\Blog:
type: entity
table: blog
/* Here something should be about the relationship if I'm correct*/
repositoryClass: BlogBundle\Entity\BlogRepository /* In this repository all my database non-DQL "queries" should go if I'm correct about the recommended use */
id:
    id:
        type: integer
        strategy: auto
fields:
    parent:
        type: integer
    name:
        type: string(255)

I’m fairly new to Doctrine and although I’m very keen to learn it I can’t seem to figure this one out. I’ve read all the documentation, but even then I can’t seem to find an answer. Any help would be amazing! Thank you so much in advance!

As it turns out, the Doctrine documentation covers exactly this scenario (a hierarchy of categories).

I cannot believe I missed that one! Thank you so much!

Now I have indeed declared (without error this time) the relationship, but… How do i retrieve the data? A fetchAll doesnt work, or is that a fully wrong approach?

@Jeff_Mott Thanks again!

So now I’ve been working on the YML file and here is the database create code, yml file and the Entity.

The table create code

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parentId` int(11) DEFAULT NULL,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_3AF3466810EE4CEE` (`parentId`),
  CONSTRAINT `FK_3AF3466810EE4CEE` FOREIGN KEY (`parentId`) REFERENCES `categories` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `categories` (`id`, `parentId`, `name`) VALUES
(1, NULL, 'test'),
(2, 3, 'test'),
(3, 1, 'test');

YML config

FormBundle\Entity\Category:
    type: entity
    oneToMany:
        children:
            fetch: "EAGER"
            targetEntity: FormBundle\Entity\Category
            mappedBy: parent
            cascade: ["all"]
    manyToOne:
        parent:
            targetEntity: FormBundle\Entity\Category
            inversedBy: children
            joinColumn:
                name: parentId
                referencedColumnName: id
    table: categories
    repositoryClass: FormBundle\Entity\CategoryRepository
    id:
        id:
            column: id
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: '100'
    lifecycleCallbacks: {  }

The Entity file

<?php

namespace FormBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Category
 */
class Category
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var category
     */
    private $parent;


    /**
     * @var arrayCollection
     */
    private $children;

    /**
     * @var string
     */
    private $name;

    public function __construct()
    {
        $this->children = new ArrayCllection();
    }

    public function getChildren() {
        return $this->children;
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set parentId
     *
     * @param integer $parentId
     * @return Category
     */
    public function setParent(Category $parent)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parentId
     *
     * @return integer
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

}

But now the question is; How do I retrieve the data on the order I want? The order is the main categories with beneath each of those that main’s subcategories.

Please help me out here. I’m able to retrieve the rows with a simple findAll, but that doesn’t order my elements. I also tried to place the order option in the YML, but it didn’t do anything. What am I missing here?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.