2010-01-31 3 views
1

단순한 방법으로 Zend Framework에서 모델을 사용하여 단일 데이터베이스 값을 업데이트해야하는 특정 방법이 있습니까?Zend Framework 모델에서 단일 데이터베이스 셀 업데이트

나는 현재이 작업을 수행 :

class Model_MyModel extends Zend_Db_Table_Abstract 
{ 
    $_name = 'table'; 
    public function updateSetting($id,$status) 
    { 
     $data = array(
      'status' => $status 
     ); 
     $this->update($data, 'id = '.$id); 
    } 
} 

$update = new Model_MyModel(); 
$update->updateSetting(10,1); 

은 분명히 내가 업데이트 할 수있는 열 등 다른 인수에 전달할 수 있습니다. 나는이 일을해야하는 "마법"적인 방법이 있는지 궁금해했다. 당신은 다음과 같은 물건을 할 수

class Model_MyModel extends Zend_Db_Table_Abstract 
{ 
    protected $_name = 'table'; 

    /** 
    * Should be a Zend_Db_Table_Row instance 
    * 
    * @var Zend_Db_Table_Row 
    */ 
    protected $_currentRow = null; 

    /** 
    * Property overloader 
    * 
    * For more information on this see 
    * http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members 
    * 
    * @param string $key 
    * @param string $value 
    * @return void 
    */ 
    public function __set($key, $value) 
    { 
     $row = $this->getCurrentRow(); 
     if (null !== $row) 
     { 
      $row->$key = $value; 
     } 
     else 
     { 
      throw new Exception('Cannot update a column on a non existent row!'); 
     } 
    } 

    /** 
    * Save current row 
    * 
    * @return Model_MyModel 
    */ 
    public function saveCurrentRow() 
    { 
     $row = $this->getCurrentRow(); 
     if (null !== $row) 
     { 
      $row->save(); 
     } 
     else 
     { 
      throw new Exception('Cannot save a non existent row!'); 
     } 
    } 

    /** 
    * Set current row 
    * 
    * @param Zend_Db_Table_Row $row 
    * @return Model_MyModel 
    */ 
    public function setCurrentRow(Zend_Db_Table_Row $row) 
    { 
     $this->_currentRow = $row; 
     return $this; 
    } 

    /** 
    * Get current row 
    * 
    * @return Zend_Db_Table_Row 
    */ 
    public function getCurrentRow() 
    { 
     return $this->_currentRow; 
    } 
} 

:

$model = new Model_MyModel(); 
$model->status = 'foo'; 
$model->somecolumn = 'bar' 
$model->saveCurrentRow(); 

이 방법은 코드에서 최소한의 수정을 요구하지만, 더 나은

답변

관련 문제