Symfony - query sum - php

I am writing a query which will return amount of total spent money for that user. Total spent = sum of all transaction amounts, where type = spend. I have set my type field in a base to 'spend'..
It returns
Invalid parameter: token type is not defined in the query.
My code.
public function getTotal(User $user, $type)
{
$query = $this->getSpendingRepository()
->createQueryBuilder('p')
->select("sum(p.amount) as total_amount")
->where('p.type = :spend')
->andWhere('p.user = :user')
->setParameters(['user' => $user,
'type' => $type])
->getQuery()
->getOneOrNullResult();
return $query;
}
and this is sql that is working so I don't know what am I doing wrong..
select user_id, sum(amount) as amount
from transaction
where type = 'spend'
group by user_id

Replace
->where('p.type = :spend')
with
->where('p.type = :type')

I fixed it.
public function getTotalSpent(User $user, $type = 'spend')
{
$query = $this->getTransactionRepository()
->createQueryBuilder('p')
->select("sum(p.amount) as total_amount")
->where('p.type = :type')
->andWhere('p.user = :user')
->setParameters(['user' => $user,
'type' => $type])
->getQuery()
->getOneOrNullResult();
return $query;
}

Related

Replace Having Cause with Where Clause

Please i have some trouble in my project in symfony
How can i replace having clause wuĆ­th where clause in this code above cause having clause dont work white paginator in symfony
...
...
public function getAllClients($all = true, $user = null)
{
$qb = $this->_em->createQueryBuilder();
//
$qb->select('u, count(ls.id) as nbr')
->from($this->_entityName, 'u')
->join(Client::class, 'c', Join::WITH, 'c.user = u.id')
->join(LicenceShop::class, 'ls', Join::WITH, 'ls.client = c.id')
->groupBy('u.id')
->having('nbr > 1');
$qb->andWhere('u.roles LIKE :roles');
$qb->setParameter('roles', '%"ROLE_CLIENT"%');
return $qb->getQuery();
}
Thanks you
When i use having clause symfony response like that
Cannot count query that uses a HAVING clause. Use the output walkers for pagination
And this the function
enter code public function show($id = null, UserRepository $userRepository, Request $request, PaginatorInterface $paginator)
{
if($id)
$user = $userRepository->find($id);
else
$user = $this->getUser();
$client = $user->getClientInfo();
$myClients = $userRepository->getAllClients(false, $user);
$myClients = $paginator->paginate(
$myClients,
$request->query->getInt('page_c', 100)
);
$referal = null;
$licences = null;
if ($client) {
$referal = $client->getReferal();
$licences = $client->getLicences();
}
//$licences = $module->getLicences();
return $this->render('resellers/show.html.twig', [
'controller_name' => 'ResellersController',
'user' => $user,
'client' => $client,
'referal' => $referal,
'licences' => $licences,
'my_clients' => $myClients,
]);
}

Symfony - Doctrine - Select query with a user (but can be anonymous user)

I'm using symfony 3, and I'm storing some data to database.
For this, the user does not have to be logged in.
Now I've created a select query with $this->getUser();. But the User can be NULL. When I execute this query, it does not select any rows.
return $this->createQueryBuilder('product')
->where('product.user = :user')
->andWhere('product.product = :product')
->andWhere('product.cartId = :uniqueCartId')
->setParameters([
'product' => $product,
'user' => $user,
'uniqueCartId' => $uniqueCartId
])
->getQuery()
->execute();
$user = $this->getUser();
I already tried this:
if(!$user instanceof User) $user = NULL;
But that does not work.
If I remove the User check, it works.
Any ideas?
I guess you should bind the where clause if there is an user object like
$parameters=array(
'product' => $product,
'uniqueCartId' => $uniqueCartId
);
$user = $this->getUser();
$query = $this->createQueryBuilder('product')
->where('product.product = :product')
->andWhere('product.cartId = :uniqueCartId');
if($user != null){ /* if($user instanceof User) */
$parameters['user'] = $user;
$query = $query->andWhere('product.user = :user');
}else{
/* $query = $query->andWhere('product.user IS NULL'); Not necessary */
}
$query->setParameters($parameters)
->getQuery()
->execute();

Calculation in codeigniter

