0

我有两个实体:

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 ===========

第 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 形式 - 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

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

4

2 回答 2

0

我认为您必须保留表单的数据,而不是表单..

if($request->isMethod('POST'))
{
  $formularz->bindRequest($request);
  $query = $this->getDoctrine()->getEntityManager();
  $query->persist($formularz**->getData()**);
  $query->flush();
  return new Response('Done');
} 
于 2013-06-07T21:22:55.323 回答
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');
}
于 2012-11-15T09:32:19.857 回答