Select only specific fields from related models in CakePHP

I have the following relationships:

  • User hasMany UserNotification
  • Notification hasMany UserNotification
  • UserNotificationbelongs UsertoNotification

    Table
  • notificationshas the following columns: id, subject,content

    Table
  • users_notificationshas the following columns: id, user_id, notification_id,status

Q UsersController, how can I get all notifications from one user? (This means that for each notification, all this data: subject, content and status).

And also, how can I limit the number of returned fields? When I make a request from UserController, I do not want to retrieve any field from the User model in the find () array.

Thank!

+3
source share
1 answer

UserController, :

$notifications = $this->User->Notification->find('all');

, Notification.

, , . $ -1. ​​

, , . AppModel, , .

var $recursive = -1;

find():

$this->User->Notification->recursive = -1;

- Containable.

( AppModel ):

var $actsAs = array('Containable');

, , . - , , :

$notifications = $this->User->Notification->find('all', array(
    'contain' => array('User')
);

, !

+3

Source: https://habr.com/ru/post/1777189/


All Articles