php - Doctrine Entity Relationship -
am having bit of challenge creating entity relationship between product category , associated color(s) following entities (i omitted getters , setters though):
#product /** * product * * @orm\table(name="product") * @orm\entity(repositoryclass="appbundle\repository\productrepository") */ class product { /** * @var int * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string * * @orm\column(name="name", type="string", length=255) */ private $name; /** * @var float * * @orm\column(name="price", type="float") */ private $price; /** * @var int * * @orm\column(name="category", type="integer") * * many products have 1 category * * @orm\manytoone(targetentity="category", inversedby="products") * @orm\joincolumn(name="category_id", referencedcolumnname="id", nullable=false) */ protected $category; /** * @var int * * many products have 1 color * * @orm\manytoone(targetentity="color", inversedby="products") * @orm\joincolumn(name="color_id", referencedcolumnname="id") * * @orm\column(name="color", type="integer") */ private $color; } #category /** * category * * @orm\table(name="category") * @orm\entity(repositoryclass="appbundle\repository\categoryrepository") */ class category { /** * @var int * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string * * @orm\column(name="name", type="string", length=170, unique=true) */ private $name; /** * @var string * * @orm\column(name="desc", type="string", length=170, nullable=true) */ private $description; /** * 1 category has many products assigned * * @orm\onetomany(targetentity="product", mappedby="category", cascade={"persist"}) */ private $products; /** * class constructor * * @param none * @return void **/ public function __construct() { $this->products = new arraycollection(); } } #color class color{ /** * @var int * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string * * @orm\column(name="color", type="string", length=191, unique=true) */ private $color; /** * @var string * * @orm\column(name="code", type="string", length=191, unique=true) */ private $hexcode; /** * 1 color has many products assigned * * @orm\onetomany(targetentity="product", mappedby="color", cascade={"persist"}) */ private $products; /** * class constructor * * @param none * @return void **/ public function _construct(){ $this->products = new arraycollection(); } }
when run:
* php bin/console doctrine:schema:validate:
i error messages:
* association appbundle\entity\category#products refers owning side field appbundle\entity\product#category not defined association, field. * association appbundle\entity\category#products refers owning side field appbundle\entity\product#category not exist.
and:
* association appbundle\entity\color#products refers owning side field appbundle\entity\product#color not defined association, field. * association appbundle\entity\color#products refers owning side field appbundle\entity\product#color not exist.
i noticed whenever comment out lines:
** @orm\column(name="category", type="integer") ** @orm\column(name="color", type="integer")
the above errors vanish new message , error saying:
** [mapping] ok - mapping files correct. ** [database] fail - database schema not in sync current mapping file.
what doing wrong, new doctrine concept, , have followed documentations. appreciated...
for doctrine orm relations not integers entity objects.
remove @orm\column
annotations fields relations (in each entity).
then update database schema in development environment with:
php bin/console doctrine:schema:update --force
then generate doctrine migration file execute on production server
php bin/console doctrine:migrations:generate
update generetad migration file needs
and execute him way
php bin/console doctrine:migrations:execute timestampofthemigratefile
wiki
Comments
Post a Comment