Is there a way to use the name of the reference column in doctrine 2?

I need to define a value (previously created entites Id) for a column reference in a doctrine 2 model, for example

/** * @Entity @Table(name="products") */ class product { /** * * @ManyToOne(targetEntity="category") */ protected $category; public function assignCategoryId($id) { $this->category_id=$id; } } 

I assume that category_id is created by Doctrine 2 as the name of the link column. Do not ask why I want to assign the id to the object itself, because it should be so. Is there any way to do this? Any idea?

+4
source share
2 answers

While @Orbling's answer is correct, you actually don't need to load the object from the database. You can use the link instead:

 // method on Product entity public function setCategory(Category $category) { $this->category = $category; } // then set the category on the product $product->setCategory($entityManager->getReference('category', $categoryId)); 

Here you can find.

+3
source

To do this, you must create a category object and assign it to the relationship property.

 $category = $entityManager->find('category', $categoryID); $product = new product(); $product->category = $category; 

This will create a relationship that I believe.

0
source

Source: https://habr.com/ru/post/1345860/


All Articles