public function updateSubTopicAction(Request $request ,$id) {
$updatesubtopic = $this->getDoctrine()
->getRepository('FeedbackBundle:Subtopics')
->findASubtopics($id);
//print_r($updatesubtopic);die;
if ($request->getMethod() == 'POST') {
$update = $request->request->all();
//print_r($update);die;
$updatesubtopic->setTopic($update['Topicname']);
$updatesubtopic->setName($update['subtopicname']);
$updatesubtopic->setDetails($update['subtopicdetails']);
//print_r($updatesubtopic);die;
$em = $this->getDoctrine()->getManager();
$em->persist($updatesubtopic);
$em->flush();
return new RedirectResponse($this-generateUrl('view_topic'));
}
return $this-render(
'FeedbackBundle:SubTopic:update_sub_topic.html.php',
array('updatesubtopic'=>$updatesubtopic)
);
}
how can i update data?
Related
I am having this issue where I'm only able to get the error message. I have some tables where student id is the foreign key, however even thou the id number is not any of the tables it still gives the message "You cannot delete this Student" but won't pass there if it can be deleted
public function findBystudentid($studentid)
{
$record= $this->getEntityManager()->getRepository('AcmeDemoBundle:record')->findBy(['studentid' => $studentid]);
$lecture = $this->getEntityManager()->getRepository('AcmeDemoBundle:lecture')->findBy(['studentid' => $studentid]);
$faculty = $this->getEntityManager()->getRepository('AcmeDemoBundle:faculty')->findBy(['studentid' => $studentid]);
if ($record||$lecture||$faculty){
return true;
} else {
return false;
}
}
public function deleteAction(Request $request, $studentid)
{
$form = $this->createDeleteForm($studentid);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$deletable = $em->getRepository('AcmeDemoBundle:Student')->findBystudentid($studentid);
if ($deletable) {
$this->addFlash('error','ERROR! You cannot delete this Student' );
}
else
{
$em->remove($deletable);
$em->flush();
$this->addFlash('error','Student Deleted');
}
return $this->redirect($this->generateUrl('Student'));
}
First, your naming is a bit off. You need to fix it as it tends to be a bit confusing. With that in mind, I suggest you do it like this:
1. Controller method to check if student is deletable:
private function isStudentDeletable($studentid)
{
$em = $this->getEntityManager();
$record= $em->getRepository('AcmeDemoBundle:record')->findBy(['studentid' => $studentid]);
if ( $record ){
return false;
}
$lecture = $em->getRepository('AcmeDemoBundle:lecture')->findBy(['studentid' => $studentid]);
if ( $lecture ){
return false;
}
$faculty = $em->getRepository('AcmeDemoBundle:faculty')->findBy(['studentid' => $studentid]);
if ( $faculty ){
return false;
}
return true;
}
2. Controller's action to invoke the above
public function deleteAction(Request $request, $studentid)
{
$form = $this->createDeleteForm($studentid);
$form->handleRequest($request);
$deletable = $this->isStudentDeletable($studentid);
if (!$deletable) {
$this->addFlash('error','ERROR! You cannot delete this Student' );
}
else
{
$em = $this->getDoctrine()->getManager();
$student = $em->getRepository('AcmeDemoBundle:Student')->find($studentid)
$em->remove($student);
$em->flush();
$this->addFlash('error','Student Deleted');
}
return $this->redirect($this->generateUrl('Student'));
}
Hope this help and clarifies a bit.
I think you are calling findBystudentid wrong because findBystudentid is not in the Entity.
Here is the updated version
public function deleteAction(Request $request, $studentid)
{
$form = $this->createDeleteForm($studentid);
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$deletable = $this->findBystudentid($studentid);
if ($deletable) {
$this->addFlash('error','ERROR! You cannot delete this Student' );
} else {
$em->getRepository('AcmeDemoBundle:Student')->findBy(['studentid' => $studentid])
$em->remove($deletable);
$em->flush();
$this->addFlash('error','Student Deleted');
}
return $this->redirect($this->generateUrl('Student'));
}
Also findBystudentid should be a private function
private function findByStudentId() ...
i want to edit my data with symfony form and i have a problem
probably with my controller. I have some like this :
public function detailAction($id,Request $request)
{
$order = $this->getDoctrine()->getRepository(OrderMain::class)->find($id);
if (!$order) {
throw $this->notFoundException();
}
$form = $this->createForm(OrderMainType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// do not enter here
$orderEdit = $form-getData();
$em = $this->getDoctrine()->getManager();
$em->persist($orderEdit);
$em->flush();
}
return $this->render('ModiModiAdminBundle:Order:detail.html.twig',array(
'form' => $form->createView(),
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
/.../
->add('edit', SubmitType::class, array(
'attr' =>array('class'=>'edit'),
));
}
All show corectly but when i click a button my page is reload ( dosen't save changes ). Thanks for help.
It is an issue with your controller method. Below should work for you.
public function detailAction($id,Request $request)
{
$order = $this->getDoctrine()->getRepository(OrderMain::class)->find($id);
if (!$order) {
throw $this->notFoundException();
}
$form = $this->createForm(OrderMainType::class, $order);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//do not enter here
$em = $this->getDoctrine()->getManager();
$em->flush();
}
return $this->render('ModiModiAdminBundle:Order:detail.html.twig',array(
'form' => $form->createView(),
));
}
You can remove the line of $orderEdit = $form-getData();. When your form is submitted, the entity should be updated based on the submitted data. Since this is already a managed entity, you can also remove $em->persist($orderEdit);
public function edit(Request $request)
{
$id = $request->get('id');
$category = $this->getDoctrine()->getRepository(Category::class)->find($id);
if (!$category) {
throw $this->notFoundException();
}
$form = $this->createForm(CategoryType::class,$category);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$categoryData = $form->getData();
$em->persist($categoryData);
$em->flush();
}
return $this->render('admin/category/edit.html.twig',array(
'form' => $form->createView(),
));
}
you are missing => handle request from code
$form->handleRequest($request);
I have created a search form,i'm trying to fetch the following data from the database (airport,airport1,departuredate,price),i can only get [airport] but not the other entity.
This is my controller:
public function searchtabflightResultAction(Request $request)
{
$form = $this->createForm(new SearchflightType());
if ($this->get('request')->getMethod() == 'POST')
{
$form->handleRequest($request);
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form['airport']->getData());
} else {
throw $this->createNotFoundException('The page you were looking for doesn\'t exist.');
}
return $this->render('FLYBookingsBundle:Post:searchtabflightResult.html.twig', array('entities' => $entities));
}
PostRepository.php
public function searchflight($entity)
{
$qb = $this->createQueryBuilder('u')
->select('u')
->where('u.airport like :entity')
->andWhere('u.airport1 like :entity')
->orderBy('u.id')
->setParameter('entity', '%'.$entity.'%');
return $qb->getQuery()->getResult();
}
Try something like :
In your controller:
$entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form);
In your repo:
public function searchflight(FormInterface $form)
{
$qb = $this->createQueryBuilder('u');
if ($form->has('airport')) {
$qb->andWhere(
$qb->expr()->like('u.airport', ':airport')
)
->setParameter('airport', '%'. $form->get('airport')->getData().'%')
}
// ... complete like this with the other params
when I try to update one of my entites, I get this exception:
UndefinedMethodException: Attempted to call method "bindRequest" on class "Symfony\Component\Form\Form" in /Applications/MAMP/htdocs/Seotool/src/Seotool/MainBundle/Controller/TaskController.php line 64.
edit Action:
/**
#Route(
* path = "/tasks/edit/{id}",
* name = "edit_task"
* )
* #Template()
*/
public function edit_taskAction($id, Request $request)
{
$request = $this->get('request');
if (is_null($id)) {
$postData = $request->get('task');
$id = $postData['id'];
}
$em = $this->getDoctrine()->getManager();
$task = $em->getRepository('SeotoolMainBundle:Task')->find($id);
$form = $this->createForm(new TaskType(), $task);
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// perform some action, such as save the object to the database
$em->flush();
return $this->redirect($this->generateUrl('taskmanager'));
}
}
return array('form' => $form->createView());
}
What's wrong with my code?
Because there is no method bindRequest. The exception is quite explicit. If you check the official API, I suppose you want to use handleRequest
I am super new to doctrine and I just converted the Akrabat album rest api example to work with doctrine and it works well to show the data. But when i edit, delete or add albums it throws an error which says,
Fatal error: Call to undefined method Zend\Http\PhpEnvironment\Request::post()
Here's is my controller,
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController,
Zend\View\Model\ViewModel,
Album\Form\AlbumForm,
Doctrine\ORM\EntityManager,
Album\Entity\Album;
class AlbumController extends AbstractActionController
{
protected $em;
public function setEntityManager(EntityManager $em)
{
$this->em = $em;
}
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
return $this->em;
}
public function indexAction()
{
return new ViewModel(array(
'albums' => $this->getEntityManager()->getRepository('Album\Entity\Album')->findAll()
));
}
public function addAction()
{
$form = new AlbumForm();
$form->get('submit')->setAttribute('label', 'Add');
$request = $this->getRequest();
if ($request->isPost()) {
$album = new Album();
$form->setInputFilter($album->getInputFilter());
$form->setData($request->post());
if ($form->isValid()) {
$album->populate($form->getData());
$this->getEntityManager()->persist($album);
$this->getEntityManager()->flush();
// Redirect to list of albums
return $this->redirect()->toRoute('album');
}
}
return array('form' => $form);
}
public function editAction()
{
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
if (!$id) {
return $this->redirect()->toRoute('album', array('action'=>'add'));
}
$album = $this->getEntityManager()->find('Album\Entity\Album', $id);
$form = new AlbumForm();
$form->setBindOnValidate(false);
$form->bind($album);
$form->get('submit')->setAttribute('label', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->post());
if ($form->isValid()) {
$form->bindValues();
$this->getEntityManager()->flush();
// Redirect to list of albums
return $this->redirect()->toRoute('album');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
public function deleteAction()
{
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
if (!$id) {
return $this->redirect()->toRoute('album');
}
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->post()->get('del', 'No');
if ($del == 'Yes') {
$id = (int)$request->post()->get('id');
$album = $this->getEntityManager()->find('Album\Entity\Album', $id);
if ($album) {
$this->getEntityManager()->remove($album);
$this->getEntityManager()->flush();
}
}
// Redirect to list of albums
return $this->redirect()->toRoute('default', array(
'controller' => 'album',
'action' => 'index',
));
}
return array(
'id' => $id,
'album' => $this->getEntityManager()->find('Album\Entity\Album', $id)->getArrayCopy()
);
}
}
What am i doing wrong here, I am not able to persist any changes that i make.
Because there's no post method. Try getPost(); see here Zend\Http\Request