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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