PHP: Doctrine: order joined records - php

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

Related

Order By on Joined Entity - Doctrine Query Builder

I have the following query:
$qb = $this->_em->createQueryBuilder();
$qb->select('a')
->from('CatalogueBundle\Entity\Avantage','a')
->leftJoin('a.avantagegrilles', 'g')
...
->orderBy('a.avantageOrdre', 'ASC');
The result of the query is as follows:
An avantage can contain several avantagegrilles. Now there is a variable in entity avantagegrille called avantagegrilleMini and I want to do an order by asc on it; that is, when an avantage is returned, its avantagegrilles are ordered according to their avantagegrilleMini.
I added ->orderBy('g.avantagegrilleMini', 'ASC') to the query but it didn't have any effect. Is what I want to implement possible in Doctrine?
Let's try this line: ->orderBy('g.avantageOrdre', 'ASC');

CodeIgniter Subquery in Query Builder

Is there a way or can someone translate this to code igniter query builder.
SELECT result.id, result.name, result.added
FROM (SELECT tbl.id, tbl.name, tbl.date_added
FROM table tbl
GROUP BY tbl.id) result
GROUP BY result.date_added
I already have my research(link below) but can't find anything like this(query above).
https://www.codeigniter.com/userguide3/database/query_builder.html
https://arjunphp.com/how-to-write-subqueries-in-codeigniter-active-record/
And yes, this can be done using Stored Procedure but I have another reason why I need to implement this as query builder.
try this one.
// Sub Query
$this->db->select('result.id, result.name, result.added')->from('table tbl');
$subQuery = $this->db->get_compiled_select();
// Main Query
$this->db->select('result.id, result.name, result.added')
->from('table tbl')
->where("id IN ($subQuery)", NULL, FALSE)
->get()
->result();

propel - JoinWith and use Query

What is a diference beetween JoinWith, and useTablenameQuery ? How its work in propel, why JoinWith is faster than useTable_nameQuery ? How work formatter ? Thanks for help :
)
Main difference is when you have filter specifically for the on the right like TableB=?
joinWith: Default Join condition as defined in the schema
$rows = TableAQuery::create()
->joinWith('TableB')
->find();
useTableBQuery Use a filter for the table right
$rows = TableAQuery::create()
->useTableBQuery()
->filterByColumnName($value) // A column in TableB
->endUse()
->filterByColumnName($value2) // A column in TableA
->find();
Notice the second ->filterByColumnA() is outside the ->useTablBQuery, to illustrate that you can have filter on both tables

How to make the active record many to many relationship easier to develop in codeigniter?

I have the table like this:
[Note]
[id]
[Note_Tag_list]
[Note_id]
[Tag_id]
[Tag]
[id]
Is there any active record to let me ONLY join these three tables?
Because every time I based on the note id to loop back the note_tag_list record, and use the note tag list record, make me feel really, really annoying.
The flow I use:
//loop the note the user request
//get the note by id
//use the note id to get the note tag list, and return the array of note tag list
//loop the array of note tag list and use the tag id to quest the tag
How do I do make this easier?
Maybe you could try datamapper :)
I know this is an old question, but anyways i will answer it for anyone who faces the same problem in future.
you need to use a JOIN query to connect multiple tables using primary and foreign keys relation.
Code to get all records
$q = $this->db
->select()
->from('Note AS n')
->join('Note_Tag_list AS n_t_l', 'n_t_l.Note_id = n.id', 'left')
->join('Tag AS t', 't.id = n_t_l.Tag_id', 'left')
->get();
Code to get only specific records (by using WHERE clause)
$where = array(
'n.id' => **provide note id here**
);
$q = $this->db
->select()
->from('Note AS n')
->join('Note_Tag_list AS n_t_l', 'n_t_l.Note_id = n.id', 'left')
->join('Tag AS t', 't.id = n_t_l.Tag_id', 'left')
->where($where)
->get();
For more information read the codeigniter's docs.
Codeigniter Query Builder

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

Categories