models on the fly


anyone know how create models on the fly?

I am trying to create jqgrid for many databases and tables, so I wonder if there is a

way to not create every model.


You may want to extend CActiveRecord to prepare it to receive custom table name.

ok thank you for your advice!

lets work on it…

I dont think it is a good idea. One AR for every table? You are going to mess yourself up with that :)

I suggest the following. You create your models using the CLI of yii, thus the creation of models will not be very time consuming. Secondly you provide an interface like "IJqGridListable". In that interface you provide to methods.

interface IJqGridListable {

  public function getListAttributes();

  public function getListHeaders();


Every model you want to display with your grid must implement the interface.

The first will return a list of model attributes that will be listed in your grid. The latter will return its headers accordingly.

In your jqgrid you check if the model implements "IJqGridListable". If not you do nothing. If it does you interate through the headers to create the first row. Then you iterate through the attributes and retrieve the values from the model. Here is something from skratch.

//create header

if (count($models) == 0) {



$headers = $models[0]->getHeaders();

echo '<table>';

echo '<tr>'

foreach ($headers as $headerCol) {


  echo '<th>' . $headerCol .'</th>';


echo '</tr>';

$attributes = array();

foreach ($models as $item)

    $attributes = $item->getAttributes();

    echo '<tr>';

    foreach ($attributes as $attr) {

        echo '<td>' . $item->{$attr} . '</td>';


    echo '</tr>';


echo '</table>';

Hi Dave,

this definitely sounds much better.

I’ll implement this and see what happens.

thank you!