I have 3 objects:
1.
class Product
{
protected $id;
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.
class ProductLabel
{
protected $product;
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.
class Label {
protected $id;
protected $title;
public function setId($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
}
And I'm trying to moisten the product with shortcuts:
$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();
But I have no changes to the database. I get only 4 SELECT queries from the product_label table. Where is my mistake? Can composite keys be used this way?
source
share