dpi9530 2014-10-23 20:54
浏览 97


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!


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(
            '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(
                '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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了
  • ¥15 如何删除这个虚拟音频