Pessoal,
Sou iniciante no yii tbm, e esse tópico ja me ajudou bastante, pois consigo exibir todos os campos de minhas tabelas, porém não vi nos comentarios como resolveram o problema de clicar para inserir e simplesmente não acontecer nada.
Em um relacionamento do meu banco tenho tres tabelas ‘FUNCIONARIO’, “FUNCIONARIO_DESEMPENHA_FUNCAO” e “FUNCAO”. tenho esse modelo pq a tabela “FUNCIONARIO_DESEMPENHA_FUNCAO”, contem os dados de inicio e fim da função, e todas essas informações faz ligação com uma outra tabela que efetua um atendimento, basicamente “um funcaionario que desempenha uma funcao efetua um atendimento”.
A ideia é que eu inclua os dados de FUNCIONARIO e FUNCIONARIO_DESEMPENHA_FUNCAO em uma so página.
No meu caso consegui deixar todos os itens que preciso inserir no create e no _form e apareceu na tela normalmente, ainda inseri um dropdown no campo "CODIGO_FUNCAO" para ja aparecer ao usuario as funções(Cargos) que já estão cadastradas, porém ao clicar em inserir todos so campos apagam o que está digitado e não insere no banco.
Se alguem puder me ajudar agradeço demais, pois ja quebrei muita cabeça ![:confused: :confused:](https://forum.yiiframework.com/images/emoji/twitter/confused.png?v=6)
Segue minhas páginas
FuncionarioController
<?php
class FuncionarioController extends Controller
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new FUNCIONARIO;
$modelFDF=new FUNCIONARIO_DESEMPENHA_FUNCAO;
$funcao=new FUNCAO;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['FUNCIONARIO']) && isset($_POST['FUNCIONARIO_DESEMPENHA_FUNCAO']) && isset($_POST['FUNCAO']))
{
$model->attributes=$_POST['FUNCIONARIO'];
$modelFDF->attributes=$_POST['FUNCIONARIO_DESEMPENHA_FUNCAO'];
$model->attributes=$_POST['FUNCAO'];
if($model->validate() && $modelFDF->validate() && $funcao->validate()){
$model->save();
$modelFDF->save();
$funcao->save();
}
$this->redirect(array('view','id'=>$model->MATRICULA));
}
$criteria = new CDbCriteria();
//$criteria->order = "CODIGO ASC";
$funcao = CHtml::listData(FUNCAO::model()->findAll($criteria), 'CODIGO', 'DESCRICAO');
$this->render('create',array(
'model'=>$model,
'modelFDF'=>$modelFDF,
'funcao'=>$funcao
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['FUNCIONARIO']))
{
$model->attributes=$_POST['FUNCIONARIO'];
if($model->save())
$this->redirect(array('view','id'=>$model->MATRICULA));
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('FUNCIONARIO');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new FUNCIONARIO('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['FUNCIONARIO']))
$model->attributes=$_GET['FUNCIONARIO'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer $id the ID of the model to be loaded
* @return FUNCIONARIO the loaded model
* @throws CHttpException
*/
public function loadModel($id)
{
$model=FUNCIONARIO::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param FUNCIONARIO $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='funcionario-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
Model Funcionario
<?php
/**
* This is the model class for table "FUNCIONARIO".
*
* The followings are the available columns in table 'FUNCIONARIO':
* @property string $MATRICULA
* @property string $LOTACAO
* @property string $TELEFONE
* @property string $EMAIL
* @property string $NOME
* @property string $COD_EQUIPE
*/
class FUNCIONARIO extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'FUNCIONARIO';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('MATRICULA, EMAIL, COD_EQUIPE', 'required'),
array('LOTACAO, EMAIL, NOME', 'length', 'max'=>256),
array('TELEFONE', 'length', 'max'=>11),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('MATRICULA, LOTACAO, TELEFONE, EMAIL, NOME, COD_EQUIPE', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'FK_FUNCIONARIO_FDF'=>array(self::HAS_ONE,'FUNCIONARIO_DESEMPENHA_FUNCAO','ID'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'MATRICULA' => 'Matricula',
'LOTACAO' => 'Lotacao',
'TELEFONE' => 'Telefone',
'EMAIL' => 'Email',
'NOME' => 'Nome',
'COD_EQUIPE' => 'Cod Equipe',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('MATRICULA',$this->MATRICULA,true);
$criteria->compare('LOTACAO',$this->LOTACAO,true);
$criteria->compare('TELEFONE',$this->TELEFONE,true);
$criteria->compare('EMAIL',$this->EMAIL,true);
$criteria->compare('NOME',$this->NOME,true);
$criteria->compare('COD_EQUIPE',$this->COD_EQUIPE,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return FUNCIONARIO the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
View ‘_form’ Funcionario
<?php
/**
* This is the model class for table "FUNCIONARIO".
*
* The followings are the available columns in table 'FUNCIONARIO':
* @property string $MATRICULA
* @property string $LOTACAO
* @property string $TELEFONE
* @property string $EMAIL
* @property string $NOME
* @property string $COD_EQUIPE
*/
class FUNCIONARIO extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'FUNCIONARIO';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('MATRICULA, EMAIL, COD_EQUIPE', 'required'),
array('LOTACAO, EMAIL, NOME', 'length', 'max'=>256),
array('TELEFONE', 'length', 'max'=>11),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('MATRICULA, LOTACAO, TELEFONE, EMAIL, NOME, COD_EQUIPE', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'FK_FUNCIONARIO_FDF'=>array(self::HAS_ONE,'FUNCIONARIO_DESEMPENHA_FUNCAO','ID'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'MATRICULA' => 'Matricula',
'LOTACAO' => 'Lotacao',
'TELEFONE' => 'Telefone',
'EMAIL' => 'Email',
'NOME' => 'Nome',
'COD_EQUIPE' => 'Cod Equipe',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('MATRICULA',$this->MATRICULA,true);
$criteria->compare('LOTACAO',$this->LOTACAO,true);
$criteria->compare('TELEFONE',$this->TELEFONE,true);
$criteria->compare('EMAIL',$this->EMAIL,true);
$criteria->compare('NOME',$this->NOME,true);
$criteria->compare('COD_EQUIPE',$this->COD_EQUIPE,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return FUNCIONARIO the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
View 'create’Funcionario
<?php
/* @var $this FuncionarioController */
/* @var $model FUNCIONARIO */
$this->breadcrumbs=array(
'Funcionarios'=>array('index'),
'Inserir',
);
$this->menu=array(
array('label'=>'Exibir Funcionarios', 'url'=>array('index')),
array('label'=>'Gerenciar Funcionarios', 'url'=>array('admin')),
);
?>
<h1>Inserir Funcionario</h1>
<?php $this->renderPartial('_form', array('model'=>$model, 'modelFDF'=>$modelFDF, 'funcao'=>$funcao)); ?>
model FUNCIONARIO_DESEMPENHA_FUNCAO
<?php
/**
* This is the model class for table "FUNCIONARIO_DESEMPENHA_FUNCAO".
*
* The followings are the available columns in table 'FUNCIONARIO_DESEMPENHA_FUNCAO':
* @property string $ID
* @property string $DATA_INICIO
* @property string $DATA_FIM
* @property string $MATRICULA_FUNCIONARIO
* @property string $CODIGO_FUNCAO
*/
class FUNCIONARIO_DESEMPENHA_FUNCAO extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'FUNCIONARIO_DESEMPENHA_FUNCAO';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('ID, DATA_INICIO, MATRICULA_FUNCIONARIO, CODIGO_FUNCAO', 'required'),
array('DATA_FIM', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('ID, DATA_INICIO, DATA_FIM, MATRICULA_FUNCIONARIO, CODIGO_FUNCAO', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'FK_FDF_FUNCIONARIO'=>array(self::HAS_MANY,'FUNCIONARIO','ID'),
'FK_FDF_FUNCAO'=>array(self::HAS_ONE,'FUNCAO','CODIGO_FUNCAO'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'ID' => 'ID',
'DATA_INICIO' => 'Data Início',
'DATA_FIM' => 'Data Fim',
'MATRICULA_FUNCIONARIO' => 'Matricula Funcionário',
'CODIGO_FUNCAO' => 'Código Função',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('ID',$this->ID,true);
$criteria->compare('DATA_INICIO',$this->DATA_INICIO,true);
$criteria->compare('DATA_FIM',$this->DATA_FIM,true);
$criteria->compare('MATRICULA_FUNCIONARIO',$this->MATRICULA_FUNCIONARIO,true);
$criteria->compare('CODIGO_FUNCAO',$this->CODIGO_FUNCAO,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return FUNCIONARIO_DESEMPENHA_FUNCAO the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
De qualquer forma muito obrigado