Bonjour,
Je rencontre un problème pour créer un formulaire imbriquée assez classique.
J'ai une entité Post qui va donc retenir des postes, ces postes pourront avoir plusieurs images/vidéos donc pour ce faire j'ai créé une deuxième entités Media pour la table qui va gérer les différents médias.
Entité Post :
<?php
/**
* Created by PhpStorm.
* User: jeremybarette
* Date: 11/12/2017
* Time: 17:38
*/
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity()
* @ORM\Table(name="posts")
* @ORM\HasLifecycleCallbacks()
*/
class Post
{
/**
* @var id
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category")
* @ORM\JoinColumn(nullable=false)
*/
private $category;
/**
* @ORM\Column(type="string", unique=true)
*/
private $name;
/**
* @ORM\Column="string")
*/
private $slug;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="datetime")
* @Assert\DateTime()
*/
private $publishedAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Assert\DateTime()
*/
private $updatedAt;
/**
* Post constructor.
*/
public function __construct()
{
$this->publishedAt = new \DateTime();
}
/**
* @return id
*/
public function getId(): id
{
return $this->id;
}
/**
* @param id $id
*/
public function setId(id $id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getCategories()
{
return $this->categories;
}
/**
* @param mixed $categories
*/
public function setCategories($categories)
{
$this->categories = $categories;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getSlug()
{
return $this->slug;
}
/**
* @param mixed $slug
*/
public function setSlug($slug)
{
$this->slug = $slug;
}
/**
* @return mixed
*/
public function getContent()
{
return $this->content;
}
/**
* @param mixed $content
*/
public function setContent($content)
{
$this->content = $content;
}
/**
* @return mixed
*/
public function getPublishedAt()
{
return $this->publishedAt;
}
/**
* @param mixed $publishedAt
*/
public function setPublishedAt($publishedAt)
{
$this->publishedAt = $publishedAt;
}
/**
* @return mixed
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* @param mixed $updatedAt
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
}
/**
* @ORM\PreUpdate
*/
public function updateDate()
{
$this->setUpdatedAt(new \DateTime());
}
/**
* @return mixed
*/
public function getCategory()
{
return $this->category;
}
/**
* @param mixed $category
*/
public function setCategory($category)
{
$this->category = $category;
}
}
Entité Media :
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @ORM\Table(name="medias")
*/
class Media
{
/**
* @var int
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var int
*
* @ORM\ManyToOne(targetEntity="App\Entity\Post")
* @ORM\JoinColumn(nullable=false)
*/
private $post;
/**
* @ORM\Column(type="integer")
*/
private $type;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $path;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $alt;
private $file;
private $tmpFilename;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $embed;
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id)
{
$this->id = $id;
}
/**
* @return int
*/
public function getPost(): int
{
return $this->post;
}
/**
* @param int $post
*/
public function setPost(int $post)
{
$this->post = $post;
}
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @param mixed $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* @return mixed
*/
public function getPath()
{
return $this->path;
}
/**
* @param mixed $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* @return mixed
*/
public function getAlt()
{
return $this->alt;
}
/**
* @param mixed $alt
*/
public function setAlt($alt)
{
$this->alt = $alt;
}
/**
* @return mixed
*/
public function getFile()
{
return $this->file;
}
/**
* @param mixed $file
*/
public function setFile($file)
{
$this->file = $file;
}
/**
* @return mixed
*/
public function getTmpFilename()
{
return $this->tmpFilename;
}
/**
* @param mixed $tmpFilename
*/
public function setTmpFilename($tmpFilename)
{
$this->tmpFilename = $tmpFilename;
}
/**
* @return mixed
*/
public function getEmbed()
{
return $this->embed;
}
/**
* @param mixed $embed
*/
public function setEmbed($embed)
{
$this->embed = $embed;
}
}
A côté j'ai deux formulaires, l'un pour gérer l'entité Post l'autre pour gérer l'entité Media.
Formulaire Post :
<?php
namespace App\Form;
use App\Entity\Post;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TrickType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('content', TextareaType::class)
->add('media', MediaType::class)
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Post::class
));
}
}
Formulaire Media:
<?php
namespace App\Form;
use App\Entity\Media;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class MediaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', FileType::class)
->add('embed', TextType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Media::class
));
}
}
Et pour finir j'ai évidemment un controller "BlogController" :
<?php
namespace App\Controller;
use App\Entity\Post;
use App\Form\TrickType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class BlogController extends Controller
{
/**
* @Route("/blog/add", name="blog_add")
* @Security("has_role('ROLE_USER')")
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function add(Request $request)
{
$post = new Post();
$form = $this->createForm(TrickType::class, $post);
$form->handleRequest($request);
return $this->render('blog/add.html.twig', [
'form' => $form->createView()
]);
}
}
Qui va juste se contenter pour le moment d'envoyer le formulaire à la vue.
Pour l'instant le résultat n'est pas au rendez-vous. Voici l'erreur obtenue :
Neither the property "media" nor one of the methods "getMedia()", "media()", "isMedia()", "hasMedia()", "__get()" exist and have public access in class "App\Entity\Post".
L'erreur est assez logique effectivement mais du coup je ne vois pas du tout comment faire mon formulaire imbriqué. Je trouve ça totalement illogique de mettre les getters/setters dans l'entité Post sachant que ce n'est pas elle qui gère les médias.
J'espère que j'ai été clair, je vous remercie de votre aide !