我正在与2个相关实体一起运行Symfony 3.4-具有OneToMany关系的Entry和Track。我已经通过this arcticle-我能够将ArrayCollection添加到Entry表,但是没有添加到Track表。
为了方便阅读,我试图排除尽可能多的无关代码。
条目
<?php
namespace Blogger\BlogBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="`workshop_entry`")
*/
class Entry
{
private $id;
/**
* @ORM\OneToMany(targetEntity="Blogger\BlogBundle\Entity\Track", cascade={"persist"})
*/
private $tracklist;
public function __construct()
{
$this->tracklist = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getTracklist()
{
return $this->tracklist;
}
public function addTrack(Track $track) {
$this->tracklist->add($track);
}
public function removeTrack(Track $track) {
$this->tracklist->removeElement($track);
}
}
Track
<?php
namespace Blogger\BlogBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Track
*/
class Track
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string")
*/
protected $name;
EntryType
class EntryType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('tracklist', CollectionType::class, [
'entry_type' => TrackType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'mapped' => false,
])
->add('submit', SubmitType::class);
EntryController
public function createAction(Request $request)
{
$reviewEntry = new Entry();
$form = $this->createForm(EntryType::class, $reviewEntry, ['action' => $request->getUri()]);
$form->handleRequest($request);
//Validates the form
if($form->isValid())
//
$em->persist($reviewEntry);
$em->flush();
}
return $this->render('BloggerBlogBundle:Blog:create.html.twig', ['form'=>$form->createView()]);
}
尝试将@ORM\Entity
添加到您的Track
实体并运行迁移