Symfony 1.4 query builder single character explanation - php

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

Related

From SQL to DQL

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

Doctrine 1.2: Can't reproduce query with Doctrine_Query

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

Query builder in Doctrine_Table?

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:

Using subqueries with doctrine / Errors with aliases

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

Doctrine Join DQL

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

Categories