how to show new added columns in database in yii f

2019-07-23 16:54发布

问题:

i added two new columns in my db table, and then i defined these new columns in the table model itself, and then, when i called

$model->getAttributes()

the two new columns didn't appear in the array output of $model->getAttributes() method call

there's no schema caching set in my configs, any idea how to solve this?, and how am I gonna get the value of the input forms of the new added columns from the front-end if i have a problem in showing off the two newly added columns ?

e.g

new columns INHOUSE OUTHOUSE

//view

 <?php echo $form->checkBox($model,'INHOUSE',array("id"=>"inhouse","value"=>1, "uncheckValue"=>0));?>
 <?php echo $form->checkBox($model,'OUTHOUSE',array("id"=>"outhouse","value"=>1, "uncheckValue"=>0));?>

//controller update action

i tried to save the value of the INHOUSE and OUTHOUSE didn't get saved when i do

$model->attributes = $_POST['users'];
$model->save();

NOR

$model->INHOUSE = $_POST['users']['INHOUSE'];
$model->OUTHOUSE = $_POST['users']['OUTHOUSE'];
$model->save;

any ideas how to solve all those problems?

回答1:

1-Can you insert data directly into your DB on those columns? 2-Can you view those columns on the admin view or on any other view? 3-Can you retrieve those values using a getColumn method?

If not, try this:

Make sure that you have the new columns specified in your model and view

Model_Name.php

public function attributeLabels()
{
    return array(
        'id' => 'ID', // example
        'new_column' => 'New Column',
        ...
    );
}

You should also check from TOP to bottom and as a good practice you should also type this on the top of the model

 * @property integer $id //example
 * @property data_type $new_column
...

Then to view your new columns you need to add them to the necessary views.

example

VIEW.PHP

$this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
         'id',
         'new_column',
          ...

If you have your model with all these definitions you should be able to retrieve those values from the DB, re-check any custom functions that you may have.



回答2:

Please goto to : protected/runtime folder and delete all files. And try again.



标签: php yii