我正在尝试为我正在处理的项目编写元行为,这将允许我将自定义变量/属性分配给模型,类似于在 wordpress 帖子中创建自定义字段的方式。
我创建了一个 Meta 行为,它将元模型绑定到它所作用的模型,并且还有一个 beforeSave 回调,该回调循环遍历模型数据变量并将模型名称放入元数组中。
一切都在保存,但是当我检查数据库时,模型字段又变空了。
Meta的数据库结构是
id - A unique if for the meta
model - The name of the model that this meta entry is associated with
foreign_id - The id of the above model that this meta entry is associated with
key - Name of the meta variable
value - Value of the meta variable
从表单传到 saveAll 函数的数据是
Array
(
[Page] => Array
(
[id] => 12
[name] => Test
[slug] => a/b/c/d
[layout] => 0
[body] => Test multilevel
)
[Meta] => Array
(
[0] => Array
(
[id] => 1
[key] => page_title
[value] => About Us
)
[1] => Array
(
[id] => 6
[key] => test4
[value] => test
)
[2] => Array
(
[key] => test3
[value] => lala
)
)
)
在它运行完 beforeSave 行为之后,它是
Array
(
[Page] => Array
(
[id] => 12
[name] => Test
[slug] => a/b/c/d
[layout] => 0
[body] => Test multilevel
[modified] => 2010-05-04 15:56:54
)
[Meta] => Array
(
[0] => Array
(
[id] => 1
[key] => page_title
[value] => About Us
[model] => Page
)
[1] => Array
(
[id] => 6
[key] => test4
[value] => test
[model] => Page
)
[2] => Array
(
[key] => test3
[value] => lala
[model] => Page
)
)
)
行为的代码是
<?php
/**
* Meta Model Behavior
*
* Adds custom variables to models
*
**/
class MetaBehavior extends ModelBehavior {
/**
* Contains configuration settings for use with individual model objects.
* Individual model settings should be stored as an associative array,
* keyed off of the model name.
*
* @var array
* @access public
* @see Model::$alias
*/
var $__settings = array();
var $__defaults = array(
'class' => 'Meta',
'foreign_key' => 'foreign_id',
'dependent' => true,
'auto_bind' => true
);
/**
* Initiate Meta Behavior
*
* @param object $model
* @param array $config
* @return void
* @access public
*/
function setup(&$model, $settings = array()) {
$default = $this->__defaults;
$default['conditions'] = array('Meta.model' => $model->alias);
if (!isset($this->__settings[$model->alias])) {
$this->__settings[$model->alias] = $default;
}
$this->__settings[$model->alias] = array_merge($this->__settings[$model->alias], ife(is_array($settings), $settings, array()));
if ($this->__settings[$model->alias]['auto_bind']) {
$hasManyMeta = array(
'Meta' => array(
'className' => $this->__settings[$model->alias]['class'],
'foreignKey' => $this->__settings[$model->alias]['foreign_key'],
'dependent' => $this->__settings[$model->alias]['dependent'],
'conditions' => $this->__settings[$model->alias]['conditions']
)
);
$metaBelongsTo = array(
$model->alias => array(
'className' => $model->alias,
'foreignKey' => $this->__settings[$model->alias]['foreign_key']
)
);
$model->bindModel(array('hasMany' => $hasManyMeta), false);
$model->Meta->bindModel(array('belongsTo' => $metaBelongsTo), false);
}
}
function beforeSave(&$model) {
foreach($model->data[$this->__settings['class']] as $key => $value) {
$model->data[$this->__settings['class']][$key]['model'] = $model->alias;
}
return true;
}
} // End of MetaBehavior
?>
我有一种感觉,这可能是因为关系,并且 saveall 在保存关联时使用了传递的数据(原始数据)。
我想到的唯一另一种方法是删除关系并将一些代码放入行为的 afterSave 函数中以处理保存,然后将一些其他代码放入 afterFind 以检索它们。
有任何想法吗?
干杯,院长