Docrtine 2 Composite Key Hydration

I have 3 objects:

1.

/**
 * @ORM\Entity
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="uid")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="ProductLabel", mappedBy="product")
     */
    protected $labels;

    public function __construct() {
        $this->labels = new ArrayCollection();
    }

    public function addLabels(Collection $labels) {
        foreach ($labels as $label) {
            $label->setProduct($this);
            $this->labels->add($label);
        }
    }

    public function removeLabels(Collection $labels) {
        foreach ($labels as $label) {
            $label->setProduct(null);
            $this->labels->removeElement($label);
        }
    }

    public function getLabels() {
        return $this->labels;
    }

}

2.

/**
 * @ORM\Entity
 * @ORM\Table(name="product_label")
 */
class ProductLabel
{

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Product")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="uid")
     */
    protected $product;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Label")
     * @ORM\JoinColumn(name="label_id", referencedColumnName="uid")
     */
    protected $label;

    public function setProduct($product)
    {
        $this->product = $product;
    }

    public function getProduct()
    {
        return $this->product;
    }

    public function setLabel($label)
    {
        $this->label = $label;
    }

    public function getLabel()
    {
        return $this->label;
    }

}

3.

/**
 * @ORM\Entity
 * @ORM\Table(name="label")
 */

class Label {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="uid")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $title;

    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }



}

And I'm trying to moisten the product with shortcuts:

    $hydrator = new DoctrineObject($this->getEntityManager());
    $entity = new \Application\Entity\Product();
    $data = [
        'id' => 1,
        'title' => 'asdasd',
        'labels' => [
            [ 'product' => 1, 'label' => 1],
            [ 'product' => 1, 'label' => 2],
            [ 'product' => 1, 'label' => 3],
        ]
    ];
    $entity = $hydrator->hydrate($data, $entity);
    $this->getEntityManager()->merge($entity);
    $this->getEntityManager()->flush();

But I have no changes to the database. I get only 4 SELECT queries from the product_label table. Where is my mistake? Can composite keys be used this way?

+4
source share
1 answer

'labels' => [ [ 'product' => 1, 'label' => 1], [ 'product' => 1, 'label' => 2], [ 'product' => 1, 'label' => 3], ]

It should not be in an array. it should be an example of a label class create an instance of the label class and set the values ​​for this object instead of Array

should be instance of the class (ie) label instance should be passed

+1
source

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


All Articles