Search code examples
phpdoctrine-orm

Doctrine 2 category_id always NULL when set ManyToOne relationship


When I insert new entry if I set ManyToOne relationship "Category" i will not able to fill "categoryId" field why ?

This is the Entity with the relationship :

<?php

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Item
 *
 * @ORM\Table(name="item")
 * @ORM\Entity
 */
class Item extends Base
{
    /**
     * @ORM\ManyToOne(targetEntity="Category")
     */
    private $category;


    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    public $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=40, nullable=false)
     */
    public $name;

    /**
     * @var integer
     *
     * @ORM\Column(name="category_id", type="integer", nullable=true)
     */
    public $categoryId;

}

This is a Base class I made for generate getter and setter and allow $entry->name = 'yo' instead of $entry->setName('yo');

<?php

namespace Application\Entity;

class Base
{
    public function __call($method, $args) {
        if (preg_match('#^get#i', $method)) {
            $property = str_replace('get', '', $method);
            $property = strtolower($property);
            return $this->$property;
        }

        if (preg_match('#^set#i', $method)) {
            $property = str_replace('set', '', $method);
            $property = strtolower($property);
            $this->$property = $args[0];
        }
    }

    public function fromArray(array $array = array()) {
        foreach ($array as $key => $value) {
            $this->$key = $value; 
        }
    }
}

This is how i save new item :

$item = new \Application\Entity\Item();
$item->name = 'Computer';
$item->categoryId = '12';
$this->em->persist($item);
$this->em->flush();

What is wrong ?


Solution

  • You are doing it wrong! With Doctrine, you don't work with category_id column (and alike) but with relations. Doctrine will take care of columns.

    You will have to read the docs but correct way would be:

    $category = new Category() ;
    $category->setName("Food") ;
    
    $item = new Item() ;
    $item->setName("Pizza") ;
    $item->setCategory($category) ;
    
    $em->persist($item) ;
    $em->flush() ;
    

    This is 100% correct way of doing things, you don't even need to persist newly create Category (Doctrine will do that for you). But manually trying to set category_id column is completely wrong way of doing things.

    One more: Don't try to make ActiveRecord of Doctrine2. When I was switching from D1 to D2, I was thinking of doing the same thing but at the end, figured it was waste of time. It looks like you are trying to make your own framework; do NOT do that. Learn Symfony2 instead; it is not easy but it is worth the time.