Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:12:18+00:00 2026-06-06T10:12:18+00:00

Yes, the only question is whether I’m doing it correct or not. And maybe

  • 0

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:

My MySQL admin_setting 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:

Simple 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:12:21+00:00Added an answer on June 6, 2026 at 10:12 am

    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 __get and __set methods 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 $model to the view and access the settings like $model->settingName (your __get override will need to make sure this works)

    • You can now massively set all your configuration options as attributes:

      $model = new MyAdminFormModel;
      $model->attributes = $_POST['MyAdminFormModel'];
      

    You can easily render the whole form as a series of fields in one go

    Do this by creating a new CForm and dynamically populating its elements property (MyAdminFormModel should have all the necessary information to do this). You can then render the form easily with something like

    foreach($form->getElements() as $element) {
        echo $element->render();
    }
    

    Of 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 MyAdminFormModel gets its data from) and then everything else will automagically work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lightbox with only one image. Yes, I know its not really
[Yes I have seen this question but I do not know C nor C++,
Yes, this will be a homework (I am self-learning not for university) question but
Yes, I know this question is kind of a mouthful of everything... I have
Only a little question: I have an Entity X with an to-many relation to
How can i only output that yes or no by skiping anything? $ pactl
Yes, I have searched and tried many techniques, but nothing seems to work. Here
Yes, I know this question has been asked before, but I can't find an
Yes I have a project that I'm working on in NetBeans 7.1 and I
Yes, I know this question has been asked a lot of times, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.