Asim Zaidi
Asim Zaidi

Reputation: 28284

reading and updating record and doing addition

I have a field called force. Its by default a null field. I want to add 1 everytime I run an if block. here is my code sample

if($somecondition){
$array = array();
$array[] = $this->Model->read(null, 1); 
$array['force']++;
$this->Model->updateAll(array('Model.complete' => 1, 'Model.force' => $array['force']),array('Model.completed IS NULL'));
}

I am getting an error of undefined variable $array. Not sure why.

Upvotes: 0

Views: 53

Answers (2)

Anh Pham
Anh Pham

Reputation: 5481

it seems you are very new to Cake (and maybe even PHP)

if($somecondition){
  $array = $this->Model->read('force',1);
  if($array['Model']['force']===NULL)$array['Model']['force'] = 0;
  $array['Model']['force']++;
  $array['Model']['complete']=1;
  $this->Model->save($array);
}

It you can, change the default value of 'force' to 0 in the DB, so you don't have to check for that here.

Upvotes: 1

Baseer
Baseer

Reputation: 1197

You should initialize $array['force'] to 0 before incrementing it.

Upvotes: 1

Related Questions