4

I have 3 entities:

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 hydrate Product with Labels:

    $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 in DB. I only get 4 SELECT queries from product_label table. Where is my mistake? Is it possible to use composite keys in this way?

2
  • What is the 2nd Entity used for? You only need to define the Product and Label entity. M:N is handled by doctrine2 internally, check the ManyToMany association
    – Sam
    Apr 1, 2014 at 20:56
  • I have to use 1:N + 1:M instead of M:N because of client-side (ExtJS). The second reason - there are some cases with additional data that I need to store in mediator table.
    – 4orever
    Apr 1, 2014 at 20:58

1 Answer 1

1

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

This should not be in array. it should be instance of the label class create a instance for the label class and set the values to that entity instead of Array

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.