423,336 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,336 IT Pros & Developers. It's quick & easy.

Add else Update if exist on table zend framework 1.

100+
P: 132
so i want update if exist this id on table else i want to insert add the data .
Expand|Select|Wrap|Line Numbers
  1. public function addUpdateCust(array $data)
  2. {
  3.  
  4.     if(!empty($data))
  5.     {
  6.         if($this->update($data, array('id = ?' => $data['id'])) > 0 )
  7.         {
  8.             return true;
  9.         }
  10.         $row = $this->createRow();
  11.         //$row->id           = $data['id'];
  12.         $row->code           = $data['code'];
  13.         $row->fabname        = $data['fabname'];
  14.         $row->namecustomer   = $data['namecustomer'];
  15.         $row->adresse        = $data['adresse'];           
  16.         $row->city           = $data['city'];           
  17.         $row->zipcode        = $data['zipcode'];           
  18.         $row->country        = $data['country'];           
  19.         $row->phone          = $data['phone'];       
  20.         $row->representative = $data['representative'];
  21.         $row->idUser         = NULL;
  22.  
  23.         //var_dump($row);
  24.         try {
  25.             return $row->save();           
  26.         } catch (Exception $e) {
  27.             return $e;
  28.         }
  29.     }
  30.     return false;
  31. }
Thanks in advance
Mar 31 '15 #1
Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
you have to look up if and how ZF1 supports INSERT ON DUPLICATE KEY UPDATE
Apr 1 '15 #2

100+
P: 132
Thanks Dormilich ,so i have this method of update :
Expand|Select|Wrap|Line Numbers
  1. public function editCust(array $data)
  2.     {
  3.         if(!empty($data))
  4.         {
  5.             if($this->update($data, array('id = ?' => $data['id'])) > 0 )
  6.             {
  7.                 return true;
  8.             }
  9.             return false;
  10.         }
  11.         return false;
  12.     }
and i want to add to this function because i want do update if exist else add
Thanks in advance
Apr 1 '15 #3

Dormilich
Expert Mod 5K+
P: 8,639
maybe this is of help.
Apr 1 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.