I's like to do a join between 2 tables on a specific ID. At the moment, I have this DQL:
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_Item i ON e.itemId = i.itemId')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
The result is empty, although my eventId has a value ... Can you help me please? I there somewhere a tutorial on DQL-joins? I don't get it right with the help of the Doctrine documentation.
Thanks!
PS I have doctrine working in combination with Zend Framework.
you need add a relation to the model and join the tables using the relation
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_EventItem.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
you should change the name in the left join from Model_EventItem to e
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e')
->leftJoin('Model_EventItem.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
$q = Doctrine_Query::create()
->select('e.*, i.itemName, i.itemtypeId')
->from('Model_EventItem e, e.Model_Item i')
->where('e.eventitemId = ?', $event->eventId)
->orderBy('i.itemName ASC');
Related
How I can write this sql to doctrine query language (DQL)?
SELECT *
FROM service
WHERE service.id NOT IN (SELECT id_service FROM reclamation)
Thanks.
try use not exists:
SELECT *
FROM service
WHERE NOT Exists (SELECT 1 FROM reclamation WHERE id_service = service.id)
Try to do this,
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT s
FROM YourBundle:Service s
WHERE s.id NOT IN
( SELECT s.idService FROM YourBundle:Reclamation r )'
)->getResult();
If that deserves, you can use the old school way! native-sql-with-docrtine
see the link http://doctrine.readthedocs.org/en/latest/en/manual/dql-doctrine-query-language.html#subqueries
$q = Doctrine_Query::create()
->select('s')
->from('Service s')
->where('s.id NOT IN (SELECT id_service FROM reclamation)
->getQuery()
->getResult();
i am just doing jobeet tutorial but I bit confused by single character used in query builder :
$q = Doctrine_Query::create()
->select(' j .*') //what is j ?
->from('JobeetJob j')
->leftJoin('j.JobeetCategory c') //what is c ?
->leftJoin('c.JobeetAffiliates a'); //what is a ?
->where('a.id = ?', $this->getId());
What are the meaning of j, c, a in that query builder ?
j is your alias for JobeetJob table defined in a line:
->from('JobeetJob j')
the same goes for c being an alias for JobeetCategory table, and a for JobeetAffiliates. These aliases can be whatever you like, so your query may also look like:
$q = Doctrine_Query::create()
->select(' qwerty .*')
->from('JobeetJob qwerty')
->leftJoin('qwerty.JobeetCategory asdf')
->leftJoin('asdf.JobeetAffiliates zxcv');
->where('zxcv.id = ?', $this->getId());
I have this SQL query:
select *
from tblapplicant AS a
WHERE a.napplicantid
not in (
select napplicantid
from tblcontract
where dstart BETWEEN '2011-10-27' AND '2012-01-26'
OR dend BETWEEN '2011-10-27' AND '2012-01-26')
And I want to build this query in Doctrine 1.2:
$Query = Doctrine_Query::create()
->select('a')
->from('tblapplicant a')
->innerJoin('a.tblintermediair i')
->where('i.nintermediairid = ? ', $intermediairid)
->addWhere('a.napplicantid NOT IN (select c.napplicantid from tblcontract c WHERE c.dstart BETWEEN ? AND ? OR c.dend BETWEEN ? AND ?)', array($this->tbljobavailable->getFirst()->dday, $this->tbljobavailable->getLast()->dday, $this->tbljobavailable->getFirst()->dday, $this->tbljobavailable->getLast()->dday));
but somehow it keeps complaining:
Couldn't find class c
Any ideas?
Just had this issue a few days ago.
One of the possible solutions is adding a one to one relation for a tblapplicant table itself. I did not like this one, so just created additional query to get ids for exclusion. In your case that would be like this:
$notIn = Doctrine_Query::create()->(put your subselect query here)->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
$Query = Doctrine_Query::create()
->select('a')
->from('tblapplicant a')
->innerJoin('a.tblintermediair i')
->where('i.nintermediairid = ? ', $intermediairid)
->whereNotIn('a.napplicantid', $notIn);
I have a previously writen query in other Doctrine project using Query Builder.
$qb->select('c', 'm')
->from('Chapter c')
->leftJoin('c.Book m')
->orderBy('c.chapterno DESC')
->addOrderBy('m.id ASC')
->groupBy('m.id')
->where('c.chapterno = (SELECT MAX(v.chapterno) FROM Chapter v WHERE v.mid = m.id)')
->limit($max);
How can I replicate this in my inherited class from Doctrine_table in symfony?
I am using symfony 1.4
In your ChapterTable class, something like the following:
public function getChapters($max)
{
$qry = self::getInstance()
->createQuery("c")
->leftJoin('c.Book m')
->orderBy('c.chapterno DESC')
->addOrderBy('m.id ASC')
->groupBy('m.id')
->where('c.chapterno = (SELECT MAX(v.chapterno) FROM Chapter v WHERE v.mid = m.id)')
->limit($max);
return $qry->execute();
}
Then you can call this with eg:
$myMaxValue = 25;
$results = Doctrine::getTable("Chapter")->getChapters($myMaxValue);
Here is a nice solution to organize your queries:
I'm trying to make a simple query with a subquery in a orWhere clause (with Doctrine).
As always, Doctrine tries to rename every aliases and completely destroys the queries...
Here's an example:
$q = Doctrine_Query::create()
->from('Actualite a')
->where('a.categorie_id =?', $id)
->orWhere('a.categorie_id IN (select id from cat.categorie as cat where cat.categorie_id =?)', $id)
->execute();
Which in MySQL would make something like:
SELECT *
FROM actualite a
WHERE a.categorie_id = 1 OR a.categorie_id IN (SELECT cat.id FROM categorie cat WHERE cat.categorie_id = 1);
Everything is right about it, but then again Doctrine destroys it:
Couldn't find class cat
Every time I try to do something a little complex with Doctrine, I have errors with aliases. Any advice or ideas about how to fix this?
Thanks!
The SQL example you've provided is fine but the corresponding Doctrine syntax has a couple of errors. Here's a clean version:
$q = Doctrine_Query::create()
->select('a.*')
->from('Actualite a')
->where('a.categorie_id = ?', $id)
->orWhere('a.categorie_id IN (SELECT cat.id FROM Categorie cat WHERE cat.categorie_id = ?)', $id)
->execute();
You should use createSubquery() to explicitely tell doctrine about your nested subquery. So your query should look something like this:
$q = Doctrine_Query::create()
->select('a.*')
->from('Actualite a')
->where('a.categorie_id = ?', $id)
;
$subquery = $q->createSubquery()
->select("cat.id")
->from("Categorie cat")
->where("cat.categorie_id = ?", $id)
;
$q->orWhere('a.categorie_id IN ('.$subquery->getDql().')')->execute();
Another example can be found here:
http://www.philipphoffmann.de/2012/08/taming-doctrine-subqueries/
I think you query should be like this add the select and remove as
$q = Doctrine_Query::create()
->select('a.id')
->from('Actualite a')
->where('a.categorie_id =?', $id)
->orWhere('a.categorie_id IN (select id from cat.categorie cat where cat.categorie_id =?)', $id)
->execute();
Try this may help you.
Thanks