CREATE TABLE `tb_grupo` ( `id` int(11) NOT NULL AUTO_INCREMENT, `nome` varchar(80) DEFAULT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, `ativo` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `idx_grupos_nome` (`nome`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Grupos do sitemas' INSERT INTO tb_grupo(nome) VALUES('Coordenador'), ('Professor'), ('Aluno'), ('Administrador'); CREATE TABLE `tb_usuario` ( `id` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(80) NOT NULL, `senha` varchar(200) NOT NULL, `fk_grupo_id` int(11) NOT NULL, `ativo` int(11) DEFAULT NULL COMMENT 'Se for aluno, fica inativo até o pagamento da inscrição.', `updated_at` datetime NOT NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `idx_usuarios_email` (`email`), KEY `tb_grupos_tb_usuarios_fk` (`fk_grupo_id`), CONSTRAINT `tb_grupos_tb_usuarios_fk` FOREIGN KEY (`fk_grupo_id`) REFERENCES `tb_grupo` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='Usuarios do sistema'; CREATE TABLE `tb_dado_pessoal` ( `id` int(11) NOT NULL AUTO_INCREMENT, `nome` varchar(80) NOT NULL, `cpf` varchar(14) NOT NULL, `data_nascimento` date NOT NULL, `telefone` varchar(14) NOT NULL, `fk_usuario_id` int(11) NOT NULL, `updated_at` datetime NOT NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `idx_dados_pessoais_cpf` (`cpf`), KEY `tb_usuarios_tb_dados_pessoais_fk` (`fk_usuario_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Dados pessoais dos usuários.'
<?php class Model extends CActiveRecord {#código comum aos modelos} ?> <?php class Usuario extends Model { public static function model($className = __CLASS__) { return parent::model($className); } public function tableName() { return "dev.tb_usuario"; } public function scopes() { return array( "ativo" => array( "condition" => "ativo = 1" ), "inativo" => array( "condition" => "ativo = 0" ), "coordenador" => array( "condition" => "fk_grupo_id = 1" ), "aluno" => array( "condition" => "fk_grupo_id = 3" ), "professor" => array( "condition" => "fk_grupo_id = 2" ), "administrador" => array( "condition" => "fk_grupo_id = 4" ), ); } public function relations() { return array( "Grupo" => array(self::BELONGS_TO, "Grupo", "fk_grupo_id"), "DadoPessoal" => array(self::HAS_ONE, "DadoPessoal", "fk_usuario_id") ); } public function loadModel($id) { $model = Usuario::model()->ativo()->find("id = :id", array(":id" => $id)); if($model === null) throw new CHttpException(404, 'The requested page does not exist.'); return $model; } } ?> <?php class DadoPessoal extends Model { public static function model($className = __CLASS__) { return parent::model($className); } public function rules() { return array( array("nome", "safe", "on" => "search") ); } public function tableName() { return "dev.tb_dado_pessoal"; } public function relations() { return array( "Usuario" => array(self::BELONGS_TO, "Usuario", "fk_usuario_id"), ); } public function search() { $criteria = new CDbCriteria(); $criteria->with = array( "Usuario" => array( "scopes" => array( "ativo", "professor" ), 'limit' => 15, 'select' => array('id'), ), ); $criteria->compare('nome', $this->nome, true); $dataProvider = new CActiveDataProvider($this, array("criteria" => $criteria)); return $dataProvider; } } ?> <?php class DadoPessoalController extends Controller { public function actionProfessores() { $model = new DadoPessoal("search"); $model->unsetAttributes(); if(isset($_GET["DadoPessoal"])) $model->attributes = $_GET["DadoPessoal"]; $this->render("professores", array("model" => $model)); } } ?> #dadoPessoal/professores.php <?php /* @var this DadoPessoalController */ /* @var model DadoPessoal */ Yii::app()->clientScript->registerScript('search', " $('.search-button').click(function(){ $('.search-form').toggle(); return false; }); $('.search-form form').submit(function(){ $('#dadoPessoal-grid').yiiGridView('update', { data: $(this).serialize() }); return false; }); "); ?> <?php echo CHtml::link('Advanced Search', '#', array('class' => 'search-button')); ?> <div class="search-form" style="display: none"> <?php $this->renderPartial('_search', array('model' => $model)); ?> </div> <!-- search-form --> <?php $this->widget('zii.widgets.grid.CGridView', array( 'id' => 'dadoPessoal-grid', 'dataProvider' => $model->search(), 'filter' => $model, 'ajaxUrl' => Yii::app()->createAbsoluteUrl("dadoPessoal/professores"), 'columns' => array( 'Usuario.id', 'nome', array( 'class' => 'CButtonColumn', 'template' => '', 'buttons' => array() ), ), )); ?>
A minha intenção é que o CGridView liste apenas os usuarios que são professores, mas o CGridView está listando tudo e exibindo apenas os id dos que são professores. Como resolvo isso?
Obs: removi dos models e controller os código que não se referem a questão.
Uma questão secundária: utilizo o PostgreSQL. Como faço para o Yii entender que as minhas tabelas estão no schema
dev? Reparem a gambiarra que estou utilizando: schema.prefixo_nome_tabela.