cakephp 2.3 - Apply model association info when using find('list') -
for user model have association:
public $belongsto = array( 'country' => array( 'foreignkey' => 'country_id', 'conditions' => array( 'country.code_status' => 'assigned'), 'order' => array( 'short_name_en') ) );
for reason expecting using:
$countries = $this->user->country->find('list');
i list of possible country codes populate dropdown in form , comply association definition.
this not happening, , understandable since find() cannot (?) detect associated model coming from, can not apply conditions/order defined. added method appmodel like:
public function getassociationfind( $model, $type='belongsto' ) { if (!isset($this->$type)) { throw new cakeexception(__('association type %s not found in model %s.', $type, $this->name)); } $typeassociations = $this->$type; if (!isset($typeassociations[$model])) { throw new cakeexception(__('associated model %s not found.', $model)); } $conditions = (isset($typeassociations[$model]['conditions'])) ? $typeassociations[$model]['conditions'] : array(); $order = (isset($typeassociations[$model]['order'])) ? $typeassociations[$model]['order'] : array(); return array( 'conditions' => $conditions, 'order' => $order ); }
this way can use:
$countries = $this->user->country->find('list', $this->user->getassociationfind('country'));
did miss in way models work ? there better/more cakey way ?
Comments
Post a Comment