0) { $associated = $this->getAssociated(); foreach ($queryData['conditions'] AS $field => $search_value) { // Period indicates that not controller's own model if (strpos($field, '.')) { list($model, $column) = explode('.', $field); // See if it's an association if (array_key_exists($model, $associated)) { // Do stuff based on association type, so far only HABTM if ($associated[$model] == 'hasAndBelongsToMany') { $assoc = $this->hasAndBelongsToMany[$model]; // See if there's a "with" condition to use as join table. // If there is a "with", we should already have all the info we need (ex: keys) if (!empty($assoc['with'])) { $bind_model = $this->{$model}->{$assoc['with']}; $condition = $bind_model->name .'.'. $assoc['foreignKey'] .' = '. $this->name .'.id'; } else { $bind_model = $this->{$model}; // TODO: finalize $condition = ''; } // unbind // Unlike the bind model below where we pass 'false' to ensure the binding is set for the // remainder of the execution, here we do not pass 'false', because if we're doing pagination // we'll do a 'COUNT' find, then the actual 'SELECT' find and if we unbind the HABTM then the // 2nd time we pass through here, we'll lose the association and thus won't get the table/field // condition changed below. // TODO: fix it so we can keep the unbind. $this->unbindModel(array('hasAndBelongsToMany' => array($model))); // bind new // Pass 'false' as the 2nd parameter to bind for remainder of execution $this->bindModel( array( 'hasOne' => array( $bind_model->name => array( // 'fields' => '', 'foreignKey' => false, 'type' => 'INNER', 'conditions' => array($condition) ), ) ), false); // we're working with a different association name now, so change the condition if (!empty($assoc['with'])) { // set it in our return array $ret_queryData['conditions'][$bind_model->name .'.'. $assoc['associationForeignKey']] = $search_value; // and unset the old one, since different id field and such unset($ret_queryData['conditions'][$field]); } // finally: since we have a HABTM change, we add the group by so we can do it properly. $ret_queryData['group'] = $this->name .'.id'; } } } } } return $ret_queryData; } ?>