Qiang Xue
11 years ago
12 changed files with 362 additions and 143 deletions
@ -1,8 +1,83 @@
|
||||
<?php |
||||
|
||||
use yii\helpers\StringHelper; |
||||
|
||||
/** |
||||
* Created by JetBrains PhpStorm. |
||||
* User: qiang |
||||
* Date: 8/26/13 |
||||
* Time: 5:22 PM |
||||
* To change this template use File | Settings | File Templates. |
||||
* This is the template for generating a CRUD controller class file. |
||||
* |
||||
* @var yii\base\View $this |
||||
* @var yii\gii\generators\crud\Generator $generator |
||||
*/ |
||||
|
||||
$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 <?php echo StringHelper::dirname(ltrim($generator->searchModelClass, '\\')); ?>;
|
||||
|
||||
use yii\base\Model; |
||||
use yii\data\ActiveDataProvider; |
||||
use <?php echo ltrim($generator->modelClass, '\\'); ?>;
|
||||
|
||||
/** |
||||
* <?php echo $searchModelClass; ?> represents the model behind the search form about <?php echo $modelClass; ?>.
|
||||
*/ |
||||
class <?php echo $searchModelClass; ?> extends Model
|
||||
{ |
||||
public $<?php echo implode(";\n\tpublic $", $searchAttributes); ?>;
|
||||
|
||||
public function rules() |
||||
{ |
||||
return array( |
||||
<?php echo implode(",\n\t\t\t", $rules); ?>,
|
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
*/ |
||||
public function attributeLabels() |
||||
{ |
||||
return array( |
||||
<?php foreach ($labels as $name => $label): ?> |
||||
<?php echo "'$name' => '" . addslashes($label) . "',\n"; ?> |
||||
<?php endforeach; ?> |
||||
); |
||||
} |
||||
|
||||
public function search($params) |
||||
{ |
||||
$query = <?php echo $modelClass; ?>::find();
|
||||
$dataProvider = new ActiveDataProvider(array( |
||||
'query' => $query, |
||||
)); |
||||
|
||||
if (!($this->load($params) && $this->validate())) { |
||||
return $dataProvider; |
||||
} |
||||
|
||||
<?php echo implode("\n\t\t", $searchConditions); ?> |
||||
|
||||
return $dataProvider; |
||||
} |
||||
|
||||
protected function addCondition($query, $attribute, $partialMatch = false) |
||||
{ |
||||
$value = $this->$attribute; |
||||
if (trim($value) === '') { |
||||
return; |
||||
} |
||||
if ($partialMatch) { |
||||
$value = '%' . strtr($value, array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')) . '%'; |
||||
$query->andWhere(array('like', $attribute, $value)); |
||||
} else { |
||||
$query->andWhere(array($attribute => $value)); |
||||
} |
||||
} |
||||
} |
||||
|
@ -1,38 +1,45 @@
|
||||
<?php |
||||
|
||||
use yii\helpers\Inflector; |
||||
use yii\helpers\StringHelper; |
||||
|
||||
/** |
||||
* The following variables are available in this template: |
||||
* - $this: the CrudCode object |
||||
* @var yii\base\View $this |
||||
* @var yii\gii\generators\crud\Generator $generator |
||||
*/ |
||||
|
||||
echo "<?php\n";
|
||||
?> |
||||
<?php echo "<?php\n"; ?> |
||||
/* @var $this <?php echo $this->getControllerClass(); ?> */
|
||||
/* @var $model <?php echo $this->getModelClass(); ?> */
|
||||
/* @var $form CActiveForm */ |
||||
|
||||
use yii\helpers\Html; |
||||
use yii\widgets\ActiveForm; |
||||
|
||||
/** |
||||
* @var yii\base\View $this |
||||
* @var <?php echo ltrim($generator->searchModelClass, '\\'); ?> $model
|
||||
* @var yii\widgets\ActiveForm $form |
||||
*/ |
||||
?> |
||||
|
||||
<div class="wide form"> |
||||
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-search">
|
||||
|
||||
<?php echo "<?php \$form=\$this->beginWidget('CActiveForm', array(
|
||||
'action'=>Yii::app()->createUrl(\$this->route), |
||||
'method'=>'get', |
||||
)); ?>\n"; ?> |
||||
<?php echo '<?php'; ?> $form = ActiveForm::begin(array('method' => 'get')); ?>
|
||||
|
||||
<?php foreach($this->tableSchema->columns as $column): ?> |
||||
<?php |
||||
$field=$this->generateInputField($this->modelClass,$column); |
||||
if(strpos($field,'password')!==false) |
||||
continue; |
||||
$count = 0; |
||||
foreach ($generator->getTableSchema()->getColumnNames() as $attribute) { |
||||
if (++$count < 6) { |
||||
echo "\t\t<?php echo " . $generator->generateActiveSearchField($attribute) . " ?>\n";
|
||||
} else { |
||||
echo "\t\t<?php // echo " . $generator->generateActiveSearchField($attribute) . " ?>\n";
|
||||
} |
||||
} |
||||
?> |
||||
<div class="row"> |
||||
<?php echo "<?php echo \$form->label(\$model,'{$column->name}'); ?>\n"; ?> |
||||
<?php echo "<?php echo ".$this->generateActiveField($this->modelClass,$column)."; ?>\n"; ?> |
||||
</div> |
||||
|
||||
<?php endforeach; ?> |
||||
<div class="row buttons"> |
||||
<?php echo "<?php echo CHtml::submitButton('Search'); ?>\n"; ?> |
||||
</div> |
||||
<div class="form-group"> |
||||
<?php echo '<?php'; ?> echo Html::submitButton('Search', array('class' => 'btn btn-primary')); ?>
|
||||
<?php echo '<?php'; ?> echo Html::resetButton('Reset', array('class' => 'btn btn-default')); ?>
|
||||
</div> |
||||
|
||||
<?php echo "<?php \$this->endWidget(); ?>\n"; ?> |
||||
<?php echo '<?php'; ?> ActiveForm::end(); ?>
|
||||
|
||||
</div><!-- search-form --> |
||||
</div> |
||||
|
Loading…
Reference in new issue