Cakephp find('all')只返回一行

时间:2014-08-29 13:47:04

标签: cakephp model cakephp-2.5

在我的应用程序中,我有两个关联的模型

通知 belongsTo 个人资料

这是我的Notificaiton模式

class Notification extends AppModel {

    public $name = 'Notification';

public $belongsTo = array('Profile');

    public function getnotification($id = NULL){
    $result = $this->find('all',array(
        'conditions' => array(
        'Notification.receiver_id' => $id
        )
    ));
    return $result;
    }
}

输出

Array
(
    [0] => Array
        (
            [Notification] => Array
                (
                    [id] => 1
                    [profile_id] => 8
                    [receiver_id] => 1
                    [notification_text] => tester Sent you message on project Test
                    [notification_descriptions] => {"projectid":"2"}
                    [is_active] => 1
                    [created_on] => 2014-08-29 18:50:38
                    [modified_on] => 2014-08-29 18:50:38
                )

            [Profile] => Array
                (
                    [id] => 8
                    [user_id] => 8
                    [u_id] => 63c0cd43
                    [profile_firstname] => tester
                    [profile_lastname] => seller
                    [profile_gender] => 1
                    [profile_dob] => 1999-08-13
                    [profile_paypalid] => testseller2@yopmail.com
                    [profile_company] => 
                    [profile_occupation] => Web Developer
                    [profile_address] => 
                    [profile_city] => 
                    [profile_state] => 
                    [country_id] => 106
                    [profile_postalcode] => 
                    [currency_id] => 150
                    [timezone_id] => 93
                    [profile_avatar] => 
                    [profile_role] => 2
                    [profile_status] => 1
                    [about_me] => "But I must explain to you how all this mistaken idea"
                    [profile_title] => 
                    [rate_per_hour] => 
                    [language_id] => 38
                    [visibility] => 
                    [profile_rating] => 100
                    [is_active] => 1
                    [last_modified] => 2014-08-27 11:34:56
                    [rating] => 100
                )

        )

)

但即使有两行或更多行

,它总是从表中返回第一行

预期输出

Array
(
    [0] => Array
        (
            [Notification] => Array
                (
                    [id] => 1
                    [profile_id] => 8
                    [receiver_id] => 1
                    [notification_text] => tester Sent you message on project Test
                    [notification_descriptions] => {"projectid":"2"}
                    [is_active] => 1
                    [created_on] => 2014-08-29 18:50:38
                    [modified_on] => 2014-08-29 18:50:38
                )

            [Profile] => Array
                (
                    [id] => 8
                    [user_id] => 8
                    [u_id] => 63c0cd43
                    [profile_firstname] => tester
                    [profile_lastname] => seller
                    [profile_gender] => 1
                    [profile_dob] => 1999-08-13
                    [profile_paypalid] => testseller2@yopmail.com
                    [profile_company] => 
                    [profile_occupation] => Web Developer
                    [profile_address] => 
                    [profile_city] => 
                    [profile_state] => 
                    [country_id] => 106
                    [profile_postalcode] =>                     
                    [currency_id] => 150
                    [timezone_id] => 93
                    [profile_avatar] => 
                    [profile_role] => 2
                    [profile_status] => 1
                    [about_me] => "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was"
                    [profile_title] => 
                    [rate_per_hour] => 
                    [language_id] => 38
                    [visibility] => 
                    [profile_rating] => 100
                    [is_active] => 1
                    [last_modified] => 2014-08-27 11:34:56
                    [rating] => 100
                )

        )

    [1] => Array
        (
            [Notification] => Array
                (
                    [id] => 2
                    [profile_id] => 7
                    [receiver_id] => 1
                    [notification_text] => tester Sent you message on project Test
                    [notification_descriptions] => {"projectid":"2"}
                    [is_active] => 1
                    [created_on] => 2014-08-29 18:50:38
                    [modified_on] => 2014-08-29 18:50:38
                )

            [Profile] => Array
                (
                    [id] => 7
                    [user_id] => 7
                    [u_id] => 63c0cd458
                    [profile_firstname] => teste
                    [profile_lastname] => person
                    [profile_gender] => 1
                    [profile_dob] => 1999-08-13
                    [profile_paypalid] => testseller2@yopmail.com
                    [profile_company] => 
                    [profile_occupation] => Web Developer
                    [profile_address] => 
                    [profile_city] => 
                    [profile_state] => 
                    [country_id] => 106
                    [profile_postalcode] => 
                    [currency_id] => 150
                    [timezone_id] => 93
                    [profile_avatar] => 
                    [profile_role] => 2
                    [profile_status] => 1
                    [about_me] => "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a c
                    [profile_title] => 
                    [rate_per_hour] => 
                    [language_id] => 38
                    [visibility] => 
                    [profile_rating] => 100
                    [is_active] => 1
                    [last_modified] => 2014-08-27 11:34:56
                    [rating] => 100
                )

        )

)

