简体   繁体   中英

Symfony2 + OneToMany + have to save data?

I have two entities :

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 namespace 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 form - 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');
}

Action in controller :

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()
   )); 
}

And when i try to add something to database using Kth form - symfony give me :

The class 'Symfony\Component\Form\Form' was not found in the chain configured namespaces Nauka\OneBundle\Entity
500 Internal Server Error - MappingException

So can someone tell me what I'm doing wrong ?

You can't persist the Form instance, in your case $formularz .

What you intend to do, is actually to persist the entity, referenced by the $kth variable.

Replace your if block inside your controller action with:

if ($request->isMethod('POST') && $formularz->bindRequest($request)->isValid()) {
    $em = $this->getDoctrine()->getEntityManager();
    $em->persist($kth);
    $em->flush();

    return new Response('Done');
}

I think what you must persist a data of Form, not the Form..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM