Changing model data in beforeSave of Behavior when using saveAll

I'm trying to write Meta behavior for a project I'm working on, which will allow me to assign custom variables / attributes to the model, similar to how you can create custom fields in a wordpress message.

I created a Meta behavior that associates a metamodel with the model on which it acts, and also has a beforeSave callback that passes through the model data variable and puts the model name in a meta array.

Everything is saved, but when I check the database, the model field is returned empty.

Database structure for Meta p>

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

The data coming into the saveAll function from the form,

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
                )

        )

)

and after he performed beforeSave behavior, he

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
                )

        )

)

Behavior Code

<?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, .

?

Cheers,

+3
1

, , Model:: saveAll() beforeSave(). , , $this- > save().

1652 , :: __ save(), .

, , magicModel() MetaBehavior . , - , .

, :

  • afterSave MetaBehavior, ,
  • saveAll() , , parent:: saveAll()

, Model:: saveAll() . DRY- , , ,

TL;DR; Meta , , .

0

Source: https://habr.com/ru/post/1743924/


All Articles