This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

symfony2 book, Doctrine: category_id field is not written

This is for discussion, mainly on 1.x but there's some 2.x topics here too.

symfony2 book, Doctrine: category_id field is not written

by GeorgeDorn » Wed Jun 22, 2011 5:53 pm

Hello symfony!

First of all: Thanks for the great book, which I am working through. At the moment I am stuck at the "Relationships/Associations"-Chapter. I've been trying to get the console to create the category_id field in the product-table over and over again.

The way i tried it was:


1.) Get a fresh symfony2 beta5 edition with vendors

2.) add Acme-bundle: php app/console init:bundle "Acme\StoreBundle" src/

3.) enable bundle in the kernel

4.) add namespace to autoloader

5.) configure/create the database

6.) create product entity php app/console doctrine:generate:entity AcmeStoreBundle:Product "name:string(255) price:float description:text" which only worked after i had first done php app/console doctrine:generate:entities Acme

7.) added mapping info
Code: Select all
<?php

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length="100")
     */
    protected $name;

    /**
     * @ORM\Column(type="decimal", scale="2")
     */
    protected $price;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set price
     *
     * @param float $price
     */
    public function setPrice($price)
    {
        $this->price = $price;
    }

    /**
     * Get price
     *
     * @return float $price
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set description
     *
     * @param text $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * Get description
     *
     * @return text $description
     */
    public function getDescription()
    {
        return $this->description;
    }
}


8.) generated getters and setters php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product

9.) wrote database first time php app/console doctrine:schema:update --force

10.) added category entity php app/console doctrine:generate:entity AcmeStoreBundle:Category "name:string(255)" --mapping-type=yml

11.) added mapping info
Code: Select all
<?php

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Acme\StoreBundle\Entity\Category
 */
class Category
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var string $name
     */
    private $name;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }
}


12.) changed product entity according to the book
Code: Select all
<?php

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length="100")
     */
    protected $name;

    /**
     * @ORM\Column(type="decimal", scale="2")
     */
    protected $price;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set price
     *
     * @param float $price
     */
    public function setPrice($price)
    {
        $this->price = $price;
    }

    /**
     * Get price
     *
     * @return float $price
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set description
     *
     * @param text $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * Get description
     *
     * @return text $description
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;
}


13.) updated entities php app/console doctrine:generate:entities Acme

14.) and updated database php app/console doctrine:schema:update --force

but got no category_id field in the Product-Table. How can that be?

Thanks for any help,

GeorgeDorn
GeorgeDorn
Junior Member
 
Posts: 1
Joined: Wed Jun 22, 2011 5:34 pm

Re: symfony2 book, Doctrine: category_id field is not writte

by nemo » Mon Sep 12, 2011 11:00 am

i ran into the same issue. did you find a solution. i tried several hours and i didnt find any solution. Thanks a lot.
nemo
Junior Member
 
Posts: 1
Joined: Mon Sep 12, 2011 10:56 am

Re: symfony2 book, Doctrine: category_id field is not writte

by jeffsacco » Thu Mar 27, 2014 8:32 pm

I have run into the same issue. I followed the tutorial and for whatever reason, cannot generate that category_id in the products table.

Anyone having the same issue or found a way to resolve it?
jeffsacco
Junior Member
 
Posts: 1
Joined: Thu Mar 27, 2014 8:30 pm

Re: symfony2 book, Doctrine: category_id field is not writte

by mickburkesnr » Wed Oct 22, 2014 4:44 pm

I think you'll find that the "category_id" field is actually "id".
mickburkesnr
Member
 
Posts: 78
Joined: Mon Aug 22, 2011 12:33 pm
Location: Birmingham, UK