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
Related
I have this query filter which works fine to query out the result. For example, if I need to find books which have the Author name something:
Book::with('user', 'author')
->whereHas('author', function ($query) use($filters) {
$query->filter($filters);
})->paginate(30);
But the thing is if I want to order the Books by the author names. I this case nothing happens.
How can I sort books by author name?
QueryFilter looks like this:
public function __construct(Request $request) {
$this->request = $request;
$this->filterService = new FilterService();
}
public function apply(Builder $builder) {
$this->builder = $builder;
foreach ($this->filters() as $name => $value) {
if (method_exists($this, $name)) {
call_user_func_array([$this, $name], array_filter([$value]));
}
}
return $this->builder;
}
public function filters() {
return $this->filterService->removeEmptyFieldsFromRequest($this->request);
}
And the book filter looks like this:
public function date($date) {
return $this->builder->where('date', $date);
}
public function name_sort($order = 'asc') {
return $this->builder->orderBy('name', $order);
}
You can use class join statement :
$books = Book::select('author.name AS author_name, countries.*')
->with('user')
->join('author', 'author.id', '=', 'book.author_id')
->orderBy('author.name', 'ASC')
->paginate(10);
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() ...
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?
I have this code:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$entities = $em
->getRepository('BreedrGeckoBundle:Weight')
->findBy(array('user' => $user), array());
return array(
'entities' => $entities,
);
}
Which is fine, gets all the weights in the database for the current user.
But how can I order the results so that all the weight entries for the same gecko id are together?
My table structure is as follows:
You need to query for your objects using Doctrine's QueryBuilder.
Something like this:
$repository = $this->getDoctrine()
->getRepository('BreedrGeckoBundle:Weight');
$query = $repository->createQueryBuilder('w')
->where('w.user = :user')
->orderBy('w.geckoId ASC')
->getQuery();
$query->setParameter(':user', $user);
$entities = $query->getResult();
If you want additional ordering, you can do something like:
->orderBy('w.geckoId ASC, w.weight ASC, w.id ASC')
You should create your own repository and use the addGroupBy function on the querybuilder. Example:
class WeightRepository extends EntityRepository
{
public function getGroupedByGeckoId(User $user)
{
$qb = $this->createQueryBuilder('weight');
$qb->select('whateverYouWant');
$qb->andWhere('weight.user = :user');
$qb->setParameter('user', $user);
$qb->addGroupBy('weight.geckoId');
return $qb->getQuery()->getResult();
}
}
You can also order by using the repository object itself like so:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$entities = $em
->getRepository('BreedrGeckoBundle:Weight')
->findBy(array('user' => $user), array('geckoId' => 'ASC'));
return array(
'entities' => $entities,
);
}
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