I'm using Doctrine's querybuilder in Symfony2 to create a query to fetch entities.
I'm trying to get this result in MySQL :
SELECT u0_.id AS id0
FROM user u0_
LEFT JOIN rva_victims r3_ ON u0_.id = r3_.user_id
INNER JOIN rva r1_ ON r1_.id = r3_.rva_id or u0_.id = r1_.declarant_id
I've tried both ON and WITH conditionType in $qb->join(), but nothing work.
Any ideas to solve my problem?
The solution should be pretty straightforward. In your case it should look like this, assuming you're in the repository class:
$this
->createQueryBuilder('u')
->leftJoin(Victims::class, 'v', Query\Expr\Join::WITH, 'v.user = u.id')
->join(Rva::class, 'r', Query\Expr\Join::WITH, 'r.id = v.rva OR u.id = r.declarant');
This should work fine, I am just assuming the class names right now. Please also take in account that all the conditions are done on class attribute names (DQL), not column names.
You can do this by creating a query builder in entity repository
$qb = $this->createQueryBuilder('u')
->leftJoin('yourbundle:Entityname', 'ye', 'WITH', 'u0_.id = r3_.user_id')
->innerJoin('yourbuncle:entityname', 'yen', 'WITH', 'r1_.id = r3_.rva_id', 'OR', 'u0_.id = r1_.declarant_id');
Related
I need to convert native query to doctrine query.
Native query
select u.*, v.browser_id
from users u
left join visits v
on u.id = v.user_id
and v.id = (
select max(id) from visits v2
where v2.user_id = u.id
)
You can see right here that I need to select users with the latest browser they used to visit
So to convert this query to DQL I understand that I need to do something like this
$queryBuilder->leftJoin('u.visits', 'lastVis', Join::WITH,
// ????
);
but I don’t know what exactly. Perhaps someone will solve this quickly?
Thanks, seem I found the solution:)
$subQuery = $this->entityManager->createQueryBuilder()
->select('MAX(vis2.id)')
->from(Visit::class, 'vis2')
->where('vis2.user = u.id');
$this->entityManager->createQueryBuilder()
->select('u')
->from(User::class, 'u')
->leftJoin(
'u.visits', 'vis',
Join::WITH, sprintf('vis.id = (%s)', $subQuery->getDQL()),
);
I want to change this SQL query : (i'm using a bundle)
(Evenement is an entity and evenement_evenement is the result of self-referencing many to many of evenement)
SELECT *
FROM evenement e
natural JOIN evenement_evenement ee
WHERE e.id = ee.evenement_source
AND e.id = 3
Into DQL. For now I have this :
public function findAllEventAssociateByEvent($idEvent){
$qb = $this->createQueryBuilder('e');
$qb->add('select', 'e');
$qb->from('Bundle:Evenement', 'e2');
$qb->where('e = :evenement');
$qb->andWhere('e2 in e.evenements');
$qb->setParameter('evenement', $idEvent);
return $qb;
//select * from evenement e NATURAL join evenement_evenement ee where e.id = ee.evenement_source and e.id = $idEvent
}
And i have this :
$eventAssocies = $em->getRepository('Bundle:Evenement')->findAllEventAssociateByEvent($id)->getQuery()->getResult();
But it's not working, i have an error in my "andWhere", but I don't know why...
I think you misunderstood some stuff, I reckon you query should more look like this:
public function findAllEventAssociateByEvent($idEvent){
$qb = $this->createQueryBuilder('e')
->join('e.evenement_evenement', 'e2')
->where('e = :evenement')
->setParameter('evenement', $idEvent);
return $qb;
}
The join will do what you were trying to do with your AndWhere I reckon
You cannot use andWhere like that, you have to treat it like you treated your where clause. Declare a parameter, and later on, set it. From what I understand, you'd need to use a subquery for that e.evenements part. Whatever you expect to be in e.evenements part, extract it to a variable, e.g: $evenements and do the following:
$qb->andWhere('e2 IN (:evenements)')
->setParameter('evenements', $evenements, Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
Assuming $evenements is an array. If it's a string, you can explode() it for example.
If you are looking for a quick solution, try this:
$qb = $this->createQueryBuilder('e')
->select('e.id')
->where('e.id = :evenement')
->setParameter('evenement', $idEvent);
$evenementsIds = $qb->getQuery()->getResult(); // This will get you an array of ID's
$qb2 = $this->createQueryBuilder('e2')
->where('e2.id IN (:evenements)')
->setParameter('evenements', $evenementsIds, Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
$result = $qb2->getQuery()->getResult();
I'm using Symfony2 and i'm trying to get an array of languages not associated to a specified client.
I have a Client entity indicating the Client, ClientLanguage that has the following structure:
id_menu_language PRIMARY KEY
language the association with the Language entity
client the association with the Client entity
sequence tells the order the language should be shown (not used here)
and Language Entity.
To get an array of languages not associated to the client i want to proceed in the following way:
Get the languages that the client has already associated (and I'm getting the correct DQL in the $clientLanguagesDQL variable)
Retrieve a list of all the available languages
Exclude from that list all the languages already associated to the client (by using NOT IN (...) ).
This is the function I wrote to accomlish that:
<?php
namespace AppBundle\Repository;
use AppBundle\Entity\Client;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
class ClientRepository extends EntityRepository
{
/**
* #param $client Client to check
* #return array
*/
public function getLanguagesNotAssociatedToClient($client)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$clientLanguagesDQL = $qb
->select('lang')
->from('AppBundle:Language', 'lang')
->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
->where('client.idClient = :client_id')
->getQuery()
->getDQL();
$languages = $qb->select('language')
->from('AppBundle:Language', 'language')
->where($qb->expr()->notIn('language', $clientLanguagesDQL))
->setParameter('client_id', $client->getIdClient())
->getQuery()
->getResult();
return $languages;
}
}
However, when I run this, Symfony complains about a : [Semantical Error] line 0, col 293 near 'lang INNER JOIN': Error: 'lang' is already defined. It also tells me that there is a QueryException, and it shows me the following query:
SELECT language
FROM AppBundle:Language lang
INNER JOIN AppBundle:ClientLanguage languages_assoc
WITH languages_assoc.language = lang
INNER JOIN AppBundle:Client client
WITH languages_assoc.client = client, AppBundle:Language language WHERE language NOT IN(
SELECT lang
FROM AppBundle:Language lang
INNER JOIN AppBundle:ClientLanguage languages_assoc
WITH languages_assoc.language = lang
INNER JOIN AppBundle:Client client
WITH languages_assoc.client = client
WHERE client.idClient = :client_id
)
And this is definitely not what I want to do. Why there appeared to be two joins with AppBundle:ClientLanguage and AppBundle:Client? I use this association only in my first subquery.
If it can help, when I run this:
$clientLanguagesDQL = $qb
->select('lang')
->from('AppBundle:Language', 'lang')
->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
->where('client.idClient = :client_id')
->getQuery()
->getDQL();
This is the returned DQL stored in $clientLanguageDQL:
SELECT lang
FROM AppBundle:Language lang
INNER JOIN AppBundle:ClientLanguage languages_assoc
WITH languages_assoc.language = lang
INNER JOIN AppBundle:Client client
WITH languages_assoc.client = client
WHERE client.idClient = :client_id
What's wrong with this query?
My mistake was very stupid, all I had to do is creating a new query builder for each query instead of reusing the first one:
$qb1 = $this->getEntityManager()->createQueryBuilder();
$clientLanguagesDQL = $qb1
->select('lang')
->from('AppBundle:Language', 'lang')
->join('AppBundle:ClientLanguage', 'languages_assoc', Join::WITH, 'languages_assoc.language = lang')
->join('AppBundle:Client', 'client', Join::WITH, 'languages_assoc.client = client')
->where('client.idClient = :client_id')
->getQuery()
->getDQL();
$qb2 = $this->getEntityManager()->createQueryBuilder();
$languages = $qb2->select('language')
->from('AppBundle:Language', 'language')
->where($qb2->expr()->notIn('language', $clientLanguagesDQL))
->setParameter('client_id', $client->getIdClient())
->getQuery()
->getResult();
return $languages;
Im working on a Symfony project and I want to create Doctrine Query for this SQL.
USER table :
columns -
NICK_NAME
REVIEWS table:
columns -
USER_ID
, REVIEW,
CREATED_AT
Thank you
SELECT
`USER`.NICK_NAME,
REVIEWS.REVIEW,
REVIEWS.CREATED_AT
FROM
REVIEWS
INNER JOIN `USER` ON REVIEWS.USER_ID = `USER`.ID
WHERE
REVIEWS.MOVIE_ID = 625
GROUP BY
REVIEWS.USER_ID
I tried something like this
$q = Doctrine_Query::create()
->select("u.NICK_NAME,r.REVIEW,r.CREATED_AT")
->from('REVIEWS r')
->innerJoin('`USER` ON REVIEWS.USER_ID = `USER`.ID')
->where('REVIEWS.MOVIE_ID = 625')
->groupBy('REVIEWS.USER_ID');
and got this:
500 | Internal Server Error | Doctrine_Exception Couldn't find class `USER`
Without using complex DQL in Symfony you can use simple SQL.
Try this function in the controller where you want to run the DQL.
function getUserReviews($params) {
$query = "SELECT REVIEWS.REVIEW, `USER`.NICK_NAME, REVIEWS.CREATED_AT FROM REVIEWS INNER JOIN `USER` ON REVIEWS.USER_ID = `USER`.ID WHERE REVIEWS.MOVIE_ID ='".$params."'";
return Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($query);
}
You Should Specify Entity name not table name in DQL like this YourBundleName:EntityName
Use it like this:
$q = Doctrine_Query::create()
->select("u.NICK_NAME,r.REVIEW,r.CREATED_AT")
->from('REVIEWS r')
->innerJoin('YourBundleName:EntityName ON REVIEWS.USER_ID = `USER`.ID')
->where('REVIEWS.MOVIE_ID = 625')
->groupBy('REVIEWS.USER_ID');
Alternative Solution if above solution doesn’t work:
$q = Doctrine_Query::create()
->select("u.NICK_NAME,r.REVIEW,r.CREATED_AT")
->from('REVIEWS r')
->innerJoin('YourBundleName:EntityName', 'USER_ID')
->where('REVIEWS.MOVIE_ID = 625')
->groupBy('REVIEWS.USER_ID');
If that doesn't work, use Inner join in following manner:
->InnerJoin('YourBundleName:Entity', '<alias>', Expr\Join::ON, $qb->expr()->eq('IDENTITY(<alias.<clummn_name>)', '<comapring_column>'))
inner Join from Doctrine2 Documentation:
Example -
$qb->innerJoin('u.Group', 'g', Expr\Join::WITH, qb->expr()->eq('u.status_id', '?1'))
$qb->innerJoin('u.Group', 'g', 'WITH', 'u.status = ?1')
$qb->innerJoin('u.Group', 'g', 'WITH', 'u.status = ?1', 'g.id')
innerJoin Method Prototype:
innerJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null);
Find More Here
Hope this will help you.
I try to get this mysql query to work with Yii model but i can't.
SELECT COUNT( qhc.countries_id) AS counter, q.question, co.name
FROM questions AS q , countries as co, questions_has_countries AS qhc
WHERE qhc.questions_id = q.id
AND co.id = qhc.countries_id
GROUP BY question
HAVING counter = 2
So far i have this, but somehow thou it seems ok, it doesnt work :
$criteria = new CDbCriteria();
$criteria->select = 'question, COUNT(countries_id) as counter';
$criteria->with = array('countries', 'categories');
$criteria->addInCondition('countries.id' , $_POST['Questions']['countries']);
$criteria->group = 'question';
$criteria->having = ('counter = 1');
$model = Questions::model()->findAll($criteria)
Pls help, I'am pretty new to Yii framework.
Thanks.
Sql from the log :
SELECT `t`.`question` AS `t0_c1`,
COUNT(countries_id) as counter, `t`.`id` AS `t0_c0`, `countries`.`id` AS
`t1_c0`, `countries`.`name` AS `t1_c1`, `categories`.`id` AS `t2_c0`,
`categories`.`name` AS `t2_c1` FROM `questions` `t` LEFT OUTER JOIN
`questions_has_countries` `countries_countries` ON
(`t`.`id`=`countries_countries`.`questions_id`) LEFT OUTER JOIN `countries`
`countries` ON (`countries`.`id`=`countries_countries`.`countries_id`)
LEFT OUTER JOIN `questions_has_categories` `categories_categories` ON
(`t`.`id`=`categories_categories`.`questions_id`) LEFT OUTER JOIN
`categories` `categories` ON
(`categories`.`id`=`categories_categories`.`categories_id`) WHERE
(countries.id=:ycp0) GROUP BY question HAVING (counter = 2). Bound with
:ycp0='1'
You have done most of work. Now you need to call the $criteria into model. Just like this
$rows = MODEL ::model()->findAll($criteria);
Where MODEL is model class of table which you want to apply criteria on.
To learn more about this you can follow this CActiveRecord Class.
Try to set together in CDbCriteria
...
$criteria->together = true;
$model = Question::model()->findAll($criteria);
when you use "as counter", your model must have a property named "counter" or it will not load it into your model.
if you don't have a property named "counter", try using another one of your models property that you are not selecting right now : "as someColumn"
and use condition or addCondition or .. instead of having
cheers