学说插入两个连接实体



如何插入值product_import?

实体特征

/**
 * Features
 *
 * @ORMTable(name="features")
 * @ORMEntity
 */
class Features
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="name", type="string", length=45, nullable=false)
     */
    private $name;

}

实体产品

/**
 * Product
 *
 * @ORMTable(name="product", indexes={@ORMIndex(name="fk_product_features1_idx", columns={"features_id"})})
 * @ORMEntity
 */
class Product
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="NONE")
     */
    private $id;
    /**
     * @var Features
     *
     * @ORMId
     * @ORMGeneratedValue(strategy="NONE")
     * @ORMOneToOne(targetEntity="Features")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="features_id", referencedColumnName="id")
     * })
     */
    private $features;
    /**
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * @param integer $id
     *
     * @return self
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }
    /**
     * @return Features
     */
    public function getFeatures()
    {
        return $this->features;
    }
    /**
     * @param Features $features
     *
     * @return self
     */
    public function setFeatures(Features $features)
    {
        $this->features = $features;
        return $this;
    }
}

实体产品导入

/**
 * ProductImport
 *
 * @ORMTable(name="product_import", indexes={@ORMIndex(name="fk_product_import_product1_idx", columns={"product_id", "product_features_id"})})
 * @ORMEntity
 */
class ProductImport
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;
    /**
     * @var Product
     *
     * @ORMManyToOne(targetEntity="Product")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="product_id", referencedColumnName="id"),
     *   @ORMJoinColumn(name="product_features_id", referencedColumnName="features_id")
     * })
     */
    private $product;
    /**
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * @param integer $id
     *
     * @return self
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }
    /**
     * @return Product
     */
    public function getProduct()
    {
        return $this->product;
    }
    /**
     * @param Product $product
     *
     * @return self
     */
    public function setProduct(Product $product)
    {
        $this->product = $product;
        return $this;
    }
}

插入

$data['product'] = entityProduto;
$data['product_features'] = 1;
$entity = new ProdutosImport($data);
$em->persist($entity);
$em->flush();

消息错误

SQLSTATE[23000]:完整性约束冲突:1048 列 "product_features_id"不能为空

Mcsky,实体有水化器,这些信息不适合,我认为错误并不重要。

  public function __construct(array $data) 
   {
       $hydrator = new ClassMethods();
       $hydrator->hydrate($data, $this);
   }

相关内容

  • 没有找到相关文章

最新更新