CakePHP 1.3: proper way to insert or update records -


i'm using cakephp 1.3, , inserting new records (a few ~ thousands) in loop if it's new or updating existing records if exist already. way i'm saving not check whether or not record exist; instead, assign primary key value in each of records. id assign guaranteed unique. cakephp seems know insert if id not exist , update otherwise.

however, i'm not sure if i'm doing right way. read here if i'm creating new record in loop, need $this->create(), implement in other functions know each record going unique. don't here because seems work without errors, , don't need check each of record's existence.

each of record array i'm inserting or updating has key id indicate primary key in respective tables, , each array contains data 2 models associated.

e.g.

$this->saveall($mydata);  array (     [mymodel] => array     (         [id] => 123         [xyz] => ...         ...     )     [mysecondmodel] => array     (         [id] => 789         [my_model_id] => 123         [qwe] => ...         ...     ) ) 

is i'm doing good, or need check existence followed $this->create(), if necessary?

if turn on sql debugging, see cake fetches record database if specify id before save(). if record exists cake perform update, otherwise insert; specifying null id forces insert. doing fine.

the create() important if not filling columns; otherwise data remain in object , end spilling data 1 record other. create resets internal data structure.

you don't need check existence of data after save(), unless you're paranoid. however, do check result save(); if returns false went wrong.


Comments