views:

32

answers:

1

Hello,

I have an element that grabs data from mysql. Here is my working code:

$this->requestAction('posts/index/sort:id/direction:desc');

I want to grab only posts between id 1 and 6. How can I run that query via requestAction? Some of scripts that I have tried are below. None is working:

$this->requestAction('posts/index/sort:id/direction:desc', array('id between ? and ?' => array('1,6')));

or

$this->requestAction('posts/index/sort:id/between:1,6/direction:desc');

You may see my project at http://bake.yemeklog.com/ I want this code for third column (Last 30 days faves)

A: 

If I was going to be calling it via requestAction (!) then I would write a custom method in my controller, then I'd probably pass the two id's into that method as parameters.

Then you can process the parameters and formulate your query.

$this->Model->find('all', 'conditions' => array('id'=>array(1,2,3,4,5,6)));

Not ideal by any means, but I'm not quite sure how else I would approach this kind of problem.

If it's static id's then perhaps

$this->Model->find('all', null, null, 'order' => 'id ASC LIMIT 0,6');

** Now I'm not sure if that would work as I haven't tried it, but I often hacked little things like this into Cake with some success. So perhaps give it a try, be sure to set debug = 2 so you can see the query, or grab DebugKit from OhLo

PS, Dont forget if you write requestAction methods, to check that $this->params['requested'] is true, so you know it come from a requestAction

DavidYell
Hi David,Thanks for reply. I dont know will it be suitable for me. Because I have three columns in my layout. Every column has different sql query results. Current script works OK expect sql bewtween. Here is my project. It may be helpful to see what I am talking about. http://bake.yemeklog.com/
Ahmet Kemal
In that case I would use an element for each column, with a specific find for each, then pass in that data into the element as parameter :)
DavidYell
Oh then I will try that. But I am really new at cake so I never want to change any structure that I get used to :) Hope I can get that
Ahmet Kemal