Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用saveAll时在行为的beforeSave中修改模型数据_Php_Cakephp_Models_Behavior - Fatal编程技术网

Php 使用saveAll时在行为的beforeSave中修改模型数据

Php 使用saveAll时在行为的beforeSave中修改模型数据,php,cakephp,models,behavior,Php,Cakephp,Models,Behavior,我正在尝试为我正在工作的项目编写一个元行为,它允许我为模型分配自定义变量/属性,类似于在wordpress帖子中创建自定义字段的方式 我已经创建了一个元行为,它将元模型绑定到它所作用的模型,并且还有一个beforeSave回调,该回调循环通过models数据变量,并将模型名称放入元数组中 所有内容都在保存,但当我检查数据库时,模型字段返回为空 元数据的数据库结构是 id - A unique if for the meta model - The name of the model that t

我正在尝试为我正在工作的项目编写一个元行为,它允许我为模型分配自定义变量/属性,类似于在wordpress帖子中创建自定义字段的方式

我已经创建了一个元行为,它将元模型绑定到它所作用的模型,并且还有一个beforeSave回调,该回调循环通过models数据变量,并将模型名称放入元数组中

所有内容都在保存,但当我检查数据库时,模型字段返回为空

元数据的数据库结构是

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

有什么想法吗

干杯,
Dean

我不相信您会有任何运气,因为Model::saveAll()根本不调用beforeSave()。实际上,它在调用$this->save()之前加载关联

模型源的第页显示了在对模型进行任何实际调用之前加载的关联::u save(),该调用不只是验证数据

这意味着,乍一看,MetaBehavior中的magic bindModel()调用似乎不会产生任何影响。老实说,我并不真的为这样的事情烦恼,因为在页面模型类定义中设置关联更简单

当然,你可以:

  • 在MetaBehavior中创建一个afterSave,以便于保存元模型数据,或
  • 在模型中重写saveAll()以添加绑定,然后调用parent::saveAll()
最终,我觉得使用Model::saveAll()并在模型类定义中设置真正的关联更明智。利用行为来促进枯燥的实践是非常高尚的,但这并不意味着您需要在您或其他人遵循的开发方法中使事情变得不那么琐碎


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

为什么不通过正常的HasMany关联将Meta与页面模型关联起来呢?您是否也需要将Meta与其他模型关联?