[英]Symfony Form with Doctrine Entity - Relation not loading for editing
我對某些我認為可能很基本的東西有疑問,但原因正在逃避。
我有兩個實體,一個組織和一個訂閱。 一個組織與一個訂閱鏈接。
我希望能夠通過表單進行編輯(通過表單創建已經可以)。 當我執行以下操作時:
$organisation = $this->getDoctrine()->getRepository('AppBundle\Entity\Organisation')
->findOneBy(array(
'id' => $id
));
$form = $this->createForm(new OrganisationType(), $organisation, array(
'method' => 'POST'
));
$form->submit($paramFetcher->all());
我收到一個異常,因為未設置組織實體的預訂屬性(盡管將具有預訂的屬性傳遞給默認表單)。 異常如下:
Expected argument of type "AppBundle\Entity\Subscription", "NULL" given
很明顯,這是由Organization實體上的setSubscription方法拋出的,但是我不確定為什么該表單應該將轉換為Int值的int值自動轉換為Subscription實體。
我在這里做蠢事嗎? 我已在下面附上了相關代碼。 謝謝!
復制此問題的最簡單的控制器操作
public function testAction(Request $request)
{
$organisation = $this->getDoctrine()->getRepository('AppBundle\Entity\Organisation')
->findOneBy(array('id' => 7));
$form = $this->createForm(new OrganisationType(), $organisation);
$form->submit($request->request->all());
return $this->render('test.html.twig', array(
'testform' => $form->createView()
));
}
Organisation.php
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Organisation
*
* @ORM\Table(name="organisation")
* @ORM\Entity(repositoryClass="AppBundle\Repository\OrganisationRepository")
*/
class Organisation
{
const ENABLED = 1;
const DISABLED = 2;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @JMS\Groups({"default", "list-organisation"})
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @JMS\Groups({"default", "list-organisation"})
*/
private $name;
/**
* @var Subscription|null The subscription this organisation is currently linked to
* @ORM\ManyToOne(targetEntity="Subscription", inversedBy="organisations")
* @ORM\JoinColumn(name="subscription_id", referencedColumnName="id", onDelete="set null")
* @JMS\Groups({"default", "list-organisation"})
*/
private $subscription;
/**
* @var Collection
* @ORM\OneToMany(targetEntity="User", mappedBy="organisation")
* @JMS\Groups({})
*/
private $users;
/**
* @var Collection
* @ORM\OneToMany(targetEntity="Subgroup", mappedBy="organisation")
* @JMS\Groups({"default"})
*/
private $subgroups;
/**
* @var string $enabled
*
* @ORM\Column(type="string")
*/
private $enabled = self::ENABLED;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
* @JMS\Groups({"default", "list-organisation"})
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
* @JMS\Groups({"default", "list-organisation"})
*/
private $updated;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Organisation
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Sets subscription
*
* @param Subscription $subscription
* @return $this
*/
public function setSubscription(Subscription $subscription)
{
$this->subscription = $subscription;
return $this;
}
/**
* Gets subscription
*
* @return Subscription|null
*/
public function getSubscription()
{
return $this->subscription;
}
/**
* Sets enabled
*
* @param $enabled
*/
public function setEnabled($enabled)
{
if (!in_array($enabled, array(self::ENABLED, self::DISABLED))) {
throw new \InvalidArgumentException('Invalid enabled status');
}
$this->enabled = $enabled;
}
/**
* Gets enabled
*
* @return string
*/
public function getEnabled()
{
return $this->enabled;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Returns Users belonging to this organisation
*
* @return Collection
*/
public function getUsers()
{
return $this->users;
}
public function __toString()
{
return $this->getName();
}
}
Subscription.php
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Organisation
*
* @ORM\Table(name="subscription")
* @ORM\Entity(repositoryClass="AppBundle\Repository\SubscriptionRepository")
*/
class Subscription
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @JMS\Groups({"default", "list-organisation"})
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @JMS\Groups({"default", "list-organisation"})
*/
private $name;
/**
* @var Collection
* @ORM\OneToMany(targetEntity="Organisation", mappedBy="subscription")
* @JMS\Groups({"default"})
*/
private $organisations;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
* @JMS\Groups({"default"})
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
* @JMS\Groups({"default"})
*/
private $updated;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Subscription
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Returns Organisations with this subscription type
*
* @return Collection
*/
public function getOrganisations()
{
return $this->organisations;
}
/**
* @return string
*/
public function __toString()
{
return $this->getName();
}
}
OrganisationType.php
<?php
namespace AppBundle\Form;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class OrganisationType extends AbstractType
{
private $manager;
public function __construct(ObjectManager $objectManager)
{
$this->manager = $objectManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('subscription', EntityType::class, array(
'class' => 'AppBundle\Entity\Subscription'
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Organisation',
'csrf_protection' => false
));
}
}
在新的Symfony 3.1上,這就像一種魅力(如先前的評論所述,這是處理表單提交的推薦方法):
public function testAction(Request $request)
{
$organisation = $this->getDoctrine()->getRepository('AppBundle\Entity\Organisation')
->find(1);
$form = $this->createForm(OrganisationType::class, $organisation);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->persist($organisation);
$this->getDoctrine()->getManager()->flush();
die('saved into database.');
}
return $this->render('test.html.twig', array(
'testform' => $form->createView()
));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.