1. 程式人生 > >yii2.0行為:資料型別轉換

yii2.0行為:資料型別轉換

use yii\behaviors\AttributeTypecastBehavior;
 
  class Item extends \yii\db\ActiveRecord
  {
 
      public function rules()
      {
          return [
              ['amount', 'integer'],
              ['price', 'number'],
              ['is_active', 'boolean'],
          ];
      }
 
      public function behaviors()
      {
          return [
              'typecast' => [
                 'class' => AttributeTypecastBehavior::className(),
                  // 'attributeTypes' will be composed automatically according to `rules()`
              ],
          ];
      }
 
      // ...
 }