0

I read much about how great containable is. Honestly I have read all docs, I have it working in my Users controller, but some things are not clear:

  1. Do I have to use it in All actions or only in Index()?
  2. Do I have to define it in every controller index() function or is it enough to it once in the Users controller
  3. What about if e.g. Country_ID is a FK connected to both user and a related model? For example:

    function index() {
       $this->paginate = array(
                'limit'=>10,
                'order'=>'User.created DESC',
                'fields'=>array('User.id','User.name', 'User.country_id', 'User.email'),
                'contain'=>array(
                    'Post',
                    'Company' => array(
                        'Country' => array(
                            'fields' => array('id', 'country')
                        )
                    ),
                    'Position' => array(
                        'Profession'
                    ),
                    'Preference',
                    'Country',
                    'Type'
                    ),
    
            );
            $this->set('users',$this->Paginate('User'));
    

    }

Country is both connected to User and Company. How to define this without creating duplicates?

Many thanks!

ChrisDK
  • 243
  • 1
  • 6
  • 14

1 Answers1

1

You seem to have the wrong idea about containable. It "allows you to filter and limit model find operations". You use it whenever you need include (or exclude) specific related model data in your find().

For example, a User hasOne Profile, hasMany Roles, which belongsTo a Company. And you need to get all the roles and related companies for a user, but you don't need the profile, you can use $this->User->find('first',array('conditions'=>...,'contain'=>array('Role'=>array('Company'))))

It has nothing to do with index() or users_controller.

Country is both connected to User and Company. How to define this without creating duplicates? What duplicates?

Anh Pham
  • 5,431
  • 3
  • 23
  • 27