0

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

abhinand
  • 554
  • 5
  • 10
Webghost
  • 933
  • 2
  • 7
  • 18

3 Answers3

1

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;
}
qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • Qoop, thanks for getting back. But it's not null because of the way data is added. Plus, I tested by adding data to database manually, it is still not working. – Webghost Jan 05 '14 at 00:39
  • My problem was very similar: You only could access one side of the relation (item => category), but category => items was empty. The explicit usage of "setCategory" solved it for me, because it "tells" the other side, that there is one to take care of. – k00ni Jan 15 '20 at 09:02
1

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.

0

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

Ayaddi
  • 1
  • 3