how to select query from drupal 7 - drupal

i have this query on drupal 6
$catq=' ( SELECT term_node.nid as node_id FROM {term_node} WHERE tid='.$catint.') as cat, '
i have upgrade it like this
$query=db_select('term_node');
->addfield('term_node', 'nid', 'node');
->field('term_node', 'node' );
->condition('term_node.tid', = , $catint);
$cat=$query->addfield($query, 'cat');
is it wrong?

You are doing it wrong.
The right syntax for query - keeping your data will be:
$query = db_select('term_node', 't')
->fields('t')
->condition('term_node.tid', $catint, '=')
->execute();
$logo = $query->fetchAll();
But in drupal 7 there is no table term_node, probably you need 'taxonomy_index'.

Related

Doctrine left join with priority on language field with querybuilder

I'm using the query below:
use Doctrine\ORM\Query\Expr\Join;
$query = $this->createQueryBuilder('ad')
->select('ad.id, ad.title, ad.year, ad.hours, ad.status')
->addSelect('rem.remark')
->leftJoin('ad.remark', 'rem', Join::WITH, "rem.language = 'NL'")
->getQuery()
->getResult();
This query is working fine and returns the remark of a ad in the Dutch language. The ad has a one-to-many relation with its remark.
Only I also have ads that have for example an English remark and not a Dutch one. The I will like to get the English remark of that one and on the others in the list still the Dutch remark. So too summarise making a priority list on the languages that are returned?
One way to solve this is to use an extra join without relation:
$query = $this->createQueryBuilder('ad')
->select('ad.id, ad.title, ad.year, ad.hours, ad.status')
->addSelect('rem.remark')
->leftJoin('ad.remark', 'rem', Join::WITH, "rem.language = 'NL' OR rem.language = 'EN'")
->leftJoin(Remark::class, 'customRem', Join::WITH,
"rem.id <> customRem.id
AND rem.ad = customRem.ad
AND customRem.language = 'NL'")
->where('customRem.id IS NULL')
->getQuery()
->getResult();
The idea is
if NL language remark exists for an ad, add this remark joined to each result row of this ad expect to itself(will be null)
if NL language remark does not exist and an EN exists, then the joined row will be null
Finally, the condition customRem.id IS NULL makes this work.
Multiple languages solution
In the case of 3 supported languages, because DE > EN > NL, you could do:
->leftJoin(Remark::class, 'customRem', Join::WITH,
"rem.id <> customRem.id AND rem.ad =
customRem.ad AND rem.language < customRem.language")
For multiple languages and suppose a "customized" ability to order the languages, you could use:
"rem.id <> customRem.id
AND rem.ad = customRem.ad AND
(case when rem.language = 'NL' THEN 3 " .
"when rem.language = 'EN' THEN 2 " .
"when rem.language = 'DE' THEN 1 ELSE 0 END) < (case when customRem.language = 'NL' THEN 3 " .
"when customRem.language = 'EN' THEN 2 " .
"when customRem.language = 'DE' THEN 1 ELSE 0 END)"
Alternatively, you could create "lang_position" table(id, language, position) and join twice to get the position from the language.

How to use join on recent record in db_select drupal 7

I have two tables.
Table One
xx
yy
zz
Table Two
xx-3
xx-2
xx-1
yy-2
yy-1
zz-4
zz-3
zz-2
zz-1
I want to use join between these 2 tables in such a way that it will use latest record from Table Two for each record of table One.
i.e. xx join with xx-3
yy join with yy-2
and zz join with zz-4
query will go like this:
$query = db_select('One', 'o');
$query->leftJoin('Two', 't', 't.column1 = o.column1');
$query->fields('o',array('column1','column2'));
$query->fields('t',array('column1','column2'));
$query->orderBy('o.column1', 'DESC');
How can I do this using db_select in Drupal 7?
Try this code
$query = db_select('Two', 't');
$query->join('One', 'o', 'o.column1 = t.column1');
$query->fields('t',array('column1','column2'));
$query->orderBy('t.column1', 'DESC');
$res = $query->execute()->fetchAll();
Thanks!

Two foreign keys reference to same table in symfony2 querybuilder

Table1(identity1, a1,b1)
Table2(identity, foreign(identity1),foreign(identity1))
Now when I use this query
$query = $qb->select('a1', ' b1')
->from('table2', 'm1')
->join('ApiMapBundle:tabl1, 'u1', 'WITH', $qb->expr()->orX('m1. foreign(a1) =u1.identity', 'm1. foreign(b1) = u1.identity '))
->andWhere('m1.identity=:tt')
->setParameter('tt', $cn)
->getQuery()
->getResult();
Now the problem with this query is that sometimes it gives id let's tt:5 so it gives me a value like this
Array(
0 => Array(id => 1, b1=> 8000225),
1 => Array(id => 9, b1 => 8000234)) given).
Basically in table the values structure is like this
Table2(Identity=5,foreign1=9,foreign2=1)
Any idea that how can I exactly get the given structure? Because in some cases it is fine they give me proper foreign 1 and foreign 2 but in other cases it make it alternative. Any idea?

Doctrine - select last 4 rows from table

I'm using Symfony/Doctrine.
I'm trying to select last 4 rows from table, but im getting error.
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'SELECT c FROM DprocMainBundle:Courses c ORDER BY id DESC LIMIT 4'
);
$course = $query->getResult();
This is my query but it shows error.
Expected end of string, got 'LIMIT'
How should i use limit, and get the LAST 4 rows?
thanks!
Use setMaxResults() to limit the number of results.
$course = $query->setMaxResults(4)->getResult();
If you want to use this for pagination you can add a setFirstResult() call.
$course = $query->setMaxResults(4)->setFirstResult(10)->getResult();

Drupal 7 altering query in view

I'm trying to alter a query in a view using mymodule_views_pre_execute and have used devel to find the sql query it is currently using, which is below:
SELECT node.nid AS nid FROM node node LEFT JOIN field_data_field_date
field_data_field_date ON node.nid = field_data_field_date.entity_id AND
(field_data_field_date.entity_type = :views_join_condition_0 AND
field_data_field_date.deleted = :views_join_condition_1)
WHERE ((
(DATE_FORMAT(field_data_field_date.field_date_value, '%Y-%m-%d\T%H:%i') > :node_date_filter) )AND
(( (node.status = :db_condition_placeholder_2) )))
LIMIT 10 OFFSET 0
I am then re-doing this using the following:
$query = db_select("node", "n");
$query->addField("n", "nid");
$query->leftJoin("{field_data_field_date}", "{field_data_field_date}",
"n.nid = field_data_field_date.entity_id AND field_data_field_date.entity_type = 'node'
AND field_data_field_date.deleted = '0'");
$query->where("(DATE_FORMAT(field_data_field_date.field_date_value, '%Y-%m-%d\T%H:%i') > NOW())");
$query->where("n.status = '1'");
I've had to replace :views_join_condition_0 with 'node', :views_join_condition_1 with '0' and :node_date_filter to NOW() although i'm not sure if this is the correct way? If I leave :views_join_condition_0, :views_join_condition_1 and :node_date_filter in though it doesn't work?!
Use hook_view_query_alter(&$view, &$query) instead.

Resources