Here I try to reduce the number of leave limit by the amount of leave 4-12, but every time I want to reduce the amount is always only the previous number is reduced
controller
public function approved($id)
{
$data = array(
'status'=> 'approved'
);
$this->Mcuti->update($id, $data);
$operasi = array(
'jumlah_bc'=> 'jumlah_bc'-'jumlah_cuti'
);
$this->Mcuti->pengurangan($id, $operasi);
redirect('backend/cuti/preview');
}
Model
public function pengurangan($id, $operasi)
{
$this->db->query('UPDATE batas_cuti JOIN cuti ON batas_cuti.nik=cuti.nik SET batas_cuti.jumlah_bc=batas_cuti.jumlah_bc-cuti.jumlah_cuti');
$query = $this->db->where($id);
return $query;
}

Display only last 5 records from database using Codeigniter 3

I wanted to get recent records from database limit only to 5. I used the code below but not working. Maybe I missed something or the whole query is wrong. Please guide me. Thanks
Model
public function get_comments ($id) {
$this->db->select('*');
$this->db->order_by('id', 'ASC');
$this->db->from('Item_comments');
$this->db->limit('5');
$this->db->where(array('checklist_item_id' => $id, 'status' => 1));
$query = $this->db->get();
return $query->result_array();
}
Note: query works fine and get 5 records from db but I want to
position the recent into the bottom. Thanks
To get the last results you need to order them descending
$this->db->order_by("id", "desc");
Of course you can also use other columns instead of id.
To order your results ascending:
$this->db->order_by("id", "asc");
To reverse the resulting array:
$results = get_comments();
$results = array_reverse($results);
Or if you would like to do it in the function:
return array_reverse($query->result_array());
Try this
public function get_comments ($id) {
$sql = 'SELECT * FROM (SELECT Item_comments.id i_id,Item_comments.column_name1,Item_comments.column_name2 FROM Item_comments WHERE Item_comments.checklist_item_id = ' . $id . ' AND Item_comments.status = 1 ORDER BY Item_comments.id DESC LIMIT 5) T1 ORDER BY i_id ASC';
$query = $this->db->query($sql);
return $query->result_array();
}
Try this
function get_comments($start,$limit,$id)
{
$condition = array('checklist_item_id' => $id,'status'=>1);
$this->db->order_by('id', 'DESC');
$this->db->limit($limit, $start);
$query = $this->db->get_where('Item_comments',$condition);
$rows = $query->result();
return $rows;
}
public function get_comments ($id) {
$this->db->select('*');
$this->db->order_by('id', 'DESC');
$this->db->from('Item_comments');
$this->db->limit('5');
$this->db->where(array('checklist_item_id' => $id, 'status' => 1));
$query = $this->db->get();
return $query->result_array();
}
Try this one :
public function get_comments ($id) {
$this->db->select('*');
$this->db->order_by('id', 'DESC');
$this->db->from('Item_comments');
$this->db->limit('0,5');
$this->db->where(array('checklist_item_id' => $id, 'status' => 1));
$query = $this->db->get();
return $query->result_array();
}

Symfony2 AJAX problems, response returns not refreshed data

I'm working with Symfony2 and AJAX and have some problems, my controller returns old data from database. I tried var_dump variable before returning and everything was fine, but JsonResponse returns old data.
My controller:
public function returnOrderAction(Request $request)
{
$id = $request->request->get('id');
$em = $this->getDoctrine()->getManager();
$bookEntity = $em->getRepository('BooksBundle:Book')->find($id);
$user = $this->getUser()->getId();
$this->get('order.complete')->returnOrder($bookEntity, $user);
$quantity = $bookEntity->getQuantity();
if (!$bookEntity) {
throw $this->createNotFoundException('Unable to find Book entity.');
}
return new JsonResponse(array(
'quantity' => $quantity,
), 200);
order.complete service:
public function returnOrder(Book $bookEntity, $userId)
{
$dq = $this->entityManager->createQueryBuilder('o')
->update('BaseBundle:Order', 'o')
->set('o.status', ':status')
->where('o.book = :id')
->andWhere('o.user = :user')
->andWhere('o.status = \'active\'')
->setParameter('status', 'done')
->setParameter('id', $bookEntity)
->setParameter('user', $userId)
->setFirstResult(0)
->setMaxResults(1)
->getQuery();
$dq->execute();
$this->entityManager->getRepository('BooksBundle:Book')->updateQuantityPos($bookEntity);
}

Categories