虽然$this->element('sql_dump');显示3行因查询而受到影响,但是当我打印结果时,它只显示一个结果,即使是count& sizeof显示1

profile_id视为sender_id

任何人都可以告诉我在哪里以及我做错了什么?

3 个答案:

答案 0 :(得分:1)

您的功能getnotification只是寻找单个用户的通知。多数民众赞成在这里发生:

'conditions' => array(
  'Notification.receiver_id' => $id
)

您的预期输出显示不同的用户(user_id 7和8)。

你到底想要得到什么?

  • 一位用户的所有通知(这就是您拥有的)
  • 所有用户的所有通知
  • 首次通知的所有用户

//编辑1:出错。认为您可能需要Containable行为?没有测试过,这就是想法:

在您的模型中:public $actsAs = array('Containable'); 在您的通知模型中:

public function getnotification($receiver_id = NULL){
  $this->find('all', array(
    'conditions' => array('Notification.receiver_id' => $receiver_id),
    'fields' => array('Notification.profile_id', 'Notification.notification_text'),
    'contain' => array(
      'Profile' => array(
        'fields' => array('Profile.id', 'Profile.profile_firstname', 'Profile.profile_lastname')
      )
    )
  ));

//编辑2 :您是否已连接模型中的所有字段?我也没有对此进行测试,但我认为有必要将所有字段链接在一起...在您的个人资料模型中,例如:

class Profile extends AppModel {
  public $hasMany = array(
    'NotificationSender' => array(
      'className' => 'Notification',
      'foreignKey' => 'profile_id'
    ),
    'NotificationReceiver' => array(
      'className' => 'Notification',
      'foreignKey' => 'receiver_id'
    )
  );
}

在通知模型中:

class Notification extends AppModel {
  public $belongsTo = array(
    'NotificationSender' => array(
      'className' => 'Profile',
      'foreignKey' => 'profile_id'
    ),
    'NotificationReceiver' => array(
      'className' => 'Profile',
      'foreignKey' => 'receiver_id'
    )
  );
}

similar。正如我所说,我没有测试,对不起。但也许它可以帮助您找到解决方案......

答案 1 :(得分:1)

我遇到了类似的问题。

第一次,我的发现('all')会返回所有内容,但在几天之后,只返回一个结果并且没有任何意义。

我找到了两种解决方法:

第一种方式是从模型中删除$ virtualFields。我的virtualFields类似于:

public $virtualFields = array(
    'total' => 'SUM(Model.quantity*Model.value)'
);

由于这个原因,发现('all')根本不起作用。也许问题是当值== null时,但我不确定。

第二种解决方法是将返回字段放在find('all')中,例如:

    $inputs = $this->InputsOutput->find('all', array(
        'fields' => array(
                'Model.id',
            ),
        'conditions' => $conditions,
    ));

我希望这可以提供帮助。

答案 2 :(得分:0)

您有两个评分字段。

 1. profile_rating
 2. rating

如果我假设其中一个是虚拟字段,请在触发查询之前取消设置。

unset($this->ModelName->virtualFields['fieldname']);