Bonjour,
Voila je rencontre un petit problème avec mon code.
Dans la page show_message, il y a un form qui permet de laisser une message, avec les fields "title, message, parents_id"
namespace Yoda\MessageBundle\Entity;
...
/**
* @ORM\ManyToOne(targetEntity="Message")
* @ORM\JoinColumn(name="parents_id", referencedColumnName="id")
*/
public $parents;
function getParents()
{
return $this->parents;
}
function setParents(Message $parents)
{
$this->parents = $parents;
return $this;
}
...
namespace Yoda\MessageBundle\Form;
...
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('message', TextareaType::class, array('attr' => array('class' => 'tinymce')))
//->add('active')
->add('parents', HiddenType::class);
//->add('owner');
}
...
namespace Yoda\MessageBundle\Controller;
public function newAction(Request $request)
{
$message = new Message();
$form = $this->createForm('Yoda\MessageBundle\Form\MessageType', $message);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$message->setCreateDate(new \DateTime());
$message->setOwner($this->getUser());
$em->persist($message);
$em->flush();
// if it is a children message, we will show parents message
$redirectId = $message->getParents()->getId() ? $message->getParents()->getId() : $message->getId();
return $this->redirectToRoute('message_show', array('id' => $redirectId));
}
return array(
'message' => $message,
'form' => $form->createView(),
);
}
public function showAction(Message $message)
{
$addForm = $this->createForm('Yoda\MessageBundle\Form\MessageType');
$addForm->get('parents')->setData($message->getId());
$childrenMessage = $this->getChildren($message);
//var_dump($childrenMessage);
return array(
'add_form' => $addForm->createView(),
'message' => $message,
'childrens' => $childrenMessage,
);
}
quand on répond, on ajoute une ligne dans la base.
Quand on commit, Il me retourne une message érreur
Catchable Fatal Error: Argument 1 passed to Yoda\MessageBundle\Entity\Message::setParents() must be an instance of Yoda\MessageBundle\Entity\Message, string given, called in C:\xampp\htdocs\symfony_projet2\vendor\symfony\symfony\src\Symfony\Component\PropertyAccess\PropertyAccessor.php on line 556 and defined
Bonsoir, l'erreur est assez parlante ta méthode setParents attend un objet de type Message et tu lui passes un string. Je pense que ton erreur est là :
public function showAction(Message $message)
{
$addForm = $this->createForm('Yoda\MessageBundle\Form\MessageType');
$addForm->get('parents')->setData($message->getId()); //cette ligne à replacer par $addForm->get('parents')->setData($message);
$childrenMessage = $this->getChildren($message);
//var_dump($childrenMessage);
return array(
'add_form' => $addForm->createView(),
'message' => $message,
'childrens' => $childrenMessage,
);
}