Webghost
Webghost

Reputation: 933

Symfony 2 Doctrine 2 one to many relationship not returning data

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

Upvotes: 0

Views: 1920

Answers (3)

Ayaddi
Ayaddi

Reputation: 1

try:

\Doctrine\Common\Util\Debug::dump($category->getItems()); 

instead of

print_r($category->getItems());

i face the same issue when i tried dump() function for some reason it doesn't dump results

Upvotes: 0

Alexis Moreno
Alexis Moreno

Reputation: 43

2 years later.. I'm also looking to make this work with the latest Symfony and Doctrine and by $category->getItems()

What I had to do is, in your case:

Controller:

$items = $em->getRepository('AppBundle:Items')->findByCategory($category);

For some reason ->getItems() is empty.

Upvotes: 1

qooplmao
qooplmao

Reputation: 17759

You need to set the category of the item when you add it otherwise it will be saved in the database as NULL rather than the category id.

/**
 *
 * @param Items $items
 * @return Category
 */
public function addItem(Items $item)
{
    $item->setCategory($this);
    $this->items[] = $item;

    return $this;
}

Upvotes: 1

Related Questions