Servus,
ich bin aktuell beim FormBuilder von Symfony2 und möchte für folgendes Beispiel ein Formular implementieren, welches die Daten in die DB schreibt. Allerdings kriege ich beim Versuch das Formular anzuzeigen schon einen Fehler (siehe unten):
Author <-> Book
Relation: 1 zu n
Hierbei soll ein Formular erzeugt werden, in welchem ich einen Autor anlegen und diesem direkt ein Buch zuweisen kann:
Author:
Firstname: _______
Lastname: _______
Book:
Title: ____
[save]
Woran ich hierbei hänge ist, die Relation 1 zu n in dem FormType darzustellen, sodass bei korrekter Eingabe der Felder automatisch die 1 zu n Beziehung (author_id in Book) in meiner mysql Tabelle abgebildet wird.
Die Tabellen stehen alle in der Datenbank korrekt (Entities sollten also korrekt sein), es geht mir um meinen AuthorType und BookType. Wie stelle ich dort die 1 zu n Relation richtig dar?
Hier ist mein Code:
Author-Entity:
Book-Entity:
AuthorType-Class:
BookType-Class:
Und schließlich meine Action im Controller:
Im Template render ich das ganze mit :
Als Fehler kriege ich:
"The form's view data is expected to be an instance of class Acme\UserBundle\Entity\Book, but is an instance of class Doctrine\Common\Collections\ArrayCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\Common\Collections\ArrayCollection to an instance of Acme\UserBundle\Entity\Book."
_________________________________
Ich hoffe jmd. kann mir hier weiter helfen.
Habe nach dem Fehler schon gesucht und etwas herumprobiert, es will aber nicht funktionieren.
Regards
ich bin aktuell beim FormBuilder von Symfony2 und möchte für folgendes Beispiel ein Formular implementieren, welches die Daten in die DB schreibt. Allerdings kriege ich beim Versuch das Formular anzuzeigen schon einen Fehler (siehe unten):
Author <-> Book
Relation: 1 zu n
Hierbei soll ein Formular erzeugt werden, in welchem ich einen Autor anlegen und diesem direkt ein Buch zuweisen kann:
Author:
Firstname: _______
Lastname: _______
Book:
Title: ____
[save]
Woran ich hierbei hänge ist, die Relation 1 zu n in dem FormType darzustellen, sodass bei korrekter Eingabe der Felder automatisch die 1 zu n Beziehung (author_id in Book) in meiner mysql Tabelle abgebildet wird.
Die Tabellen stehen alle in der Datenbank korrekt (Entities sollten also korrekt sein), es geht mir um meinen AuthorType und BookType. Wie stelle ich dort die 1 zu n Relation richtig dar?
Hier ist mein Code:
Author-Entity:
PHP-Code:
namespace Acme\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="author")
*/
class Author {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*
* @Assert\NotBlank()
* @Assert\Length(min = "2")
* @Assert\Type(type="string", message="The value {{ value }} is not a valid {{ type }}.")
*/
protected $firstname;
/**
* @ORM\Column(type="string", length=100)
*
* @Assert\NotBlank()
* @Assert\Length(min = "2")
* @Assert\Type(type="string", message="The value {{ value }} is not a valid {{ type }}.")
*/
protected $lastname;
/**
* @ORM\OneToMany(targetEntity="Book", mappedBy="author")
*/
protected $books;
public function __construct()
{
$this->books = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstname
*
* @param string $firstname
* @return Author
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
* @return Author
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Add books
*
* @param \Acme\UserBundle\Entity\Book $books
* @return Author
*/
public function addBook(\Acme\UserBundle\Entity\Book $books)
{
$this->books[] = $books;
return $this;
}
/**
* Remove books
*
* @param \Acme\UserBundle\Entity\Book $books
*/
public function removeBook(\Acme\UserBundle\Entity\Book $books)
{
$this->books->removeElement($books);
}
/**
* Get books
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBooks()
{
return $this->books;
}
}
Book-Entity:
PHP-Code:
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="book")
*/
class Book {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*
* @Assert\NotBlank()
* @Assert\Length(min = "2")
* @Assert\Type(type="string", message="The value {{ value }} is not a valid {{ type }}.")
*/
protected $title;
/**
* @ORM\ManyToOne(targetEntity="Author", inversedBy="books")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*
* @Assert\NotBlank()
* @Assert\Length(min = "2")
* @Assert\Type(type="string", message="The value {{ value }} is not a valid {{ type }}.")
*/
protected $author;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Book
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set author
*
* @param \Acme\UserBundle\Entity\Author $author
* @return Book
*/
public function setAuthor(\Acme\UserBundle\Entity\Author $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return \Acme\UserBundle\Entity\Author
*/
public function getAuthor()
{
return $this->author;
}
}
AuthorType-Class:
PHP-Code:
namespace Acme\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class AuthorType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname','text')
->add('lastname', 'text')
->add('books', new BookType());
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\UserBundle\Entity\Author',
'cascade_validation' => true,
));
}
public function getName()
{
return 'author';
}
}
BookType-Class:
PHP-Code:
namespace Acme\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class BookType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title','text')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\UserBundle\Entity\Book',
));
}
public function getName()
{
return 'book';
}
}
Und schließlich meine Action im Controller:
PHP-Code:
public function createAuthorAndBookAction(){
$author = new Author();
$form = $this->createForm(new AuthorType(), $author);
return $this->render('UserBundle:Main:author.html.twig', array('form' => $form->createView()));
}
PHP-Code:
{{ form(form) }} // normal ausführlicher
Als Fehler kriege ich:
"The form's view data is expected to be an instance of class Acme\UserBundle\Entity\Book, but is an instance of class Doctrine\Common\Collections\ArrayCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\Common\Collections\ArrayCollection to an instance of Acme\UserBundle\Entity\Book."
_________________________________
Ich hoffe jmd. kann mir hier weiter helfen.
Habe nach dem Fehler schon gesucht und etwas herumprobiert, es will aber nicht funktionieren.
Regards
Kommentar