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

Cakephp find(';all';)只返回一行,cakephp,model,cakephp-2.5,Cakephp,Model,Cakephp 2.5,在我的应用程序中,我有两个关联的模型 通知属于档案 这是我的通知 class Notification extends AppModel { public $name = 'Notification'; public $belongsTo = array('Profile'); public function getnotification($id = NULL){ $result = $this->find('all',array( 'con

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

通知
属于
档案

这是我的通知

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
                )

        )

)
但它总是从表中返回第一行,即使有两行或更多行

预期产出

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->元素('sql_dump')显示由于查询而受影响的3行,但当我打印结果时,它只显示一个结果,甚至是
计数
&
大小
显示
1

个人资料\u id
视为
发件人\u id


有人能告诉我哪里出了问题,我做错了什么吗?

您的函数getnotification只是查找单个用户的通知。这就是这里发生的事情:

'conditions' => array(
  'Notification.receiver_id' => $id
)
您的预期输出显示不同的用户(用户id 7和8)

你到底想要什么

  • 一个用户的所有通知(这是您拥有的)
  • 所有用户的所有通知
  • 第一次通知的所有用户
//编辑1:搞错了。你认为你可能需要这种行为吗?我还没有测试过,这里有一个想法:

在您的模型中:
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'
    )
  );
}

或者。正如我所说,我没有测试它,对不起。但也许它能帮你找到解决办法…

我也有类似的问题

第一次,我的find('all')返回所有内容,但几天后,只返回一个结果,没有任何意义

我找到了两种解决方法:

第一种方法是从模型中删除$virtualFields。我的virtualFields与此类似:

public $virtualFields = array(
    'total' => 'SUM(Model.quantity*Model.value)'
);
正因为如此,这个发现(“全部”)根本不起作用。也许问题在于value==null,但我不确定

第二种修复方法是将返回字段放入find('all'),例如:

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

我希望这能有所帮助。

您有两个文件用于评级

 1. profile_rating
 2. rating
若我假定其中一个是虚拟字段,那个么在启动查询之前先将其取消设置

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

如果您在表中发布一个记录示例,您将获得的数组和预期输出是什么,则可能更容易理解您的问题。预期输出是find('all')方法返回的任何内容,所有满足传递条件的行使用通知发送方配置文件的数组对一个用户发出所有通知