Salut
Je voudrai enregistrer un topic donc j'ai créé une entité topic et une autre nommée poste voila les deux fichiers :
Topic.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| <?php
namespace Siteweb\ForumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Siteweb\ForumBundle\Entity\Topic
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Siteweb\ForumBundle\Entity\TopicRepository")
*/
class Topic
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="Siteweb\ForumBundle\Entity\Poste")
*/
private $poste;
/**
* @ORM\ManyToOne(targetEntity="Siteweb\ForumBundle\Entity\Forum")
*/
private $forum;
/**
* @var string $topicTitre
*
* @ORM\Column(name="topicTitre", type="string", length=255)
*/
private $topicTitre;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set topicTitre
*
* @param string $topicTitre
*/
public function setTopicTitre($topicTitre)
{
$this->topicTitre = $topicTitre;
}
/**
* Get topicTitre
*
* @return string
*/
public function getTopicTitre()
{
return $this->topicTitre;
}
/**
* Set forum
*
* @param Siteweb\ForumBundle\Entity\Forum $forum
*/
public function setForum(\Siteweb\ForumBundle\Entity\Forum $forum)
{
$this->forum = $forum;
}
/**
* Get forum
*
* @return Siteweb\ForumBundle\Entity\Forum
*/
public function getForum()
{
return $this->forum;
}
/**
* Set poste
*
* @param Siteweb\ForumBundle\Entity\Poste $poste
*/
public function setPoste(\Siteweb\ForumBundle\Entity\Poste $poste)
{
$this->poste = $poste;
}
/**
* Get poste
*
* @return Siteweb\ForumBundle\Entity\Poste
*/
public function getPoste()
{
return $this->poste;
}
} |
Poste.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| <?php
namespace Siteweb\ForumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Siteweb\ForumBundle\Entity\Poste
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Siteweb\ForumBundle\Entity\PosteRepository")
*/
class Poste
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Siteweb\ForumBundle\Entity\Topic")
*/
private $topic;
/**
* @var text $posteTexte
*
* @ORM\Column(name="posteTexte", type="text")
*/
private $posteTexte;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set posteTexte
*
* @param text $posteTexte
*/
public function setPosteTexte($posteTexte)
{
$this->posteTexte = $posteTexte;
}
/**
* Get posteTexte
*
* @return text
*/
public function getPosteTexte()
{
return $this->posteTexte;
}
/**
* Set topic
*
* @param Siteweb\ForumBundle\Entity\Topic $topic
*/
public function setTopic(\Siteweb\ForumBundle\Entity\Topic $topic)
{
$this->topic = $topic;
}
/**
* Get topic
*
* @return Siteweb\ForumBundle\Entity\Topic
*/
public function getTopic()
{
return $this->topic;
}
} |
Puis j'ai créé un formulaire avec un fichier "handler" pour l'entité topic et deux fichiers "type" pour les deux entités topic et poste car je voudrai imbriquer le champ du formulaire de poste dans le formulaire de topic vu que le message du topic sera enregistré dans la table poste et le titre du topic dans la table topic
Voici donc les fameux fichiers :
TopicType.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <?php
// src/Siteweb/ForumBundle/Form/TopicType.php
namespace Siteweb\ForumBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class TopicType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('topictitre', 'text')
->add('', new PosteType());
}
public function getName()
{
return 'siteweb_forumbundle_topictype';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Siteweb\ForumBundle\Entity\Topic',
);
}
} |
PosteType.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?php
// src/Siteweb/ForumBundle/Form/PosteType.php
namespace Siteweb\ForumBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PosteType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('posteTexte', 'textarea');
}
public function getName()
{
return 'siteweb_forumbundle_postetype';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Siteweb\ForumBundle\Entity\Poste',
);
}
} |
TopicHandler.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?php
// src/Siteweb/ForumBundle/Form/TopicHandler.php
namespace Siteweb\ForumBundle\Form;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
use Site\ForumBundle\Entity\Topic;
use Site\ForumBundle\Entity\Poste;
use Site\ForumBundle\Entity\TopicRepository;
class TopicHandler
{
protected $form;
protected $request;
protected $em;
public function __construct(Form $form, Request $request, EntityManager $em)
{
$this->form = $form;
$this->request = $request;
$this->em = $em;
}
public function process()
{
if( $this->request->getMethod() == 'POST' )
{
$this->form->bindRequest($this->request);
if( $this->form->isValid() )
{
$this->onSuccess($this->form->getData());
return true;
}
}
return false;
}
public function onSuccess(Topic $topic)
{
$this->em->persist($topic);
$this->em->persist($topic->getPoste());
$this->em->flush();
}
} |
Mon formulaire s'affiche tres bien le prebléme c'est que quand je veux enregistrer en appuyant sur le bouton submit et bien j'ai ce message qui s'affiche :
This form should not contain extra fields
Pouvez vous m'aider svp ?
Cordialement
Partager