我有两个实体:
KTH:
namespace Nauka\OneBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Nauka\OneBundle\Entity\Kth
*
* @ORM\Table(name="kth")
* @ORM\Entity
*/
class Kth
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $imie
*
* @ORM\Column(name="imie", type="string", length=30)
*/
private $imie;
/**
* @var string $nazwisko
*
* @ORM\Column(name="nazwisko", type="string", length=30)
*/
private $nazwisko;
/**
*
* @var integer $adres
* @ORM\OneToMany(targetEntity="Adres", mappedBy="kth")
*/
private $adres;
public function __construct() {
$this->adres = new ArrayCollection();
}
public function __toString() {
return $this->nazwisko;
}
======= SET & GET functions =======
ADRES 命名空间Nauka \ OneBundle \ Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Nauka\OneBundle\Entity\Adres
*
* @ORM\Table()
* @ORM\Entity
*/
class Adres
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $ulica
*
* @ORM\Column(name="ulica", type="string", length=30)
*/
private $ulica;
/**
* @var string $dom
*
* @ORM\Column(name="dom", type="string", length=5)
*/
private $dom;
/**
*
* @var type
* @ORM\ManyToOne(targetEntity="Kth", inversedBy="adres");
*/
private $kth;
public function __toString() {
return $this->ulica;
}
=======SET & GET functions ===========
Kth表格 - KthType()
class KthType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imie')
->add('nazwisko')
->add('adres','collection',array(
'type' => new AdresType(),
'allow_add' => true,
'allow_delete' => true
))
;
}
Adresy表单 - AdresType()
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ulica')
->add('dom');
}
控制器中的动作:
public function testAction(Request $request)
{
$adres = new Adres();
$kth = new Kth();
$kth->getAdres()->add($adres);
$formularz = $this->createForm(new KthType(), $kth);
if($request->isMethod('POST'))
{
$formularz->bindRequest($request);
$query = $this->getDoctrine()->getEntityManager();
$query->persist($formularz);
$query->flush();
return new Response('Done');
}
return $this->render("NaukaOneBundle:Main:widok1.html.twig",array(
'formularz' => $formularz->createView()
));
}
当我尝试使用Kth形式向数据库添加内容时 - symfony给我:
The class 'Symfony\Component\Form\Form' was not found in the chain configured namespaces Nauka\OneBundle\Entity
500 Internal Server Error - MappingException
那么有人能告诉我我做错了吗?
答案 0 :(得分:0)
在您的情况Form
中,您无法保留$formularz
个实例。
您打算做什么,实际上是持久保存由$kth
变量引用的实体。
将控制器操作中的if
块替换为:
if ($request->isMethod('POST') && $formularz->bindRequest($request)->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($kth);
$em->flush();
return new Response('Done');
}
答案 1 :(得分:0)
我认为您必须持有Form的数据,而不是Form ..
if($request->isMethod('POST'))
{
$formularz->bindRequest($request);
$query = $this->getDoctrine()->getEntityManager();
$query->persist($formularz**->getData()**);
$query->flush();
return new Response('Done');
}