Yii Framework Forum: crudcontroller - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

crudcontroller Rate Topic: -----

#1 User is offline   kvl 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 12-February 09

Posted 14 April 2009 - 07:50 AM

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.
0

#2 User is offline   megabr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 23-March 09
  • Location:Brazil

Posted 15 April 2009 - 08:30 AM

currently have two ideias:
http://www.yiiframew...crudcontroller/
http://www.yiiframew...sion/modulegen/

maby test and work for develop more features discus...






0

#3 User is offline   kvl 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 12-February 09

Posted 17 April 2009 - 01:35 AM

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!
0

#4 User is offline   megabr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 23-March 09
  • Location:Brazil

Posted 17 April 2009 - 05:55 AM

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

.....

:)



0

#5 User is offline   kvl 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 12-February 09

Posted 17 April 2009 - 06:39 AM

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.
0

#6 User is offline   megabr 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 23-March 09
  • Location:Brazil

Posted 17 April 2009 - 06:57 AM

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,));
}


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users