This works really well, it's easy to integrate too:
$this->Model->find('all', array(
'conditions' => ...,
'order' => 'FIELD(id, ' . implode(',', $topLinks).')'
));
Works like a dream. Is this standard SQL or a MySQL-specific thingy?
On Jun 9, 10:46 am, Abhimanyu Grover <[EMAIL PROTECTED]> wrote:
> Thanks, but it looks like a lot of work going on here, which will kill
> the code simplicity unfortunately. Let me try Tarique's suggestion,
> looks good by looking.
>
> On Jun 9, 2:31 pm, grigri <[EMAIL PROTECTED]> wrote:
>
> > These are just some ideas - not sure which one would be more
> > efficient:
>
> > $order = array();
> > foreach ($topLinks as $linkId) {
> > $order[] = '(Link.id='.$linkId.') DESC';}
>
> > ... = $this->Link->find('all', array(
> > 'conditions' => ...,
> > 'order' => $order
> > ));
>
> > [or]
>
> > $coef = 1;
> > $expr = array();
> > for($i=count($topLinks)-1; $i>=0; --$i) {
> > $expr[] = $coef . '* (Link.id='.$topLinks[$i].')';}
>
> > $expr = '('.implode('+', $expr) . ') AS position';
>
> > ... = $this->Link->find('all', array(
> > 'conditions' => ...,
> > 'fields' => 'Link.*, ' . $expr,
> > 'order' => 'position ASC'
> > ));
>
> > hth
> > grigri
>
> > On Jun 9, 8:15 am, Abhimanyu Grover <[EMAIL PROTECTED]> wrote:
>
> > > Hey,
>
> > > This is not Cake specific, but just posting here as I think this will
> > > be useful for others as well. Here's my code:
>
> > > $topLinks = $this->Vote->getTopRated();
> > > $this->set('links',
> > > $this->Link->findAll(array('Link.id'=>
> > > $topLinks, 'Link.status'=>UC_STATUS_APPROVED)));
>
> > > In first line, I'm trying to get Id's of all top rated links - which
> > > comes from a bit complex algorithm.
> > > In second line, I pass on those ids to findAll() which creates sql
> > > statement like this:
>
> > > WHERE `Link`.`id` IN (1, 5, 7, 4, 3)
>
> > > Now problem is, I need them in same order as I've sent them in IN(..)
> > > but mysql returns them ordering by id. i.e. 1, 3, 4, 5, 7
>
> > > I've tried appending Order by NULL, but it wouldn't work. Can anyone
> > > help?
>
> > > Thanks,
> > > Abhimanyu Grover
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---