Добавьте значения перед Doctrine Flush

Я хотел бы добавить значения перед промывкой.
Мои другие значения взяты из формы, которая сопоставлена ​​с моей сущностью, поэтому, когда я пытаюсь использовать SetData (), мои поля остаются в «нуле».

Код:

    public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();

$entity = $em->getRepository('BlogBlogBundle:Blog')->find($id);

if (!$entity) {
throw $this->createNotFoundException('Unable to find Blog entity.');
}

$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);

if ($editForm->isValid()) {
$editForm->get('date')->setData(date("Y-m-d"));
$usr= $this->get('security.context')->getToken()->getUser();
$usr->getUsername();
$editForm->get('author')->setData($usr);
var_dump($editForm);
$em->flush();

return $this->redirect($this->generateUrl('blogAdmin_edit', array('id' => $id)));
}

return $this->render('BlogBlogBundle:Admin:edit.html.twig', array(
'entity'      => $entity,
'edit_form'   => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}

0

Решение

Вы DonT нужно использовать получить данные или же УстановитьДанные на форма,
Вы должны использовать установщик самой сущности.
То, что вы сохраните / сохраните, это не форма, а сущность.
Форма также использует установщики класса сущности для данных, которые были отправлены.

лайк:

public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();

$entity = $em->getRepository('BlogBlogBundle:Blog')->find($id);

if (!$entity) {
throw $this->createNotFoundException('Unable to find Blog entity.');
}

$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);

if ($editForm->isValid()) {
$usr = $this->get('security.context')->getToken()->getUser();
//don't know what you wanna do with the username
$usr->getUsername();
//use the setter for the date... maybe $entity->setCreatedAt()
$entity->setDate(date("Y-m-d"));
//also use the setter for the user on the entity
$entity->setAuthor($usr);
var_dump($entity);
$em->flush();

return $this->redirect($this->generateUrl('blogAdmin_edit', array('id' => $id)));
}

return $this->render('BlogBlogBundle:Admin:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
1

Другие решения

Вы должны изменить сущность, которую вы отобразили на форму и использовать правильные установщики полей сущности (я пытался угадать). Попробуйте изменить:

 ...
if ($editForm->isSubmitted() && $editForm->isValid()) {
$entity->setDate(date("Y-m-d"));
$usr= $this->get('security.context')->getToken()->getUser();
$usr->getUsername();
$entity->setAuthor($usr);
$em->persist($entity);
$em->flush();
...
}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector