0
votes

How to insert value product_import?

Entity Features

/**
 * Features
 *
 * @ORM\Table(name="features")
 * @ORM\Entity
 */
class Features
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=45, nullable=false)
     */
    private $name;


}

Entity Product

/**
 * Product
 *
 * @ORM\Table(name="product", indexes={@ORM\Index(name="fk_product_features1_idx", columns={"features_id"})})
 * @ORM\Entity
 */
class Product
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $id;

    /**
     * @var \Features
     *
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     * @ORM\OneToOne(targetEntity="Features")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(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;
    }
}

Entity Product Import

/**
 * ProductImport
 *
 * @ORM\Table(name="product_import", indexes={@ORM\Index(name="fk_product_import_product1_idx", columns={"product_id", "product_features_id"})})
 * @ORM\Entity
 */
class ProductImport
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \Product
     *
     * @ORM\ManyToOne(targetEntity="Product")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="product_id", referencedColumnName="id"),
     *   @ORM\JoinColumn(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;
    }
}

INSERT

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

Message Error

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_features_id' cannot be null

1
Give the full code of your entities with class name please - Mcsky
It´s done. Do you need more codes? - B. A.
What is the ProdutosImport entity, what do you do in the constructor with this data array .. - Mcsky
You should use something like $productImport = new ProdutosImport(); $productImport->setProduct($product) You should only work with objects with doctrine - Mcsky

1 Answers

0
votes

Mcsky, the entitys has hydrator, this information didn't fit up and I thought it was not important for error.

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