Query Paragraph Type in Drupal 8 - drupal

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();

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”

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

Select AVG of rows using Query Builder

I want to select the average of columns 'note' from a table
'noteparagraphe' where
the id_paragraphe is a parameter ($ref), I tried this query but I'm getting nothing back !
$query=$this->get('doctrine.orm.entity_manager')
->createQuery('Select avg(n.note) from ParagrapheBundle:NoteParagraphe n
WHERE n.id_paragraphe = :idModele');
$query->setParameter('idModele', $ref);
$query->execute();
$avgNote = $query->getResult();
the SQL is : SELECT AVG(note) from note_paragraphe WHERE id_paragraphe=?
Use Query builder:
$query = $this->getDoctrine()->getRepository('ParagrapheBundle:NoteParagraphe');
$avgNote = $query->createQueryBuilder('n')
->select('avg(n.note)')
->where('n.id_paragraphe = :idModele')
->setParameter('idModele', $ref);
->getQuery();
See if that works - I haven't tried it, but I think it should work.

If Else in Order by Clause in DB API of 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.

Querybuilder with a 'where' statement can not equal to '#' (For an e-mailadres)

I'm creating a symfony2 project. I would like to select a user from my database where the user's email equals to a variable. I'm receiving the following error:
[Syntax Error] Error: Expected end of string, got '#'
I want to execute this function because useremail is the only unique value that is entered. I 'm using the following code:
$query = $repo->createQueryBuilder('e')
->select('e.userId')
->where('e.userEmail = ' . $userEmail)
->getQuery();
$items = $query->getResult();
var_dump($items);
Thanks in advance
You're not wrapping your email string. Try:
->where('e.userEmail = "' . $userEmail . '"')

Resources