M
M
Maxim2013-12-13 01:24:23
Yii
Maxim, 2013-12-13 01:24:23

How to create a model in Yii, focusing on the given parameters?

The task is this:
There are table parameters in the database (their names, column names in them), written in this form

//  ...
'params'=>array(
     'tbl_profiles'=>array(
          'tbl_name'=>'accounts',
          'tbl_fields'=>array(
               'username'=>'nickname',
               'password'=>'key',
               // ...
          )
     )
)
// ....

Based on these parameters, you need to create a model.
class Profiles extends CActiveRecord {
  extract(Yii::app()->params[tbl_profiles][tbl_fields]);
  public // что тут?
  public // и тут?
  // ...

  public function tableName() {
    return Yii::app()->params[tbl_profiles][tbl_name];
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
_
_ _, 2013-12-13
@Frapsy

You do not need to describe the structure of the table in ActiveRecord, access to the table fields in it is carried out through magic methods based on the use of table metadata.
You are just saying this model is associated with a table and $model->column = $value; will immediately work if the table has a column field.

I
Ivan Trofimov, 2013-12-13
@cbone

Try to generate a model using Gii and create your own model in the same way.

D
Dialog, 2013-12-13
@Dialog

I recently did EAV proxying in CModel and did some further validation. The implementation is simple, there is a class inherited from CModel, there is protected $data = array(), in the constructor we write data to our property and write magic __get and __set that will work with $data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question