2

In my application, I have two models in association

Notification belongsTo Profile

This is my Notificaiton modal

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;
    }
}

output

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
                )

        )

)

But it always return first row from table even if there are two or more row

Expected Output

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
                )

        )

)

While $this->element('sql_dump'); shows 3 rows were affected due to query but when i print result, it shows only one result, even count & sizeof show 1.

Consider profile_id as sender_id

Can any one tell me where and what I am doing wrong?

user3873381
  • 55
  • 11
  • it may be easier to understand your problem if you post an example of records in the table, the array you are getting and what is the expected output – Nunser Aug 29 '14 at 13:50
  • expected output is whatever find('all') method returns, all rows which satisfy passed conditions – user3873381 Aug 29 '14 at 13:52

3 Answers3

2

I had a similar problem.

In first time, my find('all') returns everything but after some days, just returns one result and don't make any sense.

I found two ways to fix it:

The first way was when I removed the $virtualFields from my Model. My virtualFields was similar to that:

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

The find('all') just dont work because of that. Maybe the problem is when value == null, but I'm not sure.

The second way to fix is put the returns fields in find('all'), for example:

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

I hope this can help.

1

Your function getnotification is just looking for the notifications of ONE single user. Thats whats happening here:

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

Your expected output shows different users (user_id 7 and 8).

What exactly do you want to get?

  • All Notifications for one User (this is what you have)
  • All Notifications for all Users
  • All Users with first notification

// EDIT 1: Got it wrong. Think you might need the Containable behaviour? Haven't tested it, here's the idea:

In your Model: public $actsAs = array('Containable'); And in your Notification-Model:

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')
      )
    )
  ));

// EDIT 2: Have you connected all fields in your Models? I also didn't test this, but I think it's necessary to link all fields together... In your Profile Model something like:

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

And in your Notifications Model:

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

Or similar. As I said, I did not test it, sorry. But maybe it can help you to find the solution...

Community
  • 1
  • 1
Oops D'oh
  • 941
  • 1
  • 15
  • 34
0

You have two fileds for rating.

 1. profile_rating
 2. rating

If I presume one of them is virtual field, Unset it before you fire query.

unset($this->ModelName->virtualFields['fieldname']);
karmicdice
  • 1,063
  • 9
  • 38