开发者

Is there a way of using reference column name in doctrine 2?

开发者 https://www.devze.com 2023-02-21 06:49 出处:网络
I need to assing a value (a previously created entites Id)to reference column in a doctrine 2 model, For example

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

/**
 * @Entity @Table(name="products")
 */
class product {

    /**
    * 
    * @ManyToOne(targetEntity="category")
    */
    protected $category;

    public function assignCategoryId($id) {
        $this->category_i开发者_Python百科d=$id;
    }


}

I assume that category_id is created by doctrine 2 as referance column name, Don't ask why I want to assign the id not the object itself, because it has to be this way. Is there a way to do this ? Any idea ?


While @Orbling's answer is correct you don't actually have to load the entity from the database. Instead you can use a reference:

// 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));

You can find the documentation here.


In order to do that, you would create a category entity and assign it to the relationship property.

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

$product = new product();
$product->category = $category;

That would create the relationship I believe.

0

精彩评论

暂无评论...
验证码 换一张
取 消