Relaciones Entre Tablas Con Bd Postgres

Buenas, soy nuevo en Yii y necesito ayuda, disculpen si este tema ha sido tratado anteriormente pero no consigo dar con la solucion.

Tengo una Base de datos de un directorio

mi tabla tbl_estructura tiene un campo idtipoestructura (FK) de la tabla tbl_tipoestructura desde donde mediante el id, se trae la denominacion del tipo de estructura

(en la tabla estructura tambien existe un campo llamado denominacion) no se si tendrá algo que ver pero por ahora tengo esto, se supone que deberia poder hacer busquedas

por tipo de estructura asi como por denominacion de la tabla tbl_estructura. algun tipo de filtrado para posteriormente llegar a algo mas… por ahora solo necesito eso

pero todo esta de cabeza y ya no se que hacer.

En el modelo Estructura.php tengo esto:




public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('idpadre, idtipoestructura, idparroquia, idmaximaautoridad, denominacion, ubicacionfisica, paginaweb, telefono', 'required'),

			array('idpadre, idtipoestructura, idparroquia, idmaximaautoridad, codigoonapre, estatusreg', 'numerical', 'integerOnly'=>true),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('id, idpadre, idtipoestructura0, idtipoestructura, idparroquia, idmaximaautoridad, codigoonapre, denominacion, ubicacionfisica, paginaweb, telefono, estatusreg', 'safe', 'on'=>'search'),

		);

	}


	/**  idtipoestructura0.denominacion,

	 * @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(

			'tblSeguimientoproyectos' => array(self::HAS_MANY, 'TblSeguimientoproyecto', 'idestructura'),

			'idparroquia0' => array(self::BELONGS_TO, 'TblParroquia', 'idparroquia'),

			'idmaximaautoridad0' => array(self::BELONGS_TO, 'TblMaximaautoridad', 'idmaximaautoridad'),

			'idtipoestructura0' => array(self::BELONGS_TO, 'TblTipoestructura', 'idtipoestructura'),

			'tblDependenciases' => array(self::HAS_MANY, 'TblDependencias', 'idestructura'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'numero identificador de estructura\r\n',

			'idpadre' => 'Id Padre',

			'idtipoestructura' => 'id tipo de Estructura',


			'idtipoestructura0.denominacion' => 'Tipo de Organismo',


			'idparroquia' => 'Parroquia',

			'idmaximaautoridad' => 'Idmaximaautoridad',

			'codigoonapre' => 'COD ONAPRE',

			'denominacion' => 'Nombre de la estructura',

			'ubicacionfisica' => 'direccion.',

			'paginaweb' => 'direccion de la pagina web',

			'telefono' => 'numero de telefono',

			'estatusreg' => 'estatus en el que encuentra el registro (activo - inactivo)',

		);

	}






en el views del admin(donde se hacen los filtrados) tengo esto:


<?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'=>'estructura-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		//'id',

		'idpadre',

		//'idtipoestructura',

		'idparroquia',

		//'idmaximaautoridad',

		'codigoonapre',

		'denominacion',

		//'ubicacionfisica',

		//'paginaweb',

		//'telefono',

		//'estatusreg',

		 array(

		   'name'=>'idtipoestructura0.denominacion',	

		   'value'=>'$data->idtipoestructura0->denominacion'),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



y en el search esto:




<div class="row">

		<?php echo $form->label($model,'idtipoestructura0'); ?>

		<?php echo $form->textField($model,'idtipoestructura0'); ?>

</div>



se que debe ser una tonteria pero es primera vez que trabajo con esto. disculpen las molestias

Puedes agregar el codigo de tu metodo Search (Model-Estructura) y el del Controller referente a la busqueda?

<_<

Perdón pense que lo habia puesto.


public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


		// search with 'idt1' relation

		$criteria->with = array('idtipoestructura0');

    		$criteria->together = true;




		$criteria->compare('id',$this->id);

		$criteria->compare('idpadre',$this->idpadre);

		$criteria->compare('idtipoestructura',$this->idtipoestructura);

		$criteria->compare('idparroquia',$this->idparroquia);

		$criteria->compare('idmaximaautoridad',$this->idmaximaautoridad);

		$criteria->compare('codigoonapre',$this->codigoonapre);

		$criteria->compare('denominacion',$this->denominacion,true);


		$criteria->compare('idtipoestructura0.denominacion',$this->denominacion,true);


		$criteria->compare('ubicacionfisica',$this->ubicacionfisica,true);

		$criteria->compare('paginaweb',$this->paginaweb,true);

		$criteria->compare('telefono',$this->telefono,true);

		$criteria->compare('estatusreg',$this->estatusreg);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

y el controller, pero no he visto en los tutoriales ningun cambio en el


<?php


class TblEstructuraController 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 TblEstructura;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['TblEstructura']))

		{

			$model->attributes=$_POST['TblEstructura'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}


	/**

	 * 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['TblEstructura']))

		{

			$model->attributes=$_POST['TblEstructura'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$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('TblEstructura');

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

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new TblEstructura('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['TblEstructura']))

			$model->attributes=$_GET['TblEstructura'];


		$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 TblEstructura the loaded model

	 * @throws CHttpException

	 */

	public function loadModel($id)

	{

		$model=TblEstructura::model()->findByPk($id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param TblEstructura $model the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='tbl-estructura-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

}

Si lo que quieres es buscar los idtipoestructura que coincidan con los datos que capturas, te falta pasar el valor (FK) de la estructura a buscar, esto deberia ser algo como:




<!-- view -->

<div class="row">

    <?php echo $form->label($model,'idtipoestructura'); ?>

    <?php echo $form->textField($model->,'idtipoestructura'); ?>

</div>



En tu metodo search




$criteria=new CDbCriteria;

$criteria->with = array('idtipoestructura0');

$criteria->together = true;

$criteria->compare('t.idtipoestructura',$this->idtipoestructura);

return new CActiveDataProvider($this, array(

        'criteria'=>$criteria,

));



Esto te trae los datos referentes a ese tipo de estructura (FK)y los muestras tal y como lo haces en el GridView (‘value’=>’$data->idtipoestructura0->denominacion’))

Es lo que buscas?

:unsure:

Dejame saber

Checate este link y ahi encontraras la solucion.

CGridView custom filter

;D