Download the PHP package ynamics/relatedsearchbehavior without Composer
On this page you can find all versions of the php package ynamics/relatedsearchbehavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ynamics/relatedsearchbehavior
More information about ynamics/relatedsearchbehavior
Files in ynamics/relatedsearchbehavior
Package relatedsearchbehavior
Short Description Behavior making searching relations in CGridView as well as the use of relations easier
License MIT
Informations about the package relatedsearchbehavior
yii-relatedsearchbehavior
Yii Extension - RelatedSearchBehavior
- Requirements
- Forum
- Usage
- History
- Resources
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.
Uses the KeenActiveDataProvider extension to limit the number of requests to the database.
Requirements ¶
Used with Yii 1.1.12-1.1.26 versions.
Forum ¶
A Forum thread is available for this extension.
Usage ¶
To use this extension you must do 5 steps:
- "Install" the RelatedSearchBehavior;
- Update the Model's search() method;
- Add/Update the 'behaviors()' method;
- Add the search fields to the Model rules as safe fields;
- 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):
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:
by code like this (I prefer using intermediate variables like '$sort' to improve readability and increase flexibility):
In the above example, 'title' can be one of the related fields that you defined.
3. Add/Update the 'behaviors()' method ¶
Attach the behavior to the CActiveRecord and specifies the related fields.
4. Add the search fields to the Model rules as safe fields; ¶
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:
6. To use "autoScope" ¶
Autoscope allows you to search a field using a scope without declaring the scope yourself.
For instance, you can use this:
Before, you have to:
- Add RelatedSearchBehavior to the behaviors of your CActiveRecord (already done in the preceding steps,
- Add the following generic code to your CActiveRecord Model.
You are allowed to provide all the regular compare parameters:
defaults are the same as compare: autoscope(,false,"AND",false).
This is useful in complex nested conditions, not so much for simple searches like the above.
7. Using relations in CSort's attributes for sorting. ¶
'CSort' allows you to specify 'virtual attributes' for sorting as mentioned in the Yii documentation. Without RelatedSearchBehavior, you must make sure that you include the relations used in the search condition. With RelatedSearchBehavior, you do not need to take care about that - the extension takes care about it for you (since 1.16).
The preferred approach is that you'ld use attributes defined for RelatedSearchBehavior, but this might be useful in combined sort conditions:
8. exactSearchAttributes ¶
When CActiveRecord::exactSearchAttributes
is defined in a
CActiveRecord
, the attributes listed there are matched exactly by default
rather than partial. This is particularly useful in combination with
tables, and works for columns as well (not just relations). For instance,
IDs should not be partially matched, the id "1" would also match 10..19,
21, 31, etc. Requiring an exact match can also improve the performance of
the search as this is more appropriate when using an index.
Example:
In the above example:
is_active
is a boolean, so exact matches are perfect there;context_id
is an idea that matches a description in the UI;entity_id
matches some entity, also shown as a label specific to the entity;type_id
defines a type for the record, and also matches some label.
9. Tips & notes ¶
- If you like RelatedSearchBehavior, you can create or update your Gii template to generate it automatically.
- If you use 'ERememberFiltersBehavior', you must set the rememberScenario before getting the dataProvider - otherwise the relations will not be resolved in the sort clause. So you write:
This is the search method in projects where the author uses this extension. All Models extends from YActiveRecord which extends from CActiveRecord and implements several additions to default CActiveRecord features. It implements the following search method (the RelatedSearchBehavior has to be added to the model class itself):
`php
- else if (!isset($relations[$attribute])) {
- // Not a related search item -> do something else
- $this->compareProperty($attribute, $value,true);
- }
- `
History ¶
See RelatedSearchbehavior.php
for a short description of the versions