user1171243
user1171243

Reputation: 31

cakephp: check controller action in model (beforeSave)

Is there an easy way to check in the model which action was used in the controller to access the beforeSave() function?

I want to have another behavior if beforeSave() was called by edit vs. If it was called by add.

As a work around I used a hidden field in my form but I think that is not the most efficient way.

Thank you very much.

Upvotes: 1

Views: 3121

Answers (3)

Max G
Max G

Reputation: 66

I found this on stackoverflow when googling:

Router::getParams()

Output of this in a model will be:

array(5) {
  ["plugin"]=>
  NULL
  ["controller"]=>
  string(12) "cashflowrows"
  ["action"]=>
  string(5) "chart"
  ["named"]=>
  array(0) {
  }
  ["pass"]=>
  array(0) {
  }
}

Upvotes: 0

Kevin Vandenborne
Kevin Vandenborne

Reputation: 1387

You could always use your $this->params['controller'] and $this->params['action']

Upvotes: 1

del_dan
del_dan

Reputation: 873

I do not know if I understand correctly.

function beforeSave() {
  if (!$this->id && !isset($this->data[$this->alias][$this->primaryKey])) {
    // insert
  } else {
    // edit
  }
  return true;
}

Upvotes: 0

Related Questions