PHP code example of ynamics / relatedsearchbehavior

1. Go to this page and download the library: Download ynamics/relatedsearchbehavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ynamics / relatedsearchbehavior example snippets


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

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

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

function behaviors() {
    return array(
        'relatedsearch'=>array(
             'class'=>'RelatedSearchBehavior',
             'relations'=>array(
                  'serial'=>'device.device_identifier',
                  'location'=>'device.location.description',

                 'fieldwithdifferentsearchvalue'=>array(
                     'field'=>'device.displayname',
                     'searchvalue'=>'deviceid'
                  ),


                 'mylocalreference'=>'field.very.far.away.in.the.relation.tree',
             ),
         ),
    );
}

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

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

MyModel::model()->location('Belgium')->findAll();

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;
            }
        }
    }

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

$sort=array(
    'defaultOrder'=>'title ASC',
    'attributes'=>
        array(
            'price'=>array(
                'asc'=>'item.price',
                'desc'=>'item.price DESC',
                'label'=>'Item Price'
            ),
        ),
    );
return $this->relatedSearch(
    $criteria,
    array('sort'=>$sort)
);

$sort=array(
    'defaultOrder'=>'title ASC',
    'attributes'=>
        array(
            'groupprice'=>array(
                'asc'=>'item.group, item.price',
                'desc'=>'item.group DESC, item.price DESC',
                'label'=>'Item Price'
            ),
        ),
    );
return $this->relatedSearch(
    $criteria,
    array('sort'=>$sort)
);

public function exactSearchAttributes() {
    return [
        'is_active',
        'context_id',
        'entity_id',
        'type_id',
    ];
}

$model->rememberScenario="admin";
$dataProvider=$model->search();

/**
 * Search method valid for all Active Records (with RelatedSearchBehavior)
 *
 * @return KeenActiveDataProvider
 */
public function search() {
    $criteria=new CDbCriteria();
    $t=$this->getTableAlias(false, false);
    $ds=$this->getDbConnection()->getSchema();

    $columns=$this->getMetaData()->columns;
    //$relations=$this->relations;
    foreach ($this->getSafeAttributeNames() as $attribute) {
        $value=$this->{$attribute};
        if ($value==='=') {
            $value=array(null,'');
        }
        if (is_array($value)&&!empty($value)||(!is_array($value)&&"$value" !== "")) {
            if (isset($columns[$attribute])) {
                if (in_array($attribute,$this->exactSearchAttributes())) {
                    Yii::trace("Exact match 

         */
        }
    }
    // $criteria->together=true;

    return $this->relatedSearch($criteria
        //,array('sort'=>array('defaultOrder'=>$this->getTableAlias(true,false).'.entity_id DESC'))
    );
}

/**
 * Provides the list of attributes for which an exact search is
 * needed and not a partial search.
 * (typical: keys, enums, etc.
 * @return string[]
 */
public function exactSearchAttributes() {
    return [];
}