Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向双向DoctrineCollection添加实体_Php_Entity Framework_Symfony_Doctrine Orm_Doctrine - Fatal编程技术网

Php 向双向DoctrineCollection添加实体

Php 向双向DoctrineCollection添加实体,php,entity-framework,symfony,doctrine-orm,doctrine,Php,Entity Framework,Symfony,Doctrine Orm,Doctrine,我有两个Doctrine2实体: class Product { /** * @ORM\OneToMany(targetEntity="ProductVendor", mappedBy="product") */ protected $productVendors; //.... } class ProductVendor { /** * @ORM\ManyToOne(targetEntity="Product", inversedBy="produ

我有两个Doctrine2实体:

class Product {
   /**
   * @ORM\OneToMany(targetEntity="ProductVendor", mappedBy="product")
   */
   protected $productVendors;

   //....
}

class ProductVendor {
   /**
   * @ORM\ManyToOne(targetEntity="Product", inversedBy="productVendors")
   */
   protected $product;

   //....
}
这些实体中的每一个都有标准的getter和setter,为了简洁起见,我省略了它们。我正在向产品供应商添加一个新产品,如下所示:

$myProductVendor->setProduct($myProduct);

然后,当我为$myProduct遍历ProductVendors时,新的ProductVendor不在那里。它只包含该产品的现有产品供应商。我的理解是,原则关系将负责向关系双方添加相关实体。我遗漏了什么?

因为它是双向的,所以您需要更新双方的关联

e、 g


要获得更好的性能,请在级联持久化字段中添加以下内容:

public function addProductVendor(\Project\YourBundle\Entity\ProductVendor $productVendor)
{
    $productVendor->setProduct($this);
    $this->productVendors[] = $productVendor;

    return $this;
}
public function addProductVendor(\Project\YourBundle\Entity\ProductVendor $productVendor)
{
    $productVendor->setProduct($this);
    $this->productVendors[] = $productVendor;

    return $this;
}