If Else in Order by Clause in DB API of Drupal - drupal

I have Drupal 7 site. I am using dbApi of Drupal. Now I have a query where I need to order the records.
Table Structure:-
AlbumId
Album Name
Album Created Date
Album Release Date
Now my requirement is if Album Release Date is not NULL then sort by it, else use the Album Created Date for sorting.
$query = db_select('node', 'n');
$query->condition('n.type', 'albums', '=')
->condition('status', 1) //Published.
->fields('n', array('nid'))
->orderBy('field_album_release_date_value', 'DESC')
->execute();
$result = $query->execute();
Any help highly appreciated.

Yes it is possible. You should be using addExpresion($expression).
$query = db_select('node', 'n');
$query->condition('n.type', 'albums', '=')
->condition('status', 1) //Published.
->fields('n', array('nid'));
$query->addExpression('IF(n.field_album_release_date_value is null,'
. 'n.field_album_release_date_value, n.field_album_create_date_value)'
. ' as available_date');
$query->orderBy('available_date', 'DESC');
$result = $query->execute();
Didn't test this, so you might need to tweak it, but fact is you can use addExpression to insert sql functions like min max or in your case if.

Related

Compare a count of subquery with scalar

I'm using Symfony 4.3 and I have a problem in my DQL query.
The purpose of my query is to select email of user how have list of right for this is my DQL query:
$qb = $this->createQueryBuilder('u')
->select('u.email,u.id')
->leftJoin('u.profile', 'profile')
->leftJoin('u.country', 'country')
->leftJoin('profile.privileges', 'pri')
->leftJoin('pri.ressource', 'resource')
$checkRightQuery = $this->em->createQueryBuilder()
->select('count(rc)')
->from(Ressource::class, 'rc')
->leftJoin('rc.privileges', 'privil')
->leftJoin('privil.profile', 'prof')
->leftJoin('prof.user', 'user')
->where( $this->em->getExpressionBuilder()->in('rc.actionFront', ':rights'))
->andWhere('user.id =u.id');
$qb->andWhere(
$this->em->getExpressionBuilder()->eq(count($rights),
$checkRightQuery
)
);
$qb->setParameters(['rights' => $rights]);
The problem is when I take the result of the count it's not a scalar and it can't compare it to scalar.
Any help please?
Try using parenthesis on your condition with the subquery:
$qb->andWhere(
$this->em->getExpressionBuilder()->eq(count($rights),
'(' . $checkRightQuery . ')'
)
);
References
Doctrine return error with “eq”, no with “in”

Query Paragraph Type in Drupal 8

I am trying to Query all paragraphs of a certain type but I am not getting any output. Is my query right? Currently being returned an empty array.
$query = $this->node->getQuery()
->condition('type', 'my_paragraph_type')
->execute();
$this->logger->info('query: ' . json_encode($query));
Try with entityQuery('entity_type')
$pids = \Drupal::entityQuery('paragraph')
->condition('type', 'my_paragraph_type')
->execute();

Convert SQL query into Drupal 7 PHP rules

