[英]Docrtine 2 Hydration with Composite Keys
我有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查詢。 我的錯誤在哪里? 是否可以這種方式使用復合鍵?
'labels' => [ [ 'product' => 1, 'label' => 1], [ 'product' => 1, 'label' => 2], [ 'product' => 1, 'label' => 3], ]
這不應該在數組中。 它應該是label類的實例為label類創建一個實例,並將值設置為該實體而不是Array
should be instance of the class (ie) label instance should be passed
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.