Search code examples
kohana-3kohana-db

Duplicate entry '2' for key 'PRIMARY' on Auto Modeller Update Function in Kohana


Am trying to perform an update and nothing seems to work. It has something to do with my callback I suppose as the update works well when the callback is disabled. This is my try block.

try{
      $updatestat=NULL;
      $updateresult=NULL;
      $id = Arr::get($_POST, 'id');

      $scode=trim(Arr::get($_POST, 'stationcode'));
      $sname=trim(Arr::get($_POST, 'stationname'));
      $dsupdate = new Model_Dstations($id);
      $dsupdate->scode = $scode;
      $dsupdate->sname = $sname;

      $validation = new Validation($_POST);          
  $validation->rule('scode', array($dsupdate, 'check_updatecheck' ), array( ':validation', ':value',':field',$id ));
      $validation->rule('sname', array($dsupdate, 'check_updatecheck' ), array( ':validation', ':value',':field',$id ));
      $result['sql']=$dsupdate->save($validation);}

Solution

  • Your code looks like a complete mess. Try this:

      $dsupdate = new Model_Dstations($id);
    
      $validation = new Validation($_POST);          
      $validation->rule('scode', array($dsupdate, 'check_updatecheck' ), array( ':validation', ':value',':field',$id ));
      $validation->rule('sname', array($dsupdate, 'check_updatecheck' ), array( ':validation', ':value',':field',$id ));
    
      if ($validation->check()) {
          $dsupdate->scode = $scode;
          $dsupdate->sname = $sname;
          $dsupdate->save();
      }