Adding back 'default' key to table description, and fixing Model::create() to not include empty default values

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4297 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-01-16 12:53:39 +00:00
parent 5655ec336b
commit 6dcdad1a2c
2 changed files with 18 additions and 11 deletions

View file

@ -179,9 +179,12 @@ class DboMysql extends DboSource {
$column[0] = $column[$colKey[0]]; $column[0] = $column[$colKey[0]];
} }
if (isset($column[0])) { if (isset($column[0])) {
$fields[] = array('name' => $column[0]['Field'], $fields[] = array(
'type' => $this->column($column[0]['Type']), 'name' => $column[0]['Field'],
'null' => $column[0]['Null']); 'type' => $this->column($column[0]['Type']),
'null' => $column[0]['Null'],
'default' => $column[0]['Default']
);
} }
} }

View file

@ -648,7 +648,7 @@ class Model extends Overloadable {
case 'foreignKey': case 'foreignKey':
$data = Inflector::singularize($this->table) . '_id'; $data = Inflector::singularize($this->table) . '_id';
if ($type == 'belongsTo') { if ($type == 'belongsTo') {
$data = Inflector::singularize($this->{$class}->table) . '_id'; $data = Inflector::singularize($assocKey) . '_id';
} }
break; break;
@ -851,25 +851,22 @@ class Model extends Overloadable {
* @param array $data Optional data to assign to the model after it is created * @param array $data Optional data to assign to the model after it is created
* @return array The current data of the model * @return array The current data of the model
*/ */
function create($data = null) { function create($data = array()) {
$this->id = false; $this->id = false;
unset ($this->data);
$this->data = array(); $this->data = array();
$defaults = array();
$cols = $this->loadInfo(); $cols = $this->loadInfo();
if (array_key_exists('default', $cols->value[0])) { if (array_key_exists('default', $cols->value[0])) {
$count = count($cols->value); $count = count($cols->value);
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
if ($cols->value[$i]['name'] != $this->primaryKey) { if ($cols->value[$i]['name'] != $this->primaryKey) {
$this->data[$this->name][$cols->value[$i]['name']] = $cols->value[$i]['default']; $defaults[$cols->value[$i]['name']] = $cols->value[$i]['default'];
} }
} }
} }
if (!empty($data) && $data !== null) { $this->set(am(array_filter($defaults), $data));
$this->set($data);
}
return $this->data; return $this->data;
} }
/** /**
@ -1886,6 +1883,7 @@ class Model extends Overloadable {
/** /**
* Gets all the models with which this model is associated * Gets all the models with which this model is associated
* *
* @param string $type
* @return array * @return array
*/ */
function getAssociated($type = null) { function getAssociated($type = null) {
@ -1908,6 +1906,12 @@ class Model extends Overloadable {
} else { } else {
$assoc = am($this->hasOne, $this->hasMany, $this->belongsTo, $this->hasAndBelongsToMany); $assoc = am($this->hasOne, $this->hasMany, $this->belongsTo, $this->hasAndBelongsToMany);
if (array_key_exists($type, $assoc)) { if (array_key_exists($type, $assoc)) {
foreach ($this->__associations as $a) {
if (isset($this->{$a}[$type])) {
$assoc[$type]['association'] = $a;
break;
}
}
return $assoc[$type]; return $assoc[$type];
} }
return null; return null;