Я работаю с symfony 3.4.6 и fosrestbundle. У меня есть три связанных сущности:
Embarque class
class Embarque{
//...
/**
* @var EmbarqueContenedor[]|ArrayCollection
*
* @Serializer\SerializedName("contenedores")
* @ORM\OneToMany(targetEntity = "AppBundle\Entity\EmbarqueContenedor",mappedBy = "embarque",cascade = {"persist"})
*/
private $contenedores;
public function addEmbarqueContenedor($embarqueContenedor)
{
if (!$this->contenedores->contains($embarqueContenedor)) {
$this->contenedores->add($embarqueContenedor);
//$embarqueContenedor->setEmbarque($this);
}
}
public function removeEmbarqueContenedor($embarqueContenedor)
{
if ($this->contenedores->contains($embarqueContenedor)) {
$this->contenedores->removeElement($embarqueContenedor);
}
}
}
EmbarqueContenedor class
class EmbarqueContenedor{
/**
* @var Embarque
*
* @ORM\ManyToOne(targetEntity = "AppBundle\Entity\Embarque",inversedBy = "contenedores",)
*/
private $embarque;
/**
* @var Contenedor
*
* @ORM\ManyToOne(targetEntity = "AppBundle\Entity\Contenedor",inversedBy = "embarques")
*/
private $contenedor;
}
Contenedor class
class Contenedor{
/**
* @var EmbarqueContenedor[]|ArrayCollection
*
* @Serializer\SerializedName("contenedorEmbarques")
* @ORM\OneToMany(targetEntity = "AppBundle\Entity\EmbarqueContenedor",mappedBy = "contenedor")
*/
private $embarques;
public function addEmbarqueContenedor($embarqueContenedor)
{
if (!$this->embarques->contains($embarqueContenedor)) {
$this->embarques->add($embarqueContenedor);
$embarqueContenedor->setContenedor($this);
}
}
public function removeEmbarqueContenedor($embarqueContenedor)
{
if ($this->embarques->contains($embarqueContenedor)) {
$this->embarques->removeElement($embarqueContenedor);
}
}
}
в формах выглядит следующим образом
class EmbarqueType{
$builder->add('contenedores', CollectionType::class, [
'entry_type' => EmbarqueContenedorType::class,
'allow_add' => true,
]);
}
class EmbarqueContenedorType{
$builder->add('contenedor', EntityType::class, [
'class' => Contenedor::class,
])
}
Contenedor сущности создается отдельно и выбирается в EmbarqueContenedorType при добавлении или редактировании, EmbarqueContenedorEntity создается из EmbarqueType.
Проблема в том, что записи сохраняются в базе данных, но без какой-либо ссылки. Таблица EmbarqueContenedor не имеет ссылки на таблицы Embarque или Contenedor.
Ошибки нет, потому что данные сохраняются, но на них нет ссылок. Как это могло произойти??
Заранее спасибо!
Edit
Я заметил, что я не сериализовал свойство Id объекта Contenedor Entity, поэтому невозможно сделать ссылку, теперь она исправлена, но объект Embarque все еще не упоминается.




Я думаю, проблема в построении отношений таблиц. Таблица / объект EmbarqueContenedor не требуется. Если у вас есть отношение многие-ко-многим, просто скажите, что Doctrine позаботится обо всем остальном (Doctrine создаст все необходимые таблицы).
Итак, решение должно заключаться в том, чтобы определить ваши отношения с аннотацией МногиеToMany.
Я не могу этого сделать, потому что моя бизнес-логика заставляет меня добавлять некоторые настраиваемые поля в отношения многие ко многим, поэтому сущность EmbarqueContenedor необходима. Спасибо, в любом случае