I have a query that perform normally with MySQL :
SELECT *
FROM td_user u
JOIN td_ranking ranking ON ranking.user_id = u.id
JOIN (
SELECT x.user_id,
MAX(x.id) AS default_id
FROM td_ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id
I try to transform it in DQL for run it in Symfony :
$query = $this->_em->createQuery('
SELECT u.*,ranking.*
FROM UserBundle:User u
JOIN UserBundle:Ranking ranking
WITH ranking.user_id = u.id
JOIN (
SELECT x.user_id, MAX(x.id) AS default_id
FROM UserBundle:Ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id'
);
$results = $query->getResult();
I have this error :
[Semantical Error] line 0, col 113 near '(SELECT x.user_id,': Error: Class '(' is not defined.
Do you have any idea please ? Thanks!
Use native query
$rsm = new ResultSetMapping();
$sql = "
SELECT *
FROM td_user u
JOIN td_ranking ranking ON ranking.user_id = u.id
JOIN (
SELECT x.user_id,
MAX(x.id) AS default_id
FROM td_ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id
";
$result = $this->getEntityManager()->createNativeQuery($sql, $rsm)->getResult();
Related
I have a raw query which I would like to convert into doctrine ORM. It's basically a query which contains sub query to calculate total count.
SELECT Count(*) AS total_count
FROM (SELECT *
FROM content_item_languages
WHERE default_content_item_id IN (SELECT id AS default_content_item_id
FROM content_item
WHERE content_type = 1
AND is_translated = 0
AND modified_on >=
'$timePeriodStart'
AND is_active = 1)
AND language_id = '$language') AS t
I have written below doctrine ORM but still I am getting error
$em = $this->getEntityManager()->createQueryBuilder();
$totalPostSubselect = $em->addSelect('c.id AS defaultContentItemId')
->from('AppBundle\Entity\ContentItem\ContentItem', 'c')
->andWhere('c.contentType = 1')
->andWhere('c.isTranslated = 0')
->andWhere('c.modifiedOn >= :timeperiod')
->andWhere('c.isActive = :status')
->setParameter('status', 1)
->setParameter('timeperiod', $timePeriodStart)->getDQL();
$em = $this->getEntityManager()->createQueryBuilder();
$defaultSubSelect = $em->addSelect(['*'])
->from('AppBundle\Entity\ContentItem\ContentItemLanguages', 'cl')
->andWhere("cl.defaultContentItemId IN ($totalPostSubselect)")
->andWhere('cl.languageId = :language')
->setParameter('subSelect', $totalPostSubselect)
->setParameter('language', $language)->getDQL();
$em = $this->getEntityManager()->createQueryBuilder();
$mainQuerySelect = $em->addSelect(["count(*) as total_count"])
->from("(".$defaultSubSelect.")", 'AS t')->getQuery();
return $mainQuerySelect->getResult();
Here is the error I got
[Doctrine\ORM\Query\QueryException] [Syntax Error] line 0, col 13: Error: Expected Literal, got '*'
[Doctrine\ORM\Query\QueryException]
SELECT count(*) as total_count FROM (SELECT * FROM
AppBundle\Entity\ContentItem\ContentItemLanguages cl
WHERE (
cl.defaultContentItemId IN (SELECT c.id AS defaultContentItemId
FROM AppBundle\Entity\ContentItem\ContentItem c WHERE
c.contentType = 1 AND
c.isTranslated = 0 AND
c.modifiedOn >= :timeperiod AND c.isActive = :status))
AND cl.languageId = :language) AS t
can anyone suggest, in exactly where I am doing wrong ?
In doctrine query language you can't SELECT *, instead you have to deal with objects. For your example something like:
$mainQuerySelect = $em->addSelect("count(t.id) as total_count")
->from("(".$defaultSubSelect.")", 'AS t')->getQuery();
I have stuck in this issue from the last two days.
Please help.
I want to assign the following query .
$qr = "( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = 'node' AND subconfigcode.deleted = '0') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = '1') AND (node.type IN ('offering')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0)";
to
one view's field value.
I have used the following code.
function general_views_query_alter(&$view, &$query) {
$qr = " ( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = 'node' AND subconfigcode.deleted = '0') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = '1') AND (node.type IN ('offering')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0)";
$query->add_where(1, "node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value", $qr);
}
But, it is returning the following where query.
where node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value = '( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = \'node\' AND subconfigcode.deleted = \'0\') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = \'1\') AND (node.type IN (\'offering\')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0) ')
I want to get query without the single quote assigned for sub query and remove extra slashed added to each value.
Please help.
Answer :
Direct query is not applicable for assignment purpose in the add_where condition.
For that, I have generated the query object using.
function general_views_query_alter(&$view, &$query) {
$subQuery = db_select('node', 'node'); $subQuery->leftJoin('field_data_field_select_parent_configuratio', 'select_parent_configuratio ', 'node.nid = select_parent_configuratio .entity_id');
$subQuery->condition('select_parent_configuratio.entity_type', 'node', '=');
$subQuery->condition('select_parent_configuratio.deleted', '0', '=');
$subQuery->leftJoin('node', 'select_parent_configuratio_node', 'select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid');
$subQuery->leftJoin('field_data_field_subconfigcode', 'subconfigcode', 'select_parent_configuratio_node.nid = subconfigcode.entity_id');
$subQuery->condition('subconfigcode.entity_type', 'node', '=');
$subQuery->condition('subconfigcode.deleted', '0', '=');
$subQuery->where("select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid");
$subQuery->condition('node.status', "1", '=');
$subQuery->condition('node.type', array('offering'), 'IN');
$subQuery->orderBy('configcode');
$subQuery->addField('subconfigcode', 'field_subconfigcode_value', 'configcode');
//$subQuery->range(0, 1);
$query->add_where($group,'node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value',$subQuery,'in');
}
Using this, I am able to generate it the query and assignment to value of the variable.
I'm trying to do DQL query, but having some troubles with it...
$user = $this->getUser();
$query = $em->createQuery(
'SELECT p
FROM AppBundle:User u
JOIN AppBundle:Follower f
JOIN AppBundle:Photo p
WHERE u.id = :id
AND f.follower_id = :id
AND p.user_id = f.user_id'
)->setParameter('id', $user->getId());
I am trying to get Photos (AppBundle:Photo) of those users, to whom the logged user is following.
Getting next error:
`[Syntax Error] line 0, col 128: Error: Expected =, <, <=, <>, >, >=, !=, got 'p'`
Whats wrong here with 'p' ?
I don't understand why follower are in your code, I don't see relation with photo...
After, I think you call Join but I don't see the relation you made with the photo...
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Photo p
JOIN p.user u
WHERE u.id = :id ')->setParameter('id', $user->getId());
Here is a part of the official doc :
Example:
Regular join of the address:
createQuery("SELECT u FROM User u JOIN u.address a
WHERE a.city = 'Berlin'"); $users = $query->getResult();
Fetch join of the address:
createQuery("SELECT u, a FROM User u JOIN
u.address a WHERE a.city = 'Berlin'"); $users = $query->getResult();
did yu try this?:
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Follower f
JOIN AppBundle:User u
WHERE u.id = :id
AND f.follower_id = :id
AND p.user_id = f.user_id' )->setParameter('id', $user->getId());
i have this ( huge ) sql query to convert into doctrine2 .
i already succeeded with createNativeQuery() but sometimes results don't match original one ( from mysql )
here is the query :
SELECT e1_.etapes AS etapes3, e1_.position AS position4, c0_.exercicesData_id AS Exercice_id, exo.titre as Exo, age.bornInf as ageInf, age.bornSup as ageSup, imc.bornInf as imcInf, imc.bornSup as imcSup, sexe.bornInf as sexeInf, sexe.bornSup as sexeSup
FROM ContrainteData c0_
LEFT JOIN ExerciceData e1_ ON c0_.exercicesData_id = e1_.id
LEFT JOIN Exercice exo ON e1_.exercice_id = exo.id
LEFT JOIN contraintedata_entrydata c3_ ON c0_.id = c3_.contraintedata_id
INNER JOIN (SELECT * FROM `entrydata` GROUP BY type_id, bornInf, bornSup) AS age ON age.type_id = 1
INNER JOIN (SELECT * FROM `entrydata` GROUP BY type_id, bornInf, bornSup) AS imc ON imc.type_id = 4
INNER JOIN (SELECT * FROM `entrydata` GROUP BY type_id, bornInf, bornSup) AS sexe ON sexe.type_id = 5
WHERE
( age.bornSup >= 30 and age.bornInf <= 30 ) and
( imc.bornSup >= 20 and imc.bornInf <= 20 ) and
( sexe.bornSup >= 1 and sexe.bornInf <= 1 ) and
e1_.entrainement_id = 6
GROUP BY etapes3, position4
ORDER BY etapes3 ASC, position4 ASC
with the queryBuilder , i have done the following :
$qb1 = $this->createQueryBuilder('c');
$qb2 = $this->createQueryBuilder('t');
$qb2->select( 't' )
->from ( 'bonk\AppBundle\Entity\EntryData', 'en' )
->groupBy( 'en.type')
->addGroupBy( 'en.bornInf' )
->addGroupBy( 'en.bornSup' );
$qb1->select( 'c' )
->leftJoin( 'c.exercicesData', 'e' )
->leftJoin( 'e.exercice' , 'ex' )
->join ( $qb2->getDql(), 'age', 'with', 'age.type = 1' )
->join ( $qb2->getQuery()->getDql(), 'imc', 'with', 'imc.type = 4' )
->join ( $qb2->getQuery()->getDql(), 'sexe', 'with', 'sexe.type = 5')
->where ( 'age.bornSup >= 30 and age.bornInf <= 30' )
->andWhere( 'imc.bornSup >= 20 and imc.bornInf <= 20' )
->andWhere( 'sexe.bornSup >= 1 and sexe.bornInf <= 1' )
->andWhere( 'e.entrainement = 6' )
->groupBy ( 'e.etapes', 'ASC' )
->addGroupBy( 'e.position', 'ASC' );
return $qb1->getQuery()->getArrayResult();
i have the following error :
"[Semantical Error] line 0, col 116 near 'SELECT t FROM': Error: Class 'SELECT' is not defined "
i finally succeed with a mysql view which is called by a fake entity in doctrine and DQL Query .
to create a view on sql , simply execute this line :
"create view VIEWNAME YOUQUERY"
in my example :
"create view entrydataView SELECT * FROM `entrydata` GROUP BY type_id, bornInf, bornSup"
This is my select and it works in workbench fine, but how did i write the INNER JOINS for doctrine?
$qb = $em->createQuery(
"SELECT oh.objektnummer, oh.idSubunternehmer, oh.datum, oh.typ, oh.outbox
FROM MBSAllgemeinBundle:ObjektHistory oh
INNER JOIN MBSAllgemeinBundle:Objekt o ON o.objektnummer = oh.objektnummer AND o.idSubunternehmer = oh.idSubunternehmer
INNER JOIN MBSAllgemeinBundle:Subunternehmer s ON s.subunternehmernummer = o.id_subunternehmer
INNER JOIN MBSAllgemeinBundle:SubunternehmerUser su ON su.id_subunternehmer = s.subunternehmernummer
WHERE su.idUser = 1"
);
Try this syntax in your repository :
return $this->createQueryBuilder('oh')
->select('partial oh.{id, objektnummer, idSubunternehmer, datum, typ, outbox}')
->innerJoin('MBSAllgemeinBundle:Objekt', 'o', 'ON', 'o.objektnummer = oh.objektnummer AND o.idSubunternehmer = oh.idSubunternehmer')
->addSelect('o')
// other INNER JOIN
// other INNER JOIN
->where('su.idUser = :id')
->setParameter('id', 1)
->getQuery()
->getResult()
;