Yes, the only question is whether I’m doing it correct or not. And maybe a fix for it.
I have an application in Yii where I have a simple Admin backend where I can put some predefined settings in it.
The MySQL database table:

I have an Admin Controller:
I am only showing the intended action
class AdminController extends Controller
{
public function actionIndex()
{
// Update a setting
if (isset($_POST['submit']))
{
AdminSetting::updateSetting('topCategory', $_POST['topCategory']);
AdminSetting::updateSetting('marketRegion', $_POST['marketRegion']);
}
// Get all settings
$topCategory = AdminSetting::model()->find('name = :name', array(':name' => 'topCategory'));
$marketRegion = AdminSetting::model()->find('name = :name', array(':name' => 'marketRegion'));
if (isset($marketRegion->value))
$marketRegion = $marketRegion->value;
if (isset($topCategory->value))
$topCategory = $topCategory->value;
$this->render('index', array('topCategory' => $topCategory, 'marketRegion' => $marketRegion));
}
}
The model AdminSetting.php:
I am only showing the added function: updateSetting
class AdminSetting extends CActiveRecord
{
public static function updateSetting($name, $value)
{
Yii::app()->db->createCommand("UPDATE admin_setting SET value = '$value' WHERE name = '$name'")->execute();
}
}
The User Interface:

I have it done this way, because I have one page, with multiple records showing data. That is not the way it default works in Yii. Whereever I search I see only that other programmers are editing one record at a time.
A second question arise:
How to save multiple records to the database at a time with the default functionality of the ActiveRecordModel.
I see that if I continue this way, I have a growing list in my actionIndex that saves every field by hand.
Thanks in advance for your help.
The best approach here would be to code an entirely new model deriving from
CActiveForm. To go with this, you will have to override the form model’s__getand__setmethods to grab a list of all your configuration settings (let’s say from a hardcoded array, since the actual database entries may end up missing for whatever reason) and behave like all of them exist as properties on the model.After you have done this you are now ready to reap massive benefits:
You can work as if your configuration settings are properties of the form model
There’s no longer any need to pass each one around as its own variable; just pass
$modelto the view and access the settings like$model->settingName(your__getoverride will need to make sure this works)You can now massively set all your configuration options as attributes:
You can easily render the whole form as a series of fields in one go
Do this by creating a new
CFormand dynamically populating itselementsproperty (MyAdminFormModelshould have all the necessary information to do this). You can then render the form easily with something likeOf course all of this is going to require a non-trivial amount of code, but when you set it up you will be able to just add a row to an array (where
MyAdminFormModelgets its data from) and then everything else will automagically work.