php – Docrtine 2水合复合键

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Docrtine 2水合复合键脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有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],'label' => 3],]
    ];
    $entity = $hydrator->hydrate($data,$entity);
    $this->getEntityManager()->;merge($entity);
    $this->getEntityManager()->flush();

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

解决方法

‘labels’=> [
            [‘product’=> 1,’label’=> 1],
            [‘product’=> 1,’label’=> 2],’label’=> 3],
]

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

应该是类的实例(即)应该传递标签实例

脚本宝典总结

以上是脚本宝典为你收集整理的php – Docrtine 2水合复合键全部内容,希望文章能够帮你解决php – Docrtine 2水合复合键所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。