0

Controller:

    $data =
            array(
                'ContentI18n' =>
                array(
                    0 =>
                    array(
                        'title' => 'first',
                        'author' => 'first',
                        'source' => 'sgfsdfrst',
                        'lang' => 'fa',
                    ),
                ),
                'Content' =>
                array(
                    'publish' => 1,
                    'type' => 3,
                    'pages' => 8,
                    'volume' => 7,
                    'modified' => '2012-05-27 14:16:37',
                    'created' => '2012-05-27 14:16:37',
                    'lang' => 'fa',
                ),
    );
$this->Content->create();
$this->Content->saveAll($data);

Model:

public $hasMany = array(
    'ContentI18n' => array(
        'className' => 'ContentI18n',
    )
);

beforeSave function in behavior:

public function beforeSave(Model $model) {
    // Define the new Translate model
    App::uses($model->name . 'I18n', 'Model');
    $varI18n = $model->name . 'I18n';
    $modelI18n = new $varI18n;


    foreach ($model->data[$model->name] as $key => $data) {
        if (!in_array($key, $this->setting))
            $modelData[$model->name][$key] = $data;
        else
            $modelData[$model->name . 'I18n'][0][$key] = $data;
    }
    $modelData[$model->name . 'I18n'][0]['lang'] = $model->locale;
    $modelData[$model->name]['lang'] = $model->locale;
    $model->data = $modelData;

    //pr($model->data);
    return TRUE;
}

every things seems be cause when is save it directly it's save with saveAll. but when i use same structure of data in behavior did not work without any error.

Arash Mousavi
  • 2,023
  • 3
  • 19
  • 39

1 Answers1

2

I found out that beforeSave callback will not trigger by saveAll. for executing some code before saveAll we must override saveAll function in our model.

public function saveAll($data, $options = array()) {
            /*
            your code you want execute before saving...
            */
    parent::saveAll($data, $options);
}

for other saving methods such as saveMany,saveAssociated,... beforeSave trigger by them.

Arash Mousavi
  • 2,023
  • 3
  • 19
  • 39