Refactoring automatic model creations for HABTM associations

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5606 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-08-29 10:14:14 +00:00
parent ae4ebec603
commit d20ddfeadc
3 changed files with 27 additions and 16 deletions

View file

@ -1086,8 +1086,12 @@ class DboSource extends DataSource {
$joinFields = $model->{$assocData['with']}->loadInfo();
$joinFields = $joinFields->extract('{n}.name');
if (is_array($joinFields) && !empty($joinFields)) {
if (is_array($joinFields) && !empty($joinFields) && count($joinFields) > 2) {
$joinFields = $this->fields($model->{$assocData['with']}, $model->{$assocData['with']}->name, $joinFields);
} else {
$joinFields = array();
$joinAssoc = null;
$joinAlias = $joinTbl;
}
}

View file

@ -273,7 +273,7 @@ class Model extends Overloadable {
'belongsTo' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'counterCache'),
'hasOne' => array('className', 'foreignKey','conditions', 'fields','order', 'dependent'),
'hasMany' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'dependent', 'exclusive', 'finderQuery', 'counterQuery'),
'hasAndBelongsToMany' => array('className', 'joinTable', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery', 'deleteQuery', 'insertQuery')
'hasAndBelongsToMany' => array('className', 'joinTable', 'with', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery', 'deleteQuery', 'insertQuery')
);
/**
* Holds provided/generated association key names and other data for all associations
@ -682,6 +682,10 @@ class Model extends Overloadable {
$data = Inflector::singularize($this->{$class}->table) . '_id';
break;
case 'with':
$data = Inflector::camelize(Inflector::singularize($this->{$type}[$assocKey]['joinTable']));
break;
case 'joinTable':
$tables = array($this->table, $this->{$class}->table);
sort ($tables);
@ -691,6 +695,7 @@ class Model extends Overloadable {
case 'className':
$data = $class;
break;
}
$this->{$type}[$assocKey][$key] = $data;
}
@ -705,19 +710,21 @@ class Model extends Overloadable {
}
}
if (isset($this->{$type}[$assocKey]['with'])) {
$with = $this->{$type}[$assocKey]['with'];
$this->__constructLinkedModel($with);
$this->{$type}[$assocKey]['joinTable'] = $this->{$with}->table;
} elseif ($type == 'hasAndBelongsToMany') {
$joinClass = Inflector::camelize($this->name . $assocKey);
if(!class_exists(low($joinClass))) {
$this->{$type}[$assocKey]['_with'] = $joinClass;
$this->{$joinClass} = new AppModel(array(
'name' => $joinClass,
'table' => $this->{$type}[$assocKey]['joinTable'],
'ds' => $this->useDbConfig
));
if (isset($this->{$type}[$assocKey]['with']) && !empty($this->{$type}[$assocKey]['with'])) {
$joinClass = $this->{$type}[$assocKey]['with'];
if (!loadModel($joinClass)) {
$this->__constructLinkedModel($joinClass, 'AppModel', false, $this->{$type}[$assocKey]['joinTable'], $this->useDbConfig);
$this->{$joinClass}->name = $joinClass;
$this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey'];
if(count($this->{$joinClass}->_schema->value) > 2) {
if(isset($this->{$joinClass}->_schema->value['id'])) {
$this->{$joinClass}->primaryKey = 'id';
}
}
} else {
$this->__constructLinkedModel($joinClass);
$this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table;
}
}
}