加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Docrtine 2水合复合键

发布时间:2020-12-13 22:52:07 所属栏目:PHP教程 来源:网络整理
导读:我有3个实体: 1. /** * @ORMEntity */class Product{ /** * @ORMId * @ORMColumn(type="integer",name="uid") * @ORMGeneratedValue(strategy="AUTO") */ protected $id; /** * @ORMOneToMany(targetEntity="ProductLabel",mappedBy="product") */ pro
我有3个实体:

1.

/**
 * @ORMEntity
 */
class Product
{
    /**
     * @ORMId
     * @ORMColumn(type="integer",name="uid")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORMOneToMany(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.

/**
 * @ORMEntity
 * @ORMTable(name="product_label")
 */
class ProductLabel
{

    /**
     * @ORMId
     * @ORMManyToOne(targetEntity="Product")
     * @ORMJoinColumn(name="product_id",referencedColumnName="uid")
     */
    protected $product;

    /**
     * @ORMId
     * @ORMManyToOne(targetEntity="Label")
     * @ORMJoinColumn(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.

/**
 * @ORMEntity
 * @ORMTable(name="label")
 */

class Label {
    /**
     * @ORMId
     * @ORMColumn(type="integer",name="uid")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORMColumn(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 ApplicationEntityProduct();
    $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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读