2

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

通知 belongsTo 配置文件

这是我的通知模式

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& sizeofshow 1

考虑profile_idsender_id

谁能告诉我我在哪里做错了什么?

4

3 回答 3

2

我有一个类似的问题。

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

我找到了两种解决方法:

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

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

find('all') 只是因为这个而不起作用。也许问题出在 value == null 时,但我不确定。

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

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

我希望这会有所帮助。

于 2014-11-08T03:25:45.403 回答
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'
    )
  );
}

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

于 2014-08-31T17:04:39.850 回答
0

您有两个评级文件。

 1. profile_rating
 2. rating

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

unset($this->ModelName->virtualFields['fieldname']);
于 2014-11-10T05:25:45.193 回答