Symfony2 + OneToMany + 必须保存数据



我有两个实体:

KTH :

namespace NaukaOneBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* NaukaOneBundleEntityKth
*
* @ORMTable(name="kth")
* @ORMEntity
*/
class Kth
{
/**
 * @var integer $id
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var string $imie
 *
 * @ORMColumn(name="imie", type="string", length=30)
 */
private $imie;
/**
 * @var string $nazwisko
 *
 * @ORMColumn(name="nazwisko", type="string", length=30)
 */
private $nazwisko;
/**
 *
 * @var integer $adres
 * @ORMOneToMany(targetEntity="Adres", mappedBy="kth")
 */
private $adres;

public function __construct() {
    $this->adres = new ArrayCollection();
}
public function __toString() {
    return $this->nazwisko;
}
======= SET & GET functions =======

阿德雷斯 命名空间 Nauka\OneBundle\Entity;

use DoctrineORMMapping as ORM;
/**
 * NaukaOneBundleEntityAdres
 *
 * @ORMTable()
 * @ORMEntity
 */
class Adres
{
/**
 * @var integer $id
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var string $ulica
 *
 * @ORMColumn(name="ulica", type="string", length=30)
 */
private $ulica;
/**
 * @var string $dom
 *
 * @ORMColumn(name="dom", type="string", length=5)
 */
private $dom;
/**
 *
 * @var type 
 * @ORMManyToOne(targetEntity="Kth", inversedBy="adres");
 */
private $kth;

public function __toString() {
    return $this->ulica;
}
=======SET & GET functions ===========

第 K 个形式 - 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 form - 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 'SymfonyComponentFormForm' was not found in the chain configured namespaces NaukaOneBundleEntity
500 Internal Server Error - MappingException

那么有人可以告诉我我做错了什么吗?

您不能保留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');
}

我认为你必须坚持表单的数据,而不是表单的数据。

if($request->isMethod('POST'))
{
  $formularz->bindRequest($request);
  $query = $this->getDoctrine()->getEntityManager();
  $query->persist($formularz**->getData()**);
  $query->flush();
  return new Response('Done');
} 

相关内容

  • 没有找到相关文章