Example #1
0
 /**
  * 
  * Save the specified or already existing data for this record to the db.
  * Since this record can only talk to the db via its model property (_model)
  * the save operation will actually be done via $this->_model.
  * 
  * @param \GDAO\Model\RecordInterface|array $data_2_save
  * 
  * @return null|bool true: successful save, false: failed save, null: no changed data to save
  * 
  */
 public function save($data_2_save = null)
 {
     $result = null;
     if (is_null($data_2_save) || empty($data_2_save)) {
         $data_2_save = $this->getData();
     }
     if (!empty($data_2_save) && count($data_2_save) > 0) {
         $pri_val = $this->getPrimaryVal();
         if (empty($pri_val)) {
             //insert
             $inserted_data = $this->_model->insert($data_2_save);
             $result = $inserted_data !== false;
             if ($result === true && is_array($inserted_data) && count($inserted_data) > 0) {
                 //update the record with the newly inserted data
                 $this->loadData($inserted_data);
                 //update initial data
                 $this->_initial_data = $inserted_data;
                 //record has now been saved to the DB,
                 //it is no longer a new record (it now exists in the DB).
                 $this->markAsNotNew();
             }
         } else {
             //load data into the record
             $this->loadData($data_2_save);
             if ($this->isChanged()) {
                 //update
                 $result = $this->_model->updateSpecifiedRecord($this);
                 if ($result === true) {
                     $this->_initial_data = $this->_data;
                 }
             }
         }
     }
     return $result;
 }