Cakephp beforeSave doesn't save new data when using saveAll?

I have a relatively simple record model with five fields:

  • ID
  • type (which data type is the record)
  • quantity (how many of this type)
  • unit (type unit)
  • date (time of data transfer when this record was entered)
  • user_id (user id that enters

So, nothing out of the ordinary. Now in one form there can be several records (both existing and new), the form is expanded through ajax calls.

When submitting the form, $this->datait looks like this:

Array
(
    [Entry] => Array
        (
            [date] => 2011-01-07
            [0] => Array
                (
                    [id] => 1
                    [type] => Eat
                    [amount] => 1 Steak, one baked potatoe
                    [unit] => lunch
                    [time] => Array
                        (
                            [hour] => 13
                            [min] => 31
                        )

                )

            [1] => Array
                (
                    [type] => weight
                    [amount] => 78.5
                    [unit] => KG
                    [time] => Array
                        (
                            [hour] => 22
                            [min] => 22
                        )

                )

        )

)

The first entry in $this->data['Entry']['date']is the date ALL records should use. And since user_id is also missing, I created the "beforeSave" function in the input model. It looks like this:

function beforeSave() {
    App::import('Component','Session');
    $this->Session = new SessionComponent();

    if (isset($this->data) && isset($this->data['Entry'])) {
        $date = $this->data['Entry']['date'];
        unset($this->data['Entry']['date']);

        foreach ($this->data['Entry'] as $n => $entry) {
            if (is_array($entry)) {
             $this->data['Entry'][$n]['date'] = $date . ' ' . $entry['time']['hour'] . ':' . $entry['time']['min'] . ':00';
                $this->data['Entry'][$n]['user_id'] = $this->Session->read('Auth.User.id');
            }
        }
        debug($this->data);

    }
    return true;
}

, , mysql datetime user_id . , . ( debug()) :

Array
(
    [Entry] => Array
    (
        [0] => Array
            (
                [id] => 1
                [type] => Eat
                [amount] => 1 Steak, 1 baked potatoe
                [unit] => lunch
                [time] => Array
                    (
                        [hour] => 09
                        [min] => 31
                    )

                [date] => 2011-01-07 09:31:00
                [user_id] => 2
            )

        [1] => Array
            (
                [type] => Weight
                [amount] => 78.5
                [unit] => KG
                [time] => Array
                    (
                        [hour] => 22
                        [min] => 22
                    )

                [date] => 2011-01-07 22:22:00
                [user_id] => 2
            )

    )

)

, , , , . $this->Entry->saveAll($this->data['Entry']), , , $this->data saveAll, , saveAll - , user_id.

, beforeSave , , $this->data, - beforeSave "saveAll" , $this->data , .

+3
4

, hasMany through. , , saveAll(). $this- > $this- > Model:: beforeSave(), . Model:: saveAll(), $this- > . " " . saveAll cake/libs/model/model.php, :

if (empty($data)) {
   $data = $this->data
}

, $data $data . , saveAll(), Model:: set(), Model:: saveAll() ( $data).

+6

SaveAll, / beforeSave. ,

tmp_data

.

var $tmp_data = array();

, , .

$this->model->tmp_data = $this->data;
$this->model->saveAll();

- > beforeSaveBefore $this->data use $this->tmp_data

+1

saveAll() updateAll() deleteAll()

( )

0

?

$this->Entry->saveAll($data,array('validate'=>false));

,

However, do not forget that you need to use create () before saving for individual models if a primary key is not specified. Perhaps mixing new and existing data in the saveAll () file is a problem.

0
source

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


All Articles