Yii框架 - 改变数据库

I just migrate to yii and i created a model which it created an CActiveRecord with gii and after that i make some changes in database then it make me confused, now my question is :

1 - Should i recreate Model with gii after any change to database ? Why Active record in yii is much complicated than other frameworks like zend or codigniter !?

Edit :

If we should not change Model Class, where should we put our database functions !!? aren't Model is there for doing so?

I think, You should ideally recreate the model after DB structural changes.

In our project, We are following the rule,

Do not make any changes in the models generated by gii. So that if there are DB changes in future, we could directly regenerate the models and will have no merging efforts.

This is also inline with philosophy of design,

**Objects** hide their data behind abstractions and expose functions that operate on that data.

**Data structures**
expose their data and have no meaningful functions. It only has methods to operate on data.

ActiveRecords(models in yii) are nothing but data structures.By adding business rules in ActiveRecords, you are mixing your data structures and objects.

You can always use Gii to preview the changes that it wants to apply to the existing model (using the diff link). Then you can apply them, but in case you have inserted custom rules inside the model (rules() method), they will be overwritten. And yes, if you do any changes to your database tables, you will have to update your CActiveRecord subclasses (your models), otherwise Yii will not be able to see your new fields or fail if it tries to access fields that it thinks it has but were deleted from the database.

Nothing stops you from writing your custom methods inside the model. But what exactly do you mean by "put our database functions"?

If I generate model with gii its only one-time job. To start quickly using this model. After that if you alter your database structure( btw you didn't tell what kind of changes you made) you can change model manually.

If you generated model via gii and didn't change model you can regenerate it again (because no manual changes made). If you already changed it there is no hard work to change it (and take journey of learning about ActiveRecord).

Just for learning create model from scratch (without gii).

EASY
1) Into your aplicacion index.php create global variable

<?php

$GLOBALS['database'] = "mydbname"; //database name

// change the following paths if necessary
$yii=dirname(__FILE__).'/../yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';

// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);
Yii::createWebApplication($config)->run();

2) Into my model create a construct method

class Employees extends CActiveRecord
{

    function __construct()
    {
        $dbname = $GLOBALS['database'];

        Yii::app()->db->setActive(false);
        Yii::app()->db->connectionString = 'mysql:host=localhost;dbname='.trim($dbname);
        Yii::app()->db->setActive(true);
    }   

3) How to use into my controller

public function actionVer3()
{
   $GLOBALS['database']="classicmodels"; //dbname
   $employee = Employees::model()->findByAttributes(array("employeeNumber"=>"1002"));

   print_r($employee);

   echo "<br><hr>";
   //$this->render("index");
}

Easy way tnx.