键值表的Yii格式和模型

键值表的Yii格式和模型,yii,Yii,我有一个只有两列键值的表。我想创建一个表单,允许用户插入3对键值设置 我需要将3个不同的模型传递到视图吗?或者有什么方法可以做到这一点吗?查看此链接: 在Yii中,这被认为是更新以创建多个模型的最佳形式 本质上,对于创建,您可以创建一个for循环,生成您希望可见的任意多个输入,并在控制器循环中创建新模型 查看文件: for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherw

我有一个只有两列键值的表。我想创建一个表单,允许用户插入3对键值设置

我需要将3个不同的模型传递到视图吗?或者有什么方法可以做到这一点吗?

查看此链接:

在Yii中,这被认为是更新以创建多个模型的最佳形式

本质上,对于创建,您可以创建一个for循环,生成您希望可见的任意多个输入,并在控制器循环中创建新模型

查看文件:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
控制器操作创建:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
要更新现有模型,您可以使用相同的方法,但不必创建新模型,您可以基于$\u POST['Settings']数组中的键加载模型

要回答有关将3个模型传递给视图的问题,可以不传递它们,但要验证数据并向视图发送正确的错误消息,应将放置在数组中的三个模型传递给数组中的视图

注意:上述示例应按原样工作,但不提供任何验证,以验证模型是否有效或是否正确保存了这些模型查看此链接:

在Yii中,这被认为是更新以创建多个模型的最佳形式

本质上,对于创建,您可以创建一个for循环,生成您希望可见的任意多个输入,并在控制器循环中创建新模型

查看文件:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
控制器操作创建:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
要更新现有模型,您可以使用相同的方法,但不必创建新模型,您可以基于$\u POST['Settings']数组中的键加载模型

要回答有关将3个模型传递给视图的问题,可以不传递它们,但要验证数据并向视图发送正确的错误消息,应将放置在数组中的三个模型传递给数组中的视图


注意:上面的示例应该按原样工作,但不提供任何验证,以验证模型是否有效或是否正确保存了这些模型。我将给您一个提示,让您知道,这样做可能会使您的生活变得非常复杂

我目前正在使用一个类似于此键值的
EAV
模式表,下面列出了一些您可能会发现困难或不可能的事情:

  • 使用
    CDbCriteria
    mergeWith()在搜索()时过滤“值”上的相关元素(或其他)
  • 过滤
    CGridView
    CListView
如果这只是一个非常直接的键值,没有相关的实体方面(我猜这是因为它看起来像设置),那么一种方法是:

  • 为设置表创建一个普通的“设置”
    CActiveRecord
    (您将使用它将条目保存到设置表)
  • 通过扩展
    CFormModel
    创建表单模型,并将其用作表单中的$model
  • 在表单模型中添加一个save()方法,该方法将使用“设置”模型单独插入键值对。如果键值对未通过设置->验证()(如果适用),最好使用事务
  • 或者,您可能希望覆盖表单模型的
    getAttributes()
    ,以便在用户希望编辑条目时返回db数据
我希望这已经足够清楚了。 让我给你一些基本的代码设置。请注意,我没有测试这个。不过,这会给你一个大致的想法

设置模式:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
设置表单模型:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
控制器:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
表单视图:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
如果你想进一步澄清一点(代码等),请让我知道


PS:对于后代来说,如果其他人对这个和EAV感到疑惑,他们可以检查数据库或选择更合适的数据库系统,例如(有一些扩展)或

我将给你一个提示,让你知道你可能会因此而使你的生活变得非常复杂

我目前正在使用一个类似于此键值的
EAV
模式表,下面列出了一些您可能会发现困难或不可能的事情:

  • 使用
    CDbCriteria
    mergeWith()在搜索()时过滤“值”上的相关元素(或其他)
  • 过滤
    CGridView
    CListView
如果这只是一个非常直接的键值,没有相关的实体方面(我猜这是因为它看起来像设置),那么一种方法是:

  • 为设置表创建一个普通的“设置”
    CActiveRecord
    (您将使用它将条目保存到设置表)
  • 通过扩展
    CFormModel
    创建表单模型,并将其用作表单中的$model
  • 在表单模型中添加一个save()方法,该方法将使用“设置”模型单独插入键值对。如果键值对未通过设置->验证()(如果适用),最好使用事务
  • 或者,您可能希望覆盖表单模型的
    getAttributes()
    ,以便在用户希望编辑条目时返回db数据
我希望这已经足够清楚了。 让我给你一些基本的代码设置。请注意,我没有测试这个。不过,这会给你一个大致的想法

设置模式:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
设置表单模型:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
控制器:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
表单视图:

for ( $settings as $i=>$setting ) //Settings would be an array of Models (new or otherwise)
{
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::activeLabelEx($setting, "[$i]key");
    echo CHtml::error($setting, "[$i]key");

    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::activeTextField($setting, "[$i]value");
    echo CHtml::error($setting, "[$i]value");
}
$settings = array(new Setting, new Setting, new Setting);
if ( isset( $_POST['Settings'] ) )
    foreach ( $settings as $i=>$setting )
        if ( isset( $_POST['Setttings'][$i] ) )
        {
            $setting->attributes = $_POST['Settings'][$i];
            $setting->save();
        }
//Render View
class Setting extends CActiveRecord
{
   public function tableName()
   {
      return 'settings';
   }
}
class SettingsForm extends CFormModel
{
   /**
    * Load attributes from DB
    */
   public function loadAttributes()
   {
      $settings = Setting::model()->findAll();
      $this->setAttributes(CHtml::listData($settings,'key','value'));
   }

   /*
    * Save to database
    */
   public function save()
   {
      foreach($this->attributes as $key => $value)
      {
         $setting = Setting::model()->find(array('condition'=>'key = :key',
                                                 'params'=>array(':key'=>$key)));
         if($setting==null)
         {
            $setting = new Setting;
            $setting->key = $key;
         }
         $setting->value = $value;
         if(!$setting->save(false))
            return false;
      }
      return true;
   }
}
public function actionSettingsForm()
{
   $model = new Setting;
   $model->loadAttributes();

   if(isset($_POST['SettingsForm']))
   {
      $model->attributes = $_POST['SettingsForm'];
      if($model->validate() && $model->save())
      {
         //success code here, with redirect etc..
      }
   }
   $this->render('form',array('model'=>$model));
}
$form=$this->beginWidget('CActiveForm', array(
        'id'=>'SettingsForm'));

//all your form element here + submit 
//(you could loop on model attributes but lets set it up static for now)
//ex:
echo $form->textField($model,'fieldName'); //fieldName = db key

$this->endWidget($form);
如果你想进一步澄清一点(代码等),请让我知道

PS:对于后代来说,如果其他人对此和EAV感到疑惑,他们可以检查数据库或选择更合适的数据库系统,例如(有一些扩展)或