Getting Any Model Class

Hello,

I’m developing a new template for crud form. The current class model is always acessed by $this->modelClass on this form. At some point i needed to use another model to generate a dropbox using data istead of just showing the foreign key. How can i get another model within this context, here goes the code:

foreach($this->tableSchema->columns as $column)

{

if($column->isPrimaryKey)


	continue;


	


if($column->isForeignKey) {


?>


<div class="row">


	<?php echo "<?php echo ".$this->generateActiveLabel($this->modelClass,$column)."; ?>\n"; ?>


	<?php echo "<?php echo CHtml::activeDropDownList(\$model,'{$column->name}', CHtml::listData(".


            [color="#FF0000"]$this->modelClass[/color]."::model()->findAll(), '{$column->name}', 'Nome')); ?>\n"; ?>


	<?php echo "<?php echo \$form->error(\$model,'{$column->name}'); echo CHtml::link('New', 'index.php?r=".strtolower(str_replace(array('id','_'),'',"{$column->name}"))."', array('target'=>'_blank'));"; ?> ?>


</div>

[color="#FF0000"]$this->modelClass[/color] should be replaced with another model i choose, how do i get another model? anything would help, getting by its name, by database’s table name… etc

Thanks.

I have the same problem… if you found any solution please share it with me.

There are some gii related extensions that deal with the relations of the model.

For example, ‘gii-template-collection’ and ‘giix’. They will come up with a dropDownList when the target column is a foreign key of a BELONGS_TO relation.

I think you can use one of them out of the box, or read the source codes of them to learn how to do it.

The links to those resources are:

Gii Template Collection

On Yii:

http://www.yiiframework.com/extension/gii-template-collection

On Google Code:

http://code.google.com/p/gii-template-collection/

Giix:

On Yii:

http://www.yiiframework.com/extension/giix

On Google Code:

http://code.google.com/p/giix/

Hi to all:

The problem is solved with the function call_user_func() of PHP. I test with metaclass, factory pattern and other but nothing work. I hope that Peter Thorun can solve his problem too. Here you have a piece of code:




<div class="row">

<?php 

$modelTemp = call_user_func($className.'::model'); 


echo $form->dropDownList($model, {$column->id}, CHtml::listData($modelTemp->findAll(), '{$column->id}', '{$column->name}'),array('prompt'=>Yii::t('NameNodule.tfile', '{$column->promptMsg}'),));


// ... the rest of code lines...

?>

</div>



I will try the solution of softark. Thanks.

Best regards.