使用复合键进行学说2水合作用

时间:2014-04-01 20:05:21

标签: php doctrine-orm zend-framework2

我有3个实体:

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;
    }



}

我正试图用标签保湿产品:

    $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();

但我没有DB的变化。我只从product_label表中获得4个SELECT查询。 我的错误在哪里?是否可以这种方式使用复合键?

1 个答案:

答案 0 :(得分:1)

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

这不应该在数组中。它应该是标签类的实例 为label类创建一个实例,并将值设置为该实体而不是Array

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