cakephp - Creating new record if the id is not in db -
i working on cakephp 2.x , want create new row or record if userid not present in db otherwise update record, problem is: not updating current record. creating record userid "0" dont know problem is. if not finding record should create record userid giving.
public function activenostatus(){ $this->loadmodel('status'); $userid = $this->request->data('iduser'); $notify = $this->request->data('notify'); $data = array(); $data['activeno'] = $notify; $count = $this->status->finduserid($userid); if($count>0){ $this->status->id = $userid; $this->status->save($data); echo "update"; }else{ //create new row $datanew=array(); $this->status->id = $userid; $this->request->data['status']['activeno']=$notify; $this->status->save($this->request->data); echo "ok"; } }
it seems iduser field not autoincrement, in cakephp documentaion :
all tables cakephp models interact (with exception of join tables), require singular primary key uniquely identify each row. if wish model table not have single-field primary key, cakephp’s convention single-field primary key added table. have add single-field primary key if want use table’s model.
rather using auto-increment key primary key, may use char(36). cake use unique 36 character uuid (string::uuid) whenever save new record using model::save method.
so should make iduser autoincrement primary key.
Comments
Post a Comment