crudcontroller

Some ideas about <<subject>>?

I've just created it for some specific case, when one needs to manipulate with large amount of simple reference tables of the same structure.

currently have two ideias:

http://www.yiiframew…crudcontroller/

http://www.yiiframew…sion/modulegen/

maby test and work for develop more features discus…

Thank you for reply!

Of course, it doesn't fit to all.

Next version (if will be…) is suggested to extract primary keys only from model::tableShema and will take names of the displayed fields in relational tables from array, given by user.

Also, have some ideas about model names - according your notices, thanks!

Quote

is suggested to extract primary keys only from model::tableShema 

ummm in that case maby build a function for search for each element from the current table to find if have a primary key name with the same name in a database tables…!

ex: table user

user_id

user

group_id

emai

unset(user_id)

foreach table user:

find parent(user)

find parent(group_id)

if found, include in the relational array and build a select for create and update, find the name for view and admin lists

:)

Good idea - but my aim is to avoid any name agreements, if posiible.

By the same way one can find relational tables recursively from tableSchema - using tableSchema->foreinKeys array, or tableSchema->columns[$n]->isForeignKey.

currently this is my controller generator






	public function generateController($source,$params)


	{





		list($controllerClass,$modelClass,$moduleID)=$params;


        $MenuLink=$moduleID.'/'.$modelClass; 


		$model=CActiveRecord::model($modelClass);


		$id=$model->tableSchema->primaryKey;





                $select='';$data='data';$criteriax='criteriax';





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


				{








				    $col=explode('_',$column->name);


                    if($col[1]=='id' AND $column->name!=$id)


                     {


					 $select.="nttt ${$criteriax} = new CDbCriteria;";


					 $select.="nttt ${$criteriax}->order='{$col[0]}';";


					 $select.="nttt ${$data}      = {$col[0]}::model()->findAll(${$criteriax});";


					 $select.="nttt ${$modelClass}->{$col[0]}_id=${$modelClass}->toHtmlSelect(${$data},'{$col[0]}',${$modelClass}->{$col[0]}->{$col[0]}_id);";





                     }


                }





				$modelx=CActiveRecord::model($modelClass);


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


				{


                $line=explode('enum',$column->dbType);


                if($line[1])


				{


                $select.="nttt ${$modelClass}->{$column->name}=opcoes('{$modelClass}','{$column->name}',${$modelClass}->{$column->name});";  


				}





                }








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


				{








				    $col=explode('_',$column->name);


                    if($col[1]=='id' AND $column->name!=$id)


                     {


					 $select.="nttt ${$criteriax} = new CDbCriteria;";


					 $select.="nttt ${$criteriax}->order='{$col[0]}';";


					 $select.="nttt ${$data}      = {$col[0]}::model()->findAll(${$criteriax});";


					 $select.="nttt ${$modelClass}->{$col[0]}_id=${$modelClass}->toHtmlSelect(${$data},'{$col[0]}',${$modelClass}->{$col[0]}->{$col[0]}_id);";





                     }


                }





			 // $medico->uf=opcoes('medico','uf',$medico->uf);


			 // echo $select;





		if($id===null)


			throw new ShellException(Yii::t('yii','Error: Table "{table}" does not have a primary key.',array('{table}'=>$model->tableName())));


		else if(is_array($id))


			throw new ShellException(Yii::t('yii','Error: Table "{table}" has a composite primary key which is not supported by crud command.',array('{table}'=>$model->tableName())));





		if(!is_file($source))  // fall back to default ones


			$source=YII_PATH.'/cli/views/shell/crud/'.basename($source);





		$content=file_get_contents($source);


		return strtr($content,array(


			'{ClassName}'=>$controllerClass,


			'{ID}'=>$id,


			'{ModelClass}'=>$modelClass,


			'{ModelVar}'=>strtolower($modelClass),


			'{Select}'=>$select,


			'{ModelName}'=>strtolower($modelClass),


			'{MenuLink}'=>$MenuLink));


	}





and the controller view template:



	public function actionShow()


	{


        $menux=geraMenu('{MenuLink}');


		$this->render('show',array('{ModelVar}'=>$this->load{ModelClass}(),'menux'=>$menux));


	}





	/**


	 * Creates a new {ModelVar}.


	 * If creation is successful, the browser will be redirected to the 'show' page.


	 */


	public function actionCreate()


	{


		${ModelVar}=new {ModelClass};


		if(isset($_POST['{ModelClass}']))


		{


			${ModelVar}->attributes=$_POST['{ModelClass}'];


			if(${ModelVar}->save())


				$this->redirect(array('show','id'=>${ModelVar}->{ID}));


		}


		{Select}


        $menux=geraMenu('{MenuLink}');


		$this->render('create',array('{ModelVar}'=>${ModelVar},'menux'=>$menux));


	}





	/**


	 * Updates a particular {ModelVar}.


	 * If update is successful, the browser will be redirected to the 'show' page.


	 */








	public function actionUpdate()


	{


		${ModelVar}=$this->load{ModelClass}();





		if(isset($_POST['{ModelClass}']))


		{


			${ModelVar}->attributes=$_POST['{ModelClass}'];


			if(${ModelVar}->save())


				//$this->redirect(array('show','id'=>${ModelVar}->{ID}));


                $this->redirect(Yii::app()->getRequest()->getUrl());





		}





        {Select}





        $menux=geraMenu('{MenuLink}');


		$this->render('update',array(


             '{ModelVar}'=>${ModelVar},


             'menux'=>$menux,));


	}