You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.9 KiB
84 lines
1.9 KiB
11 years ago
|
<?php
|
||
11 years ago
|
|
||
|
use yii\helpers\StringHelper;
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* This is the template for generating a CRUD controller class file.
|
||
|
*
|
||
|
* @var yii\base\View $this
|
||
|
* @var yii\gii\generators\crud\Generator $generator
|
||
11 years ago
|
*/
|
||
11 years ago
|
|
||
|
$modelClass = StringHelper::basename($generator->modelClass);
|
||
|
$searchModelClass = StringHelper::basename($generator->searchModelClass);
|
||
|
$rules = $generator->generateSearchRules();
|
||
|
$labels = $generator->generateSearchLabels();
|
||
|
$searchAttributes = $generator->getSearchAttributes();
|
||
|
$searchConditions = $generator->generateSearchConditions();
|
||
|
|
||
|
echo "<?php\n";
|
||
|
?>
|
||
|
|
||
11 years ago
|
namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;
|
||
11 years ago
|
|
||
|
use yii\base\Model;
|
||
|
use yii\data\ActiveDataProvider;
|
||
11 years ago
|
use <?= ltrim($generator->modelClass, '\\') ?>;
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* <?= $searchModelClass ?> represents the model behind the search form about <?= $modelClass ?>.
|
||
11 years ago
|
*/
|
||
11 years ago
|
class <?= $searchModelClass ?> extends Model
|
||
11 years ago
|
{
|
||
11 years ago
|
public $<?= implode(";\n\tpublic $", $searchAttributes) ?>;
|
||
11 years ago
|
|
||
|
public function rules()
|
||
|
{
|
||
11 years ago
|
return [
|
||
11 years ago
|
<?= implode(",\n\t\t\t", $rules) ?>,
|
||
11 years ago
|
];
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* @inheritdoc
|
||
|
*/
|
||
|
public function attributeLabels()
|
||
|
{
|
||
11 years ago
|
return [
|
||
11 years ago
|
<?php foreach ($labels as $name => $label): ?>
|
||
11 years ago
|
<?= "'$name' => '" . addslashes($label) . "',\n" ?>
|
||
11 years ago
|
<?php endforeach; ?>
|
||
11 years ago
|
];
|
||
11 years ago
|
}
|
||
|
|
||
|
public function search($params)
|
||
|
{
|
||
11 years ago
|
$query = <?= $modelClass ?>::find();
|
||
11 years ago
|
$dataProvider = new ActiveDataProvider([
|
||
11 years ago
|
'query' => $query,
|
||
11 years ago
|
]);
|
||
11 years ago
|
|
||
|
if (!($this->load($params) && $this->validate())) {
|
||
|
return $dataProvider;
|
||
|
}
|
||
|
|
||
11 years ago
|
<?= implode("\n\t\t", $searchConditions) ?>
|
||
11 years ago
|
|
||
|
return $dataProvider;
|
||
|
}
|
||
|
|
||
|
protected function addCondition($query, $attribute, $partialMatch = false)
|
||
|
{
|
||
|
$value = $this->$attribute;
|
||
|
if (trim($value) === '') {
|
||
|
return;
|
||
|
}
|
||
|
if ($partialMatch) {
|
||
11 years ago
|
$value = '%' . strtr($value, ['%'=>'\%', '_'=>'\_', '\\'=>'\\\\']) . '%';
|
||
|
$query->andWhere(['like', $attribute, $value]);
|
||
11 years ago
|
} else {
|
||
11 years ago
|
$query->andWhere([$attribute => $value]);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|