Monday, October 1, 2012

struggling with containable - containing the same model twice

I've been wracking my brain over this for a few hours and its doing my head in.

Consider the following models and associations:

Operation belongsTo User, OperationType and hasMany OperationParticipant
OperationType hasMany Operation
OperationParticipant belongsTo User, Operation

if I dump out the contents of OperationParticipant in the OperationParticipants controller, I get exactly what I would expect - a list of OperationParticipants and the associated list of Users. Obviously User is correctly associated with OperationParticipant

The problem comes when I want to call up an Operation and list the users who are participating.

$this->Operation->find('all', array(
   'contain' => array(
      'OperationType', 'User', 'OperationParticipant' => array('User')
      )
   )
);

I get

Model "OperationParticipant" is not associated with model "User"

If, from within the context of the Operations controller I do pr($this->Operation->OperationParticipants->find('all'))); I get no associated data back for OperationParticipants.

Adding OperationParticipants to the $uses property yields the same result, no associated data.

I've done deep contains before, and I cannot for the life of me understand why this is acting up.


Here are the associations:


OperationParticipant.php

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Operation' => array(
            'className' => 'Operation',
            'foreignKey' => 'operation_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
                        'counterCache' => true
        )
    );


Operation.php

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'OperationType' => array(
            'className' => 'OperationType',
            'foreignKey' => 'operation_type_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'OperationParticipant' => array(
            'className' => 'OperationParticipant',
            'foreignKey' => 'operation_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );


OperationType.php

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Operation' => array(
            'className' => 'Operation',
            'foreignKey' => 'operation_type_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );



The users model does not have the association included, Operations is a plugin which may not always be there so I've not included the hasMany (and there is no reason to read the data back the other way, i.e. I have no requirement to get a list of Users and find their associated Operations - only Operations, and find their associated users.

Can anyone shed any light?

--
Like Us on FacekBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: