dpi9530 2014-10-23 20:54
浏览 97
已采纳

使用Containable在CakePHP中进行复杂排序

I`m having a problem with the Containable behaviour.

I would like to know if there is any way to access the contained model attributes for operations like ordering.

For example, I have a model B which belongs to a Model A. I need to order objects of B using an attribute (integer) of A. It would be something like:

'contain' => array(
            'A' => array(
                'B' => array(
                    'order' => 'A.integer_attribute'
                )
            )
        )

I know that there are easier ways to do this without Containable, but for reasons which are not worth being detailed here, I need to use it. This is an abstract example, in truth model A belongs to other models and this is just a small part of a deep containable tree.

I'd be very glad with any help!

EDIT

OK, I'll try my best to describe the situation without being unclear:

I have 4 models: User, Category, Field and UserField, and their relationships are as follows:

Category hasMany User
Category hasMany Field
User hasMany UserField
Field hasMany UserField

The opposite of these relations are all belongsTo. The purpose here is that the user belongs to a category, which has many fields that he needs to fill with his information (city, state etc). The information he fills is stored in the UserField table, as each information needs to have its field and the user who provided it.

That said, I need to build a screen which displays, for each category, a list of users and their information. So, I retrieve all the categories and its fields, so I can build a table for each category. Each field has an attribute "no_order", which is the number that indicates the order in which the field appears.

At the same time, I need all of each category's users to display them correctly in the tables. Finally, and there's the problem, I need to have UserField objects ordered by the "no_order" of their respective fields, for each user. So I ended up with something like:

$categories = $this->Category->find('all', array(
        'order' => 'Category.name',
        'contain' => array(
            'Field',
            'User' => array(
                'UserField' => array(
                    'order' => 'Field.no_order'
                )
            )
        )
    ));

But this doesn't work, since UserField cannot reach its respective Field's no_order from there.

I apologize if this wasn't clear enough, but for anyone who would spend a little while reading this, I would be VERY grateful for your help!

  • 写回答

2条回答 默认 最新

  • dongru2019 2014-10-24 15:58
    关注

    I finally came up with a solution. I don't know how efficient it is, but there it goes:

    'contain' => array(
                'Field',
                'User' => array(
                    'User.privilege = "Solicitante"'
                    'UserField' => array(
                        'order' => '(SELECT f.no_order FROM fields AS f WHERE UserField.field_id = f.id LIMIT 1)'
                    )
                )
            )
    

    Having the raw query solved my problem. Hope it helps anybody who comes across a similar problem!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。