Cakephp 2.0:如何为通过 find() 递归检索的数据提供 ORDER BY 指令?
我正在尝试建立一个具有大量 hasMany/hasOne 关系的模型,并且我希望将拥有的模型按字母顺序显示在“添加”内的下拉菜单中看法。我不确定如何向递归检索的模型提供 ORER BY 指令。这就是我尝试过的:
$services = $this->service->find('all', array(
'order'=>array(
'Service.start_year DESC',
'Servicecat.title DESC'
),
'recursive'=>0
) );
但这当然不会产生任何改变。我怀疑这不是很复杂,我只是在食谱/API 中找不到解决方案。有想法吗?
I am trying to set up a model with a lot of hasMany/hasOne relationships, and I'd like for the owned models to be presented, alphabetically, in a drop-down menu within the "Add" view. I am not sure how to provide ORER BY instructions to models that are retrieved recursively. This is what I've tried:
$services = $this->service->find('all', array(
'order'=>array(
'Service.start_year DESC',
'Servicecat.title DESC'
),
'recursive'=>0
) );
But of course this yields no change. I have a suspicion it's not very complicated, I just can't find a solution in the cookbook/api. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过几种方式做到这一点。我更喜欢对模型使用可包含行为,然后它允许您精细控制模型的检索方式。您还可以在模型的关系定义中设置顺序。
Model Way
Containable Way
在模型中,将它们设置为使用可包含行为:
然后,当您从控制器中查找时,您可以明确说明链接了哪些模型。您可以使用多维数组来确定递归深度。
You could do this a few ways. I prefer to use the containable behavior for models and then it allows you to finely control how the models are retrieved. You could also setup order in your model's relationship definition.
Model Way
Containable Way
In your models, set them to use the containable behavior:
Then, when you find from your controller, you can explicitly state which models are linked. You can use multidimensional arrays to determine the depth of recursion.