Qiang Xue
11 years ago
12 changed files with 362 additions and 143 deletions
@ -1,8 +1,83 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by JetBrains PhpStorm. |
* This is the template for generating a CRUD controller class file. |
||||||
* User: qiang |
* |
||||||
* Date: 8/26/13 |
* @var yii\base\View $this |
||||||
* Time: 5:22 PM |
* @var yii\gii\generators\crud\Generator $generator |
||||||
* To change this template use File | Settings | File Templates. |
|
||||||
*/ |
*/ |
||||||
|
|
||||||
|
$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 |
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
/** |
/** |
||||||
* The following variables are available in this template: |
* @var yii\base\View $this |
||||||
* - $this: the CrudCode object |
* @var yii\gii\generators\crud\Generator $generator |
||||||
*/ |
*/ |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
?> |
?> |
||||||
<?php echo "<?php\n"; ?> |
|
||||||
/* @var $this <?php echo $this->getControllerClass(); ?> */
|
use yii\helpers\Html; |
||||||
/* @var $model <?php echo $this->getModelClass(); ?> */
|
use yii\widgets\ActiveForm; |
||||||
/* @var $form CActiveForm */ |
|
||||||
|
/** |
||||||
|
* @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(
|
<?php echo '<?php'; ?> $form = ActiveForm::begin(array('method' => 'get')); ?>
|
||||||
'action'=>Yii::app()->createUrl(\$this->route), |
|
||||||
'method'=>'get', |
|
||||||
)); ?>\n"; ?> |
|
||||||
|
|
||||||
<?php foreach($this->tableSchema->columns as $column): ?> |
|
||||||
<?php |
<?php |
||||||
$field=$this->generateInputField($this->modelClass,$column); |
$count = 0; |
||||||
if(strpos($field,'password')!==false) |
foreach ($generator->getTableSchema()->getColumnNames() as $attribute) { |
||||||
continue; |
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"> |
<div class="form-group"> |
||||||
<?php echo "<?php echo \$form->label(\$model,'{$column->name}'); ?>\n"; ?> |
<?php echo '<?php'; ?> echo Html::submitButton('Search', array('class' => 'btn btn-primary')); ?>
|
||||||
<?php echo "<?php echo ".$this->generateActiveField($this->modelClass,$column)."; ?>\n"; ?> |
<?php echo '<?php'; ?> echo Html::resetButton('Reset', array('class' => 'btn btn-default')); ?>
|
||||||
</div> |
|
||||||
|
|
||||||
<?php endforeach; ?> |
|
||||||
<div class="row buttons"> |
|
||||||
<?php echo "<?php echo CHtml::submitButton('Search'); ?>\n"; ?> |
|
||||||
</div> |
</div> |
||||||
|
|
||||||
<?php echo "<?php \$this->endWidget(); ?>\n"; ?> |
<?php echo '<?php'; ?> ActiveForm::end(); ?>
|
||||||
|
|
||||||
</div><!-- search-form --> |
</div> |
||||||
|
Loading…
Reference in new issue