I got back to doctrine after a while. I have some problem with doctrine and symfony which I can't figure out at all, why it could be. A help would be nice.
I've got two entities: 1. Category:
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Items", mappedBy="category")
*/
private $items;
public function __construct()
{
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
*
* @param Items $items
* @return Category
*/
public function addItem(Items $item)
{
$this->items[] = $item;
return $this;
}
/**
*
* @param Items $item
*/
public function removeItem(Items $item)
{
$this->items->removeElement($item);
}
/**
* @return Items
*/
public function getItems()
{
return $this->items;
}
And Second is: Items
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
*
* @ORM\ManyToOne(targetEntity="Category", inversedBy="items")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set category
*
* @param Category $category
* @return Items
*/
public function setCategory(Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return Category
*/
public function getCategory()
{
return $this->category;
}
Now in Controller, I'm simply trying to retrieve items via categories:
$categories = $em->getRepository('Category')->findAll();
//just example
foreach ($categories as $category) {
print_r($category->getItems());
}
Should $category->getItems() return the Items of that category? Or I'm in illusion that it should work? Please suggest.
It returns null at the moment but not errors although there is data in database.
The table structure is:
CREATE TABLE `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
And
CREATE TABLE `items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_category` (`category_id`),
CONSTRAINT `fk_category` FOREIGN KEY (`category_id`) REFERENCES `Category` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
Thanks