Create An Advanced Search Form With [Through] Relation

Hi,

I am trying to create an advanced search form within the admin view / Manage [Model] interface.

I am new on the Yii framework, so for now, it does not work.

I created an "advancedsearch.php" file in my view directory. The content is the same as "_search" except that I added some input box.

I change my controller and my admin view to add this advancedsearch.

In my model, I added an advanced search method.

My ‘Genus’ Model :




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(

                        'family' => array(self::BELONGS_TO, 'Families', 'family_id'),

                        'majorGroup' => array(self::HAS_ONE, 'MajorGroups', array('ID','major_group_id'),'through'=>'family'),

                        'species' => array(self::HAS_MANY, 'Species', 'genus_id'),

                        'breedingSystems' => array(self::HAS_MANY, 'BreedingSystems', array('ID'=>'species_id'),'through'=>'species'),

                        'lifeHistoryTraits' => array(self::HAS_MANY, 'LifeHistoryTraits', array('ID'=>'species_id'),'through'=>'species'),

                );

        }






        public function advancedsearch()

        {

                // Warning: Please modify the following code to remove attributes that

                // should not be searched.


                $criteria=new CDbCriteria;


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

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

//              $criteria->compare('family_id',$this->family_id);

                $criteria->compare('family.name',$this->familyname,true);

                $criteria->compare('majorGroup.name',$this->majorgroupname,true);

                $criteria->compare('species.name',$this->speciesname,true);

                $criteria->with = array(

                        'family'=>array('select'=>'name'),

                        'majorGroup'=>array('select'=>'name'),

                        'species'=>array('select'=>'name'),

                );

                $criteria->order = "t.name, family.name";


                return new CActiveDataProvider($this, array(

                        'criteria'=>$criteria,

                ));

        }




I also declared some public variable (speciesname, majorgroupname, familyname) and I added the fields to the safe values.

My admin view changes :


Yii::app()->clientScript->registerScript('search', "

$('#search-button').click(function(){

        $('#search-form').toggle();

        return false;

});

$('#advancedsearch-button').click(function(){

        $('#advancedsearch-form').toggle();

        return false;

});

$('.search-form form').submit(function(){

        $.fn.yiiGridView.update('genus-grid', {

                data: $(this).serialize()

        });

        return false;

});

");


<?php echo CHtml::link('Great Advanced Search','#',array('class'=>'search-button','id'=>'advancedsearch-button')); ?>

<div class="search-form" id="advancedsearch-form" style="display:none">

<?php $this->renderPartial('advancedsearch',array(

        'model'=>$model,

)); ?>

I have no error when I execute my admin.php but it still does not work. For the simple search method, it works, with simple relationship.

Perhaps I missed something in my Controller, because the actionAdmin is still the same (I did not change anything in this method). ??

Any help would be greatful.

Thanks,

Regards;

Remd

I found one mistake on species relation. It works for ‘species’, if I add


'together'=>'true'

to the species relation. Otherwise, it still does not work for majorGroup.

Ok, I found how to resolve my stupid error :


'majorGroup' => array(self::HAS_ONE, 'MajorGroups', array('ID','major_group_id'),'through'=>'family'),

This was not good. The good way is :


'majorGroup' => array(self::HAS_ONE, 'MajorGroups', array('major_group_id'=>'ID'),'through'=>'family'),

Regards.