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条)

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?