Yii 1.1: relatedsearchbehavior

Behavior making searching relations in CGridView easier
26 followers

Creating CGridViews with related tables or getting fields from related tables is simplified with this behavior. It magically adds the needed 'with' clauses and provides aliases to fields of records in the relations.

Live demo

Uses the KeenActiveDataProvider extension to limit the number of requests to the database.

Requirements

Developped with Yii 1.1.12 - surely useable with earlier and later versions.

Forum

A Forum thread is available for this extension.

Usage

To use this extension you must do 5 steps:

  1. "Install" the RelatedSearchBehavior;
  2. Update the Model's search() method;
  3. Add/Update the 'behaviors()' method;
  4. Add the search fields to the Model rules as safe fields;
  5. Use the search field as any other field in the CGridView.

Here are the details for these steps:

1. "Install" the RelatedSearchBehavior;

Installation is as easy as adding the location to the 'import' configuration of the Yii configuration file (main.php):

return array(
         [...]
    'import'=>array(
            [...]
            'ext.KeenActiveDataProvider',
        'ext.RelatedSearchBehavior',
    ),

In the above the RelatedSearchBehavior file was placed in the extensions directory.

2. Update the Model's search() method;

Update the overload of ActiveRecord::search(). The following example shows how you can set the default search order. You can set any parameter accepted by CActiveDataProvider (such as pagination) in the second array. So, replace code like this:

return new CActiveDataProvider($this, array(
  'criteria'=>$criteria,
  'sort'=>array(
    'defaultOrder'=>'title ASC',
  )
));

by code like this (I prefer using intermediate variables like '$sort' to improve readability and increase flexibility):

$sort=array(
    'defaultOrder'=>'title ASC',
);
return $this->relatedSearch(
    $criteria,
    array('sort'=>$sort)
);

In the above example, 'title' can be one of the relations that you defined.

3. Add/Update the 'behaviors()' method

Attach the behavior to the CActiveRecord and specifies the related fields.

function behaviors() {
    return array(
        'relatedsearch'=>array(
             'class'=>'RelatedSearchBehavior',
             'relations'=>array(
                  'serial'=>'device.device_identifier',
                  'location'=>'device.location.description',
                   // Field where search value is different($this->deviceid)
                 'fieldwithdifferentsearchvalue'=>array(
                     'field'=>'device.displayname',
                     'searchvalue'=>'deviceid'
                  ),
                 // Next line describes a field we do not search,
                 // but we define it here for convienience
                 'mylocalreference'=>'field.very.far.away.in.the.relation.tree',
             ),
         ),
    );
}

4. Add the search fields to the Model rules as safe fields;

public function rules()
    {
        return array(
            [...]
            array('serial,location,deviceid','safe','on'=>'search'),
        );
    }

5. Use the search field as any other field in the CGridView.

For the CGridView column specification, you can then just put 'serial' for the column (no need to do 'name'=>..., 'filter'=>..., 'value'=>... .

Example:

$this->widget('zii.widgets.grid.CGridView', array(
  [...]
  'columns'=>array(
      [...]
      'serial',
      'location',
  )
));

6. To use "autoScope"

Autoscope allows you to search a field using a scope without declaring the scope yourself.

For instance, if your Model has a field 'username', you can use this:

MyModel::model()->username('me')->findAll();

Before, you have to: 1. Add RelatedSearchBehavior to the behaviors of your CActiveRecord (already done in the preceding steps, 2. Add the following code to your CActiveRecord Model.

/**
     * Add automatic scopes for attributes (uses RelatedSearchBehavior).
     */
    public function __call($name,$parameters) {
        try {
            return parent::__call($name,$parameters);
        } catch (CException $e) {
            if(preg_match(
                    '/'.Yii::t(
                            'yii',
                            quotemeta(
                                    Yii::t(
                                            'yii',
                                            '{class} and its behaviors do not have a method or closure named "{name}".'
                                            )
                                    ),
                                    array('{class}'=>'.*','{name}'=>'.*')
                            )
                    .'/',$e->getMessage())) {
                return $this->autoScope($name, $parameters);
            } else {
                throw $e;
            }
        }
    }

You are allowed to provide all the regular compare parameters:

MyModel::model()->username($searchvalue,$partialMatch,$operator,$escape)->findAll();

defaults are the same as compare: autoscope(,false,"AND",false).

This is usefull in complex nested conditions, not so much for simple searches like the above.

7. Tip

If you like RelatedSearchBehavior, you can create or update your Gii template to generate it automatically.

History

 * 1.03  Quoting relations in database.
 * 1.04  Added autoScope.
 *       Added option 'partialMatch' for relation.
 * 1.05  Enable multiple attributes in default sort.
 * 1.06  Fix to autoScope - return owner (chaining) + correct example in comment.
 * 1.07  Improved compatibility with ERememberFiltersBehavior ("standard" sort key).
 * 1.08  Fix in KeenActiveDataProvider for postgresql + updates to demo for postgresql.
 * 1.09  Allow array as search value.
 * 1.10  Use alias defined in model's relation (Svobik7)
 * 1.11  Corrected test of owner class type (lower case 'c') and improved error message for autoscope.

Resources

KeenActiveDataProvider

Total 20 comments

#18291 report it
le_top at 2014/10/12 12:26pm
"Recursive" relations

My local version (1.14) now looks "recursively" for relations. I.e.: when referencing a relation field that is itself a relation, it will be "expanded" too. Further, joins for "deep" relations were not always added and this is fixed too. If you want to test, let me know.

#18267 report it
le_top at 2014/10/08 04:59pm
'addCondition' for relation

Hi I just extended RelatedSearchBehavior in my "private" copy with 'autoscope' for relations and 'addRelatedCondition' to add a condition on a relation easily.

I am only releasing it in the near future if there is any real interest for that, otherwise it will be with some potential other future update.

#14506 report it
le_top at 2013/08/18 04:57pm
Allow array for search value

I updated the extension to allow arrays for the search value. I did not test with arrays, I only tested that the demo still works.

The test implemented previously was designed after the way the search method was generated with the default Gii generator.

#14493 report it
zvik2004 at 2013/08/17 11:43am
Fail when using array as a condition

Hi,
I tried to set an array to a ('related search') attribute and it failed in the line (because it's an array and not a string):

if("$search_value"!=="") {

I see that you check in the next line if it's an object:

if(!is_object($search_value)) {

Maybe you should move this check up?

Thanks again for great extension!
Zvi (keen user ;-)).

#13810 report it
le_top at 2013/06/27 05:13pm
Update for ERememberFiltersBehavior & sorting

To the followers: I just released an update so that ERememberFiltersBehavior can still rely on the expected naming of the sort $_GET key.

#13205 report it
le_top at 2013/05/14 02:33am
Discussion continued on forum

Please check the forum for the continuation of the discussion.

#13204 report it
hbalkhi at 2013/05/14 01:06am
Pagination issue

@le_top Thanks for taking time to reply , actually that's what i was planning to do but i thought maybe somebody faced the same issue ..anyway i found out that the the three order by clauses in the query (the outer and inner data sets query)that pick up the data are missing and i have no idea why ...

#13202 report it
le_top at 2013/05/13 07:26pm
Pagination issue

Hi In the demo the pagination behavior looks normal to me (using sqlite) and also on a production site (using mysql). This may have something to do with your gridview wrapper. I suggest that you check out the demo and "migrate" it to your bootstrap wrapper (using a new view). I think that this will help discover where the issue may be. Also check the SQL searches/requests made (using yiidebugtb extension for instance).

#13200 report it
hbalkhi at 2013/05/13 03:40pm
Pagination issue

thanks for this great extension..

i wanted to know if anybody facing issues with pagination where the data rows stay the same if you search using one of the related fields or sort by one of them and i noticed also that the indicators of the current page are changing but the data staying the same

everything works normally if i search using the fields of the same model i'm using crisu bootstrap grid and

appreciate any help

#13052 report it
le_top at 2013/04/30 02:13pm
Multi-attribute support for defaultOrder

Follow a request on the forum, I added multi-attribute defaultOrder support. So now the default order can be like this:

'sort'=>array('defaultOrder'=>'Artist DESC,TrackName ASC'),

where Artist and TrackName correspond to virtual attributes referring to fields in related tables (Related Search Behavior updates the defaultOrder as needed).

#12930 report it
le_top at 2013/04/21 12:57pm
Updated with partialMatch & autoScope

@zvik2004: I see that you are developing a relation as a keen user ;-).

I have added the partialMatch code (slightly differently), and 'autoScope' a feature that I started using myself.

#12927 report it
zvik2004 at 2013/04/21 07:33am
Great extension! (2) :-) - add parameter partialMatch

Hi

In your search, you use partialMatch=true. I think it will be great to get it as a parameter. I modified my code, please let me know what you think:

The configuration can be something like:

'fieldxyz'  => array(
    'field'         => 'table.field',
    'partialMatch'  => false,
),

And in the RelatedSearchBehavior file (in the appropriate places):

if(is_array($relationvar)) {
    $relationfield=$relationvar['field'];
    $search_value = isset($relationvar['searchvalue']) ? $this->getOwner()->{$relationvar['searchvalue']} : $this->getOwner()->{$var};;
    $partialMatch = isset($relationvar['partialMatch']) ? $relationvar['partialMatch'] : true;
} else {
    $relationfield=$relationvar;
    $search_value=$this->getOwner()->{$var};
    $partialMatch = true;
}
 
//and the search part:
$criteria->compare($column,$search_value,$partialMatch);

Thanks!

Zvi.

#12803 report it
le_top at 2013/04/12 04:10pm
? Your feedback on potential extension of RelatedSearchBehavior

Please check the Forum regarding a potential new feature to automatically add scopes for attributes.

#12647 report it
le_top at 2013/04/03 08:45am
? Why just one field

Hi Just one field because of the work (writing/testing) involved. Ideally, the code would accept multifield ordering in a single string specification or in an array specification. I am happy to integrate any full code.

#12645 report it
gb5256 at 2013/04/03 08:30am
Sorting on related record

Hi, thanks for extending your extension to work also with the default sort. Perfect. Just for curiosity: Why only for one field? Is this getting too complex otherwise? Don't get me wrong, I am totally happy with this, but I can think of cases where I need to do this for more than one field.

gb

#12615 report it
le_top at 2013/04/01 07:45pm
Sorting on related record

Hi I extended the extension so that you can define 1 related field for the defaultOrder. I have updated the demo (Invoiceline.php) to sort on 'Artist DESC' which is a related field.

#12612 report it
gb5256 at 2013/04/01 05:06pm
Sorting by RelatedRecord?

Thanks for providing this extension. Works very well. I right now want to do a sort on one of the related records. So to follow your example, this does not work for me:

$sort=array(
    'defaultOrder'=>'serial ASC',
);
return $this->relatedSearch(
    $criteria,
    array('sort'=>$sort)
);

Is there any work around for this? Thanks again, gb

#12454 report it
le_top at 2013/03/21 04:03pm
Demo

Hi The demo has no drop-down filters - it has the default filter function for gridviews - you fill in text. Using drop-downs for filtering is a different feature - I'll see where this is suitable to add.

I probably modified the KeenActiveDataProvider to improve it. I think I submitted my improvements, but not sure of it.

#12452 report it
maxtorchel at 2013/03/21 02:32pm
Field where search value is different

can you show example, how to setup it? I can`t understand how it works. demo has no dropdown filter.

ps your behavior not work with KeenActiveDataProvider.php from github, only work from demo.

#12435 report it
le_top at 2013/03/20 10:26am
Demo

The demo is available on line under 'Live demo' and you can download the demo too. That is not ok for you?

Leave a comment

Please to leave your comment.

Create extension