CakePHP中显示来自阵列的自定义字段 [英] CakePHP display custom fields from array

查看:68
本文介绍了CakePHP中显示来自阵列的自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道冠军不会形容我最想达到的目标,但我想不出别的了。

I know the title doesn't describe the best what I want to achieve, but I couldn't think of anything else.

让我们说我有以下数组:

Let's say I have the following array:

[Sender] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [username] => admin
                        [PrivateMessagesUser] => Array
                            (
                                [id] => 1
                                [private_message_id] => 1
                                [sender_id] => 1
                                [recipient_id] => 3
                                [sender_status] => 1
                                [recipient_status] => 0
                                [random_key] => 
                            )

                    )

                [1] => Array
                    (
                        [id] => 1
                        [username] => admin
                        [PrivateMessagesUser] => Array
                            (
                                [id] => 3
                                [private_message_id] => 1
                                [sender_id] => 1
                                [recipient_id] => 2
                                [sender_status] => 1
                                [recipient_status] => 0
                                [random_key] => 
                            )

                    )

            )

我现在用的是ContainableBehaviour切断额外数据。
我的问题是:我怎么筛选出发件人秒的关键是present在 PrivateMessagesUser 后?说我只需要在发件人,其中 PrivateMessagesUser.recipient_id = 3
我试着用

I am using the ContainableBehaviour to cut off extra data. My problem is: how do I filter out the Senders after a key which is present in PrivateMessagesUser? Say I need only the Sender where PrivateMessagesUser.recipient_id = 3. I tried with

'Sender' => array( 
 'PrivateMessagesUser' => array(

 )
),

但它没有工作。看来我必须克服的[0],[1] ...键不知怎么,我不知道怎么办。

but it didn't work. Looks like I have to get over the [0], [1]... keys somehow and I don't know how.

任何帮助将是AP preciated!

Any help would be appreciated!

修改

下面就是我如何获得该数组上面贴:

Here's how I obtain the array posted above:

$this->paginate = array(
        'conditions' => array(
            'PrivateMessage.id' => $this->__getUserPrivateMessagesInbox()
        ),
        'contain' => array(
            'Sender' => array(
                'fields' => array('id', 'username'),
                'PrivateMessagesUser' => array(
                    'fields' => array('PrivateMessagesUser.id')
                    //'conditions' => array('PrivateMessagesUser.recipient_id' => $this->Auth->user('id'))
                )
            ),
            'Recipient' => array(
                'fields' => array('id', 'username'),
                'conditions' => array('Recipient.id' => $this->Auth->user('id'))
            )
        )
    );

我如何才能达到预期的效果编辑这些行?

How can I edit these lines in order to achieve the desired result?

编辑2

我打电话从PrivateMessagesController的PAGINATE()。

I am calling the paginate() from the PrivateMessagesController.

关联:

用户模型:

var $hasAndBelongsToMany = array(
    'PrivateMessageSent' => array(
        'className' => 'PrivateMessage',
        'joinTable' => 'private_messages_users',
        'foreignKey' => 'sender_id',
        'associationForeignKey' => 'private_message_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ),
    'PrivateMessageReceived' => array(
        'className' => 'PrivateMessage',
        'joinTable' => 'private_messages_users',
        'foreignKey' => 'recipient_id',
        'associationForeignKey' => 'private_message_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )
);

PrivateMessage型号:

PrivateMessage Model:

var $hasAndBelongsToMany = array(
    'Sender' => array(
        'className' => 'User',
        'joinTable' => 'private_messages_users',
        'foreignKey' => 'private_message_id',
        'associationForeignKey' => 'sender_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ),
    'Recipient' => array(
        'className' => 'User',
        'joinTable' => 'private_messages_users',
        'foreignKey' => 'private_message_id',
        'associationForeignKey' => 'recipient_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ),
);

PrivateMessagesUser型号:

PrivateMessagesUser Model:

var $belongsTo = array(
    'PrivateMessage' => array(
        'className' => 'PrivateMessage',
        'foreignKey' => 'private_message_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Sender' => array(
        'className' => 'User',
        'foreignKey' => 'sender_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Recipient' => array(
        'className' => 'User',
        'foreignKey' => 'recipient_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

和数据库表如下:


  • 用户 ID 用户名 ..

  • private_messages ID 主题 ..

  • private_messages_users ID private_message_id SENDER_ID recipient_id 状态 ...

  • users: id, username, ...
  • private_messages: id, subject, ...
  • private_messages_users: id, private_message_id, sender_id, recipient_id, status, ...

推荐答案

有三种方式来实现这一目标。

There are three ways to achieve this.

看着你返回的数据数组,我猜 PrivateMessagesUser 的belongsTo 发件人

Looking at your returned data array, I'm guessing PrivateMessagesUser belongsTo Sender.

1。查询PrivateMessagesUser模型

有时候,它需要的是查询正确的模式。您可以直接申请的条件要在你的应用限制模式。

Sometimes, all it takes is querying the right model. You can apply the conditions directly to the model you want to apply your restrictions on.

$this->Sender->PrivateMessagesUser->find('all', array(
    'conditions' => array('PrivateMessagesUser.recipient_id' => 3),
    'contain' => array('Sender')
));

2。申请条件,相关的模型

这不工作的时候,因为CakePHP的SQL生成并不总是你想要的方式。但在简单的情况下像的hasMany关系,你可以使用这个:

This doesn't work all the time, because CakePHP's SQL generation doesn't always work the way you want it to. But in simple cases like a hasMany relationship, you can use this:

$this->Sender->find('all', array(
    'conditions' => array('PrivateMessagesUser.recipient_id' => 3),
    'contain' => array('PrivateMessagesUser')
));

此外,这将蛋糕是否执行JOIN为容纳的参数和使用权的条件下才有效。

Again, this will work only if Cake executes a JOIN for the Containable parameters AND uses the conditions right.

3。使用加入

这方法是万无一失的,但因为它有点乱的高度可定制的。

This method is foolproof but a little messy since it's highly customizable.

$this->Sender->find('all', array(
    'joins' => array(
        array(
            'table' => 'private_messages_users',
            'alias' => 'PrivateMessagesUser',
            'type' => 'inner',
            'conditions' => array(
                'PrivateMessagesUser.sender_id = Sender.id',
                'PrivateMessagesUser.recipient_id' => 3
            )
        )
    )
));


我对你的最后一条建议是这样code放入模型。按照肥肉模型,瘦控制器只要有可能。


My last piece of advice for you is to place this code into the Model. Follow the principle of "fat models, skinny controllers" whenever possible.

发件人模型:

function fetchByRecipientId($recipientId) {
    ...
}

祝你好运!

编辑:为了使这项工作分页,按照上面的方法,但使用以下格式:

In order to make this work for pagination, follow the methods above but use the following format:

$this->paginate = array(
    'PrivateMessagesUser' => array(
        'conditions' => array('PrivateMessagesUser.recipient_id' => 3),
        'contain' => array('Sender'),
        // 'order' => ... other keys if you wish
    )
);
$this->set('senders', $this->paginate());

这篇关于CakePHP中显示来自阵列的自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