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!
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?
<?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?