使用saveAll时,在行为之前修改模型数据

时间:2010-05-04 06:16:30

标签: php cakephp models behavior

我正在尝试为我正在处理的项目编写Meta行为,这样我就可以将自定义变量/属性分配给模型,类似于如何在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
                )

        )

)

之后,它已经完成了之前的行为保存

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中以检索它们。

有什么想法吗?

干杯, 迪安

1 个答案:

答案 0 :(得分:0)

我不相信你会对此有任何好运,因为Model :: saveAll()根本不会调用beforeSave()。实际上,它在加载关联之前调用$ this-&gt; save()。

模型源的

Line 1652显示在对Model :: __ save()进行任何实际调用之前加载的关联,它不仅仅验证数据。

这意味着,乍一看,MetaBehavior中的神奇bindModel()调用看起来不会有任何影响。说实话,我并不是真的在为这样的东西打扰,因为在页面模型类定义中设置关联会更简单。

当然,你可以:

  • 在MetaBehavior中创建一个便于保存元模型数据的afterSave,或
  • 覆盖模型中的saveAll()以添加绑定,然后调用parent :: saveAll()

最终,我觉得使用Model :: saveAll()并在模型类定义中设置真正的关联更为理智。利用行为促进干旱实践是非常高尚的,但这并不意味着你需要将事情变得不那么简单,作为你或其他人遵循的发展方法的一部分

TLDR;您已经将Meta添加到您的behavior数组中,添加关联只是同一文件中的另外几行代码。