Yii2 framework backup
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

<?php
11 years ago
use yii\helpers\StringHelper;
/**
11 years ago
* This is the template for generating a CRUD controller class file.
*
11 years ago
* @var yii\web\View $this
11 years ago
* @var yii\gii\generators\crud\Generator $generator
*/
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";
?>
namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;
11 years ago
use yii\base\Model;
use yii\data\ActiveDataProvider;
use <?= ltrim($generator->modelClass, '\\') ?>;
11 years ago
/**
* <?= $searchModelClass ?> represents the model behind the search form about <?= $modelClass ?>.
11 years ago
*/
class <?= $searchModelClass ?> extends Model
11 years ago
{
public $<?= implode(";\n\tpublic $", $searchAttributes) ?>;
11 years ago
public function rules()
{
return [
<?= implode(",\n\t\t\t", $rules) ?>,
];
11 years ago
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
11 years ago
<?php foreach ($labels as $name => $label): ?>
<?= "'$name' => '" . addslashes($label) . "',\n" ?>
11 years ago
<?php endforeach; ?>
];
11 years ago
}
public function search($params)
{
$query = <?= $modelClass ?>::find();
$dataProvider = new ActiveDataProvider([
11 years ago
'query' => $query,
]);
11 years ago
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
<?= 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) {
$value = '%' . strtr($value, ['%'=>'\%', '_'=>'\_', '\\'=>'\\\\']) . '%';
$query->andWhere(['like', $attribute, $value]);
11 years ago
} else {
$query->andWhere([$attribute => $value]);
11 years ago
}
}
}