php - Symfony 2 many to one -
i learning symfony 2. in documentation saw example many 1 relations. tried in code. have 2 entities: products , categories.
/** * @orm\manytoone(targetentity="category",inversedby="products") * @orm\joincolumn(name="category_id", referencedcolumnname="id") */ private $category;
in entity product have such code. executed app\console doctrine:generate:entities appbundle
, app\console doctrine:schema:update --force
. table category has appeard, in table products don't have field category_id. cleared cache doesn't work. wrong?
product
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use appbundle\entity\category; /** * product * @orm\entity * @orm\table(name="product") */ class product { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\column(type="string",length=100) */ private $name; /** * @orm\column(type="decimal", scale=2) */ private $price; /** * @orm\column(type="text") */ private $description; /** * @orm\manytoone(targetentity="category", inversedby="products") * @orm\joincolumn(name="category_id", referencedcolumnname="id") */ private $category; /** * id * * @return integer */ public function getid() { return $this->id; } /** * set name * * @param string $name * @return product */ public function setname($name) { $this->name = $name; return $this; } /** * name * * @return string */ public function getname() { return $this->name; } /** * set price * * @param integer $price * @return product */ public function setprice($price) { $this->price = $price; return $this; } /** * price * * @return integer */ public function getprice() { return $this->price; } /** * set description * * @param string $description * @return product */ public function setdescription($description) { $this->description = $description; return $this; } /** * description * * @return string */ public function getdescription() { return $this->description; } }
category
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use doctrine\common\collections\arraycollection; /** * category * @orm\entity * @orm\table(name="category") */ class category { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\column(type="string", length=64) */ private $name; /** * @orm\onetomany(targetentity="product", mappedby="category") */ private $products; public function __construct() { $this->products = new arraycollection(); } /** * id * * @return integer */ public function getid() { return $this->id; } /** * set name * * @param string $name * @return category */ public function setname($name) { $this->name = $name; return $this; } /** * name * * @return string */ public function getname() { return $this->name; } }
upd2 after php app\console doctrine:schema:update --dump-sql
c:\xampp1\htdocs\first_project # php app\console doctrine:schema:update --dump-sql create table category (id int auto_increment not null, name varchar(255) not nul l, primary key(id)) default character set utf8 collate utf8_unicode_ci engine = innodb; create table product (id int auto_increment not null, name varchar(128) not null , price int not null, description longtext not null, primary key(id)) default ch aracter set utf8 collate utf8_unicode_ci engine = innodb; comp323 c:\xampp1\htdocs\first_project #
please try in annotation complete namespace reference category entity.
/** *@orm\manytoone(targetentity="appbundle\entity\category",inversedby="products") *@orm\joincolumn(name="category_id", referencedcolumnname="id") */
Comments
Post a Comment