SELECT address
FROM user_address
WHERE username = '$user->name'
ORDER BY time DESC
LIMIT 1
Here is the SQL query that I can understand. How is it possible to convert it into Drupal's 7 PHP? I'm trying to figure that out for a day, and I tried different approaches, but it seems that I am just bad in that.
You can use db_select :
$results = db_select('user_address', 'ua')
->fields('ua', array('address'))
->condition('ua.username', $user->name, '=')
->orderBy('ua.time', 'DESC')
->range(0,1)
->execute()
->fetchAll();
var_dump($results);
Otherwise you can use db_query if you want to write entire SQL :
$results = db_query("SELECT address
FROM user_address
WHERE username = :username
ORDER BY time DESC
LIMIT 1 ", array(':username' => $user->name))->fetchAll();
var_dump($results);
Finally you can use db_query_range :
$page = 0;
$limit = 1
$results = db_query_range("SELECT address
FROM user_address
WHERE username = :username
ORDER BY time DESC",
$page * $limit,
$limit,
array(':username' => $user->name))
->fetchAll();
var_dump($results);
Try this:
$result = db_select('user_address', 'ua')
->fields('ua', array('address'))
->condition('ua.username', $user->name)
->execute()
->fetchAll();
For that we use db_select() or db_query() - first one preferable.
$query = db_select('user_address', 'u');
// Add condition and fields
$query->condition('u.username', ‘james’)
$query->fields('u’ array('u.address'));
// execute it
$result = $query->execute();
foreach ($result as $record) {
// Do something with each $record
}
For more see
https://www.drupal.org/docs/7/api/database-api/dynamic-queries/introduction-to-dynamic-queries.
update: see condition portion. Also, you can put this in a module or php executable area of your site or via drush command line.
Change the username James to match your need
$result = $result = db_select('usr_address','u')
->fields('u',array('address','uid'))
->range(0,1)
->orderby('time', 'DESC')
->condition('u.uid',$uid,'=')
->execute();
here is how it actually worked.
Thank you for your suggestions, but at the end I made it. By myself. Well, kinda ;D

symfony2 doctrine query negation of where

i wanna query for all of my categories like this:
$othercategories = $this->getDoctrine()->getRepository('Bundle:Category')->findBy(
array('language' => $language, 'active' => 1),
array('sorting' => 'ASC')
);
what i wanna do is to add another parameter to my query, i want all categories EXCEPT one with a specific id. so like:
WHERE id NOT IN ( 2 )
or
WHERE id <> 2
how can i achieve that?
You can use DQL queries like this
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery( 'SELECT c FROM Bundle:Category c WHERE c.language = :language AND c.active = 1 AND c.id NOT IN ( 2 ) ORDER BY c.language ASC' )
->setParameter('language', $language);
$category= $query->getResult();
Sorry I couldn't test this because I am using my phone to answer this question and I don't know your entity variables. Let me know what changes you did to make it work, it will help others.
For more info check http://symfony.com/doc/master/book/doctrine.html
You can add these queries in repository and reuse them. Refer the Cook book on http://symfony.com/doc/master/cookbook/index.html
Hope this helped.
You can use this syntax if you prefer
$repository = $this->getDoctrine()->getRepository('Bundle:Category');
$queryBuilder = $repository->createQueryBuilder();
$notInCategoryIds = array(2); // Category ids that will be excluded
$queryBuilder->select('c')
->from('Bundle:Category', 'c')
->where('c.language = :language')->setParameter('language', $language)
->andWhere('c.active = :active')->setParameter('active', 1)
->andWhere($queryBuilder->expr()->notIn('c.id', $notInCategoryIds)
->orderBy('c.sorting', 'ASC');
$results = $queryBuilder->getQuery()->getResult();
It's probably going to be more useful for other developers that prefers this syntax

'where not in' query with doctrine query builder

Im trying to reproduce this query:
SELECT * FROM `request_lines`
where request_id not in(
select requestLine_id from `asset_request_lines` where asset_id = 1
)
in doctrine query builder,
I am stuck on the where request_id not in(select
I currently have:
$linked = $em->createQueryBuilder()
->select('rl')
->from('MineMyBundle:MineRequestLine', 'rl')
->where()
->getQuery()
->getResult();
You need to use query builder expressions, and this means you need access to the query builder object. Also, the code is easier to write if you generate the subselect list ahead of time:
$qb = $em->createQueryBuilder();
$nots = $qb->select('arl')
->from('$MineMyBundle:MineAssetRequestLine', 'arl')
->where($qb->expr()->eq('arl.asset_id',1))
->getQuery()
->getResult();
$linked = $qb->select('rl')
->from('MineMyBundle:MineRequestLine', 'rl')
->where($qb->expr()->notIn('rl.request_id', $nots))
->getQuery()
->getResult();
It is possible to do this in one Doctrine query:
$qb = $this->_em->createQueryBuilder();
$sub = $qb;
$sub = $qb->select('arl')
->from('$MineMyBundle:MineAssetRequestLine', 'arl')
->where($qb->expr()->eq('arl.asset_id',1));
$linked = $qb->select('rl')
->from('MineMyBundle:MineRequestLine', 'rl')
->where($qb->expr()->notIn('rl.request_id', $sub->getDQL()))
->getQuery()
->getResult();
Check the reference in this answer here
Using Symfony 5, this solution might help those, who are trying to set parameters on a subquery, the notIn() 2nd argument accepts an array or you could pass a DQL instead and that's what we are doing here and keep in mind that the parameters should be added to the main query as below.
$main = $this->em->createQueryBuilder();
$sub = $main;
$sub = $sub->select('arl')
->from('$MineMyBundle:MineAssetRequestLine', 'arl')
->where($sub->expr()->eq('arl.asset_id',':id'));
$linked = $main->select('rl')
->from('MineMyBundle:MineRequestLine', 'rl')
->where($main->expr()->notIn('rl.request_id', $sub->getDQL()))
->setParameter('id', 1)
->getQuery()
->getResult();

Resources