I have this code:
$query = Doctrine_Query::create()
->select('*')
->from('attendanceRecord a')
->where("employeeId IN (?)", implode(",", $employeeId));
$employeeId is an array of numbers
The sql output was:
Select * from attendanceRecord a where employeeId IN ('2,4,5')
but it have quote and was wrong I want this:
Select * from attendanceRecord a where employeeId IN (2,4,5)
How can I do it correctly in doctrine?
As simple as:
$query = Doctrine_Query::create()
->from('attendanceRecord a')
->whereIn('a.employeeId', $employeeId);
Please make sure you see the official documentation before asking a question.
Related
This SQL:
SELECT COUNT(*) AS total_see_all_video
from users u
WHERE 7 = (SELECT COUNT(*) FROM lecciones_users lu WHERE lu.uuid = u.uuid)
I tried this code but did not work:
$data = LeccionesUsers::select(
DB::raw('COUNT(*) AS total_ase_vis_videos'),
DB::raw('where 7 = (SELECT COUNT(*) FROM lecciones_users where leccion_users.uuid = users.uuid)')
)
->join('users', 'lecciones_users.uuid', '=', 'users.uuid')
->get();
Your issue is that you are not correctly forming your query. You can do ->toSql(); instead of ->get(); and you would see the final SQL (would definitely not be the same as the one you wrote first).
So, you should have this to have the same SQL:
$total_see_all_video = LeccionesUsers::whereRaw('7 = (SELECT COUNT(*) FROM lecciones_users where leccion_users.uuid = users.uuid)')
->count();
Please, try my query (and also run ->toSql() to see if you have a correct SQL).
I would still recommend to use relationships and it is very weird to do 7 = query.
You can use
DB::query()->fromSub('Raw sql query here..')
and then can perform actions on this.For the reference you can use the documentation fromSub
You can also look into this convert this where break this query to parts to be used accordingly. You can use this section for the reference purpose:
Laravel-Raw-Expressions
Hope this will help you with the result.
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 the following query:
$query = Doctrine_Query::create()
->from('Member m')
->where("m.type='1'")
->andWhere("m.name LIKE '%$term%'")
->orWhere("m.surname LIKE '%$term%'")
->orWhere("m.company LIKE '%$term%'")
->orderBy('id DESC');
But it's not working like I want — it is ignoring type column.
What I need is result set where m.type=1 and some of other fields in this query is LIKE 'something'.
$query = Doctrine_Query::create()
->from('Member m')
->where('m.type = 1 AND m.name LIKE ?', '%'.$term.'%')
->orWhere('m.type = 1 AND m.surname LIKE ?', '%'.$term.'%')
->orWhere('m.type = 1 AND m.company LIKE ?', '%'.$term.'%')
->orderBy('m.id DESC');
Your OR conditions didn't include the first condition. It's also recommended to use the ? for your variables to ensure Doctrine escapes them.
Tom's answer is correct, although I like to keep code repetition/duplication to a minimum.
This way should also work, while being a shorter, cleaner way to do it
$query = Doctrine_Query::create()
->from('Member m')
->where('m.type = ?', 1)
->andWhere('m.name LIKE :term OR m.surname LIKE :term OR m.company LIKE :term', array(':term' => '%' . $term . '%'))
->orderBy('m.id DESC');
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
I'm new to doctrine: I have a problem with the sorting of joined records.
A sample.
I've got an Article model which is associated with a Source model in 1 <-> n. The source model has a property called 'position' with an integer value.
Now I want to fetch an article with it's sources orderes by the position. My DQL looks like this:
$q = Doctrine_Query::create()
->select('a.title, s.content')
->from('Article a')
->leftJoin('a.Source s')
->where('a.id = ?')
->orderBy('s.position');
The result doesn't change if I edit the position.
Best regards,
Sebastian
Hmm... it should do. Maybe try either of these:
->orderBy('s.position DESC')
->orderBy('s.position ASC')
Yes, it looks OK. Try to generate the SQL from the DQL with getSqlQuery() and query the database with the result. If there is still the wrong output, it might by a problem with the data or more likely, with the DQL.
Perhaps you should include the column that you are using for the ordering (s.position), so try this:
$q = Doctrine_Query::create()
->select('a.title, s.content, s.position')
->from('Article a')
->leftJoin('a.Source s')
->where('a.id = ?')
->orderBy('s.position');