2013-04-12 28 views
0

我有一個應用程序允許用戶導入/輸入信息,該信息使用Yii活動記錄保存到MySQL數據庫,並且我有一些用戶複製/粘貼文本微軟智能報價。在網站的iPhone實現中解析數據時,這是一個問題,所以我需要一種方法來擺脫所有隨處可見的引號。Yii - 刪除所有活動記錄的智能報價

我發現了一個php函數,它將從一段文本中刪除這些字符,但我想知道是否有方法在Yii中每次將文本保存到數據庫時都會調用該函數。

回答

2

可以擴展CActiveRecord覆蓋beforeSave方法以下列方式:

class ActiveRecord extends CActiveRecord 
{ 
    protected function removeMagicQuotes($value) 
    { 
     return your_function_remove_magic_quotes($value); 
    } 

    protected function beforeSave() 
    { 
     $attributes = array_keys($this->getAttributes()); 
     foreach ($attributes as $attribute) 
      $this->$attribute = $this->removeMagicQuotes($this->$attribute); 
     return parent::beforeSave(); 
    } 
} 

這一次將刪除魔術引號中聲明的所有屬性活躍的紀錄。作爲替代方案,您可以覆蓋beforeValidate方法而不是beforeSave,以在驗證之前刪除引號。

0

我建議爲可能出現的字段創建getter/setter,並在那裏對其進行過濾。

像這樣的事情在外地comment的情況下:

// Make field $comment private so get/set will work 
private $comment = ''; 
public function getComment() 
{ 
    return clear_function($this->comment); 
} 

public function setComment($value) 
{ 
    // Could clear here too if you want, so it will be stored clean in db 
    $this->comment = $value; 
}