Re: DB_DataObject sorting links-results
| From: | Stijn de Reede | Date: | Tue, 14 Jan 2003 10:36:14 +0000 |
| Subject: | Re: DB_DataObject sorting links-results | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-3298@lists.php.net to get a copy of this message | ||
Alan Knowles wrote:
Wolfram (comments in-line)Possibly, this is the easiest way to do it.i got a question about DataObjects. after getting the linked tables, i would like to have the multiple results sorted by a column how do i do that? what i got now is without sorting$contact = new DataObjects_Contact2tree; $contact->contactTree_id = $session->temp->folder->id; $contact->find();personally I would just cop out and use the query(), method directly.. - it's in there because sometimes writing the SQL manually is easier to read and understand than autobuilding it...while( $contact->fetch() ) { // link the column 'contact_id' to the table 'contact' // which contains columns like 'name' 'surname' etc. $contact->getLinks(); print_r($contact); }
It may be worth looking at the joinAdd() stuff for this.. It's beta quality at the moment, and needs a bit more thought about how to deal with things like crosstable queries (that perhaps dont need joins...) I should write the docs for this, - and stick a warning in there.. 'using this method may make your code very difficult to understand - use with care'Ghehe, thanks for the coding compliment there Alan ;-). But I know what you mean, I had to think about it twice to understand if it would be possible to use joinAdd() for this, and even now I'm not sure. But isn't actually really simple, and implemented in the standard find() method? Couldn't you just do: $contact = new DataObjects_Contact2tree; $contact->contactTree_id = $session->temp->folder->id; $contact->orderBy('name'); $contact->find(); Since the find() method clearly uses all the _condition, _group_by, _order_by and _limit variables to build the query. Ow, damn, I think I missed something here, I now see that your datamodel is different from what I thought, so the code above will not work I think. I'll just leave it there, and continue with my other solution: If this doesn't work, let me write some code using joinAdd() of the top of my head: $contact = new DataObjects_Contact; $contactTree = new DataObjects_Contact2tree; $contactTree->contactTree_id = $session->temp->folder->id; $contact->joinAdd($contactTree); $contact->orderBy('name'); $contact->find(); For this to work, you would need to setup the database.links.ini file properly: [contact] tree_id = Contact2tree:contactTree_id or something like this... Let me know if it doesn't work or if you even didn't understand a word of what I just wrote. Stijn PS: the code above clearly isn't tested, since I've got an exam in 'Programming Languages' coming up tomorrow, and only bought the book yesterday.
It still needs more thoughts about how to deal with the return values, (I would like it to do something like getLinks(), and make subobjects with the data. otherwise, $data[$contact->_contact->name] = $contact; then array_sort on the result.. Regards Alani.e. i want to sort by 'contact.name', is that possible? if so, how? the query would be:SELECT * FROM contact2tree ct,contact c WHERE ct.contact_id=c.id ORDER BY c.namebut since i am reading the contact2tree table first, when using the DataObject i have no idea how to sort by the content of a table i will 'link'/'join' later