Question

What is the relation or difference between ActiveRecord and model in YII ?

i was trying to log is_object(CActiveRecord::model('Project')); and was expecting false but it returned true;

Since the logging indicated that it's an object, i thought it is representing a row in the table, but i couldn't find any attributes that represent the coloumns.

Also http://www.yiiframework.com/doc/api/1.1/CActiveRecord#model-detail states that it's returning an instance of CActiveRecord class , but i could not find any values of the table row in that object.

Was it helpful?

Solution

The answer is in your documentation link, model() is a class level method, and it:

Returns the static model of the specified AR class. The model returned is a static instance of the AR class. It is provided for invoking class-level methods (something similar to static class methods.)

Let's say you do: $model=CActiveRecord::model('Project'); , then using that $model you can call all the class level methods of CActiveRecord, like:

$allModels = $model->findAll(); // will give you all the models of Project
$someModel = $model->findByPk('pkValue'); // will give you the row with primary key value = pkValue 
$model->deleteAll(); // will delete all the records of Project
// and so on

Edit:

Also this post in the forum says: (Difference between class level and static methods)

Class Level Methods are effectively Static Methods BUT with the benefit of being able to use inheritance. That is, you can override the operation of a Class Level Method in a subclass, whereas if you used a static method you would not be able to override it. .... So, in general, you should use class level methods, not static methods, as it gives you the benefit of inheritance although it might feel a little weird. Then you call them using $class::model()->method().

OTHER TIPS

ActiveRecord is a pattern. A pattern to store data in relational database. Model, in MVC pattern, is the part of data. So, Yii is an MVC framework that implement ActiveRecord for model.

model method is this

public static function model($className=__CLASS__)
{
    if(isset(self::$_models[$className]))
        return self::$_models[$className];
    else
    {
        $model=self::$_models[$className]=new $className(null);
        $model->_md=new CActiveRecordMetaData($model);
        $model->attachBehaviors($model->behaviors());
        return $model;
    }
}

As you can see return an object

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top