0

I need to read all records from table agreements, make changes in filed payments, and update all records, save to table. So, my problem is, that save() only create empty record. Do not update exists record. I show you how:

Reading from table:

$agreements = $this->Agreement->find('all');
$payments = $this->Payment->find('all');

Manipulation on fields (part of)(example):

$id=0;
foreach ($agreements as $agreement):
    for ($i=$first_agreement; $i<=$last_agreement; $i++){ 
        if ( $agreement['Agreement']['agreement_number']==$i){
           $agreements[$id]['Agreement']['payment']=$payd[$i];
        }
      }
$id++;
endforeach;

Writting to table:

$this->Agreement->save();

A echo debug($agreements) shows correct array, i have tryed also :

$this->Agreement->save($agreements);

or

$this->Agreement->save($this->request->data);

Can you help/explain me how to write all record?

Cake 2.5.2 PHP : 5.4.4-14

Majid Golshadi
  • 2,686
  • 2
  • 20
  • 29
tomas3man
  • 29
  • 7

1 Answers1

2

Model::save() only saves a single record. If you want to save multiple records you need to use Model::saveAll() or Model::saveMany().

ADmad
  • 8,102
  • 16
  • 18
  • I will check it. Do i need to set recursive = -1? – tomas3man Jul 01 '14 at 17:45
  • I have been thinking about. And i can see that one record is written to database. But all fileds are empty? Can you explain that? If so, saveMany() will save many empry records. And sql debug show me that engine are inserting record not update. Can you tell me how to change insert to upfate in active record convention? Will you paste me some examples? – tomas3man Jul 01 '14 at 17:48
  • this not working, sql debug show only this: INSERT INTO `uat`.`agreements` (`modified`, `created`) VALUES ('2014-07-01 21:02:49', '2014-07-01 21:02:49'). Only this fields are updated. – tomas3man Jul 01 '14 at 19:04