gpt4 book ai didi

php - 使用复合键的 Doctrine 2 Hydration

转载 作者:可可西里 更新时间:2023-10-31 23:40:41 24 4
gpt4 key购买 nike

我有 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();

但是我在数据库中没有任何变化。我只从 product_label 表中得到 4 个 SELECT 查询。我的错误在哪里?可以这样使用复合键吗?

最佳答案

'标签'=> [
[ '产品' => 1, '标签' => 1],
[ '产品' => 1, '标签' => 2],
[ '产品' => 1, '标签' => 3],
]

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

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

关于php - 使用复合键的 Doctrine 2 Hydration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com