Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
4cbdd7a6
Commit
4cbdd7a6
authored
Sep 09, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
crud WIP
parent
db212f05
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
362 additions
and
144 deletions
+362
-144
gii.js
framework/yii/gii/assets/gii.js
+0
-5
Generator.php
framework/yii/gii/generators/crud/Generator.php
+169
-39
form.php
framework/yii/gii/generators/crud/form.php
+1
-2
controller.php
framework/yii/gii/generators/crud/templates/controller.php
+27
-42
search.php
framework/yii/gii/generators/crud/templates/search.php
+80
-5
_form.php
framework/yii/gii/generators/crud/templates/views/_form.php
+2
-3
_search.php
...ework/yii/gii/generators/crud/templates/views/_search.php
+33
-27
create.php
framework/yii/gii/generators/crud/templates/views/create.php
+2
-0
index.php
framework/yii/gii/generators/crud/templates/views/index.php
+33
-16
update.php
framework/yii/gii/generators/crud/templates/views/update.php
+5
-0
view.php
framework/yii/gii/generators/crud/templates/views/view.php
+9
-4
view.php
framework/yii/gii/views/default/view.php
+1
-1
No files found.
framework/yii/gii/assets/gii.js
View file @
4cbdd7a6
...
...
@@ -81,11 +81,6 @@ yii.gii = (function ($) {
$
(
'#model-generator .field-generator-modelclass'
).
toggle
(
$
(
this
).
val
().
indexOf
(
'*'
)
==
-
1
);
}).
change
();
// crud generator: hide Search Model Class input if search is not enabled
$
(
'#crud-generator #generator-enablesearch'
).
on
(
'change'
,
function
()
{
$
(
'#crud-generator .field-generator-searchmodelclass'
).
toggle
(
this
.
checked
);
}).
change
();
// hide Generate button if any input is changed
$
(
'.default-view .form-group input,select,textarea'
).
change
(
function
()
{
$
(
'.default-view-results,.default-view-files'
).
hide
();
...
...
framework/yii/gii/generators/crud/Generator.php
View file @
4cbdd7a6
...
...
@@ -10,6 +10,7 @@ namespace yii\gii\generators\crud;
use
Yii
;
use
yii\base\Model
;
use
yii\db\ActiveRecord
;
use
yii\db\Schema
;
use
yii\gii\CodeFile
;
use
yii\helpers\Inflector
;
use
yii\web\Controller
;
...
...
@@ -26,7 +27,6 @@ class Generator extends \yii\gii\Generator
public
$controllerClass
;
public
$baseControllerClass
=
'yii\web\Controller'
;
public
$indexWidgetType
=
'grid'
;
public
$enableSearch
=
true
;
public
$searchModelClass
;
public
function
getName
()
...
...
@@ -44,16 +44,14 @@ class Generator extends \yii\gii\Generator
{
return
array_merge
(
parent
::
rules
(),
array
(
array
(
'moduleID, controllerClass, modelClass, searchModelClass, baseControllerClass'
,
'filter'
,
'filter'
=>
'trim'
),
array
(
'modelClass, controllerClass, baseControllerClass, indexWidgetType'
,
'required'
),
array
(
'modelClass,
searchModelClass,
controllerClass, baseControllerClass, indexWidgetType'
,
'required'
),
array
(
'modelClass, controllerClass, baseControllerClass, searchModelClass'
,
'match'
,
'pattern'
=>
'/^[\w\\\\]*$/'
,
'message'
=>
'Only word characters and backslashes are allowed.'
),
array
(
'modelClass'
,
'validateClass'
,
'params'
=>
array
(
'extends'
=>
ActiveRecord
::
className
())),
array
(
'baseControllerClass'
,
'validateClass'
,
'params'
=>
array
(
'extends'
=>
Controller
::
className
())),
array
(
'controllerClass'
,
'match'
,
'pattern'
=>
'/Controller$/'
,
'message'
=>
'Controller class name must be suffixed with "Controller".'
),
array
(
'controllerClass, searchModelClass'
,
'validateNewClass'
),
array
(
'enableSearch'
,
'boolean'
),
array
(
'indexWidgetType'
,
'in'
,
'range'
=>
array
(
'grid'
,
'list'
)),
array
(
'modelClass'
,
'validateModelClass'
),
array
(
'searchModelClass'
,
'validateSearchModelClass'
),
array
(
'moduleID'
,
'validateModuleID'
),
));
}
...
...
@@ -66,7 +64,6 @@ class Generator extends \yii\gii\Generator
'controllerClass'
=>
'Controller Class'
,
'baseControllerClass'
=>
'Base Controller Class'
,
'indexWidgetType'
=>
'Widget Used in Index Page'
,
'enableSearch'
=>
'Enable Search'
,
'searchModelClass'
=>
'Search Model Class'
,
));
}
...
...
@@ -87,11 +84,8 @@ class Generator extends \yii\gii\Generator
If not set, it means the controller will belong to the application.'
,
'indexWidgetType'
=>
'This is the widget type to be used in the index page to display list of the models.
You may choose either <code>GridView</code> or <code>ListView</code>'
,
'enableSearch'
=>
'Whether to enable the search functionality on the index page. When search is enabled,
a search form will be displayed on the index page, and the index page will display the search results.'
,
'searchModelClass'
=>
'This is the class representing the data being collecting in the search form.
A fully qualified namespaced class name is required, e.g., <code>app\models\PostSearchForm</code>.
This is only used when search is enabled.'
,
A fully qualified namespaced class name is required, e.g., <code>app\models\search\PostSearch</code>.'
,
);
}
...
...
@@ -107,7 +101,7 @@ class Generator extends \yii\gii\Generator
*/
public
function
stickyAttributes
()
{
return
array
(
'baseControllerClass'
,
'moduleID'
,
'indexWidgetType'
,
'enableSearch'
);
return
array
(
'baseControllerClass'
,
'moduleID'
,
'indexWidgetType'
);
}
public
function
validateModelClass
()
...
...
@@ -120,13 +114,6 @@ class Generator extends \yii\gii\Generator
}
}
public
function
validateSearchModelClass
()
{
if
(
$this
->
enableSearch
&&
empty
(
$this
->
searchModelClass
))
{
$this
->
addError
(
'searchModelClass'
,
'Search Model Class cannot be empty.'
);
}
}
public
function
validateModuleID
()
{
if
(
!
empty
(
$this
->
moduleID
))
{
...
...
@@ -142,26 +129,21 @@ class Generator extends \yii\gii\Generator
*/
public
function
generate
()
{
$files
=
array
();
$files
[]
=
new
CodeFile
(
$this
->
getControllerFile
(),
$this
->
render
(
'controller.php'
)
$controllerFile
=
Yii
::
getAlias
(
'@'
.
str_replace
(
'\\'
,
'/'
,
ltrim
(
$this
->
controllerClass
,
'\\'
))
.
'.php'
);
$searchModel
=
Yii
::
getAlias
(
'@'
.
str_replace
(
'\\'
,
'/'
,
ltrim
(
$this
->
searchModelClass
,
'\\'
)
.
'.php'
));
$files
=
array
(
new
CodeFile
(
$controllerFile
,
$this
->
render
(
'controller.php'
)),
new
CodeFile
(
$searchModel
,
$this
->
render
(
'search.php'
)),
);
$viewPath
=
$this
->
getViewPath
();
$viewPath
=
$this
->
getViewPath
();
$templatePath
=
$this
->
getTemplatePath
()
.
'/views'
;
foreach
(
scandir
(
$templatePath
)
as
$file
)
{
if
(
!
in_array
(
$file
,
array
(
'index.php'
,
'create.php'
,
'update.php'
,
'view.php'
,
'_form.php'
)))
{
continue
;
}
if
(
is_file
(
$templatePath
.
'/'
.
$file
)
&&
pathinfo
(
$file
,
PATHINFO_EXTENSION
)
===
'php'
)
{
$files
[]
=
new
CodeFile
(
"
$viewPath
/
$file
"
,
$this
->
render
(
"views/
$file
"
));
}
}
if
(
$this
->
enableSearch
)
{
}
return
$files
;
}
...
...
@@ -177,14 +159,6 @@ class Generator extends \yii\gii\Generator
}
/**
* @return string the controller class file path
*/
public
function
getControllerFile
()
{
return
Yii
::
getAlias
(
'@'
.
str_replace
(
'\\'
,
'/'
,
ltrim
(
$this
->
controllerClass
,
'\\'
))
.
'.php'
);
}
/**
* @return string the action view file path
*/
public
function
getViewPath
()
...
...
@@ -207,13 +181,12 @@ class Generator extends \yii\gii\Generator
}
/**
* @param ActiveRecord $model
* @param string $attribute
* @return string
*/
public
function
generateActiveField
(
$
model
,
$
attribute
)
public
function
generateActiveField
(
$attribute
)
{
$tableSchema
=
$
model
->
getTableSchema
();
$tableSchema
=
$
this
->
getTableSchema
();
if
(
!
isset
(
$tableSchema
->
columns
[
$attribute
]))
{
return
"
\$
form->field(
\$
model, '
$attribute
');"
;
}
...
...
@@ -237,6 +210,21 @@ class Generator extends \yii\gii\Generator
}
/**
* @param string $attribute
* @return string
*/
public
function
generateActiveSearchField
(
$attribute
)
{
$tableSchema
=
$this
->
getTableSchema
();
$column
=
$tableSchema
->
columns
[
$attribute
];
if
(
$column
->
phpType
===
'boolean'
)
{
return
"
\$
form->field(
\$
model, '
$attribute
')->checkbox();"
;
}
else
{
return
"
\$
form->field(
\$
model, '
$attribute
');"
;
}
}
/**
* @param \yii\db\ColumnSchema $column
* @return string
*/
...
...
@@ -256,4 +244,146 @@ class Generator extends \yii\gii\Generator
return
'text'
;
}
}
/**
* Generates validation rules for the search model.
* @return array the generated validation rules
*/
public
function
generateSearchRules
()
{
$table
=
$this
->
getTableSchema
();
$types
=
array
();
foreach
(
$table
->
columns
as
$column
)
{
switch
(
$column
->
type
)
{
case
Schema
::
TYPE_SMALLINT
:
case
Schema
::
TYPE_INTEGER
:
case
Schema
::
TYPE_BIGINT
:
$types
[
'integer'
][]
=
$column
->
name
;
break
;
case
Schema
::
TYPE_BOOLEAN
:
$types
[
'boolean'
][]
=
$column
->
name
;
break
;
case
Schema
::
TYPE_FLOAT
:
case
Schema
::
TYPE_DECIMAL
:
case
Schema
::
TYPE_MONEY
:
$types
[
'number'
][]
=
$column
->
name
;
break
;
case
Schema
::
TYPE_DATE
:
case
Schema
::
TYPE_TIME
:
case
Schema
::
TYPE_DATETIME
:
case
Schema
::
TYPE_TIMESTAMP
:
default
:
$types
[
'safe'
][]
=
$column
->
name
;
break
;
}
}
$rules
=
array
();
foreach
(
$types
as
$type
=>
$columns
)
{
$rules
[]
=
"array('"
.
implode
(
', '
,
$columns
)
.
"', '
$type
')"
;
}
return
$rules
;
}
public
function
getSearchAttributes
()
{
return
$this
->
getTableSchema
()
->
getColumnNames
();
}
/**
* Generates the attribute labels for the search model.
* @return array the generated attribute labels (name => label)
*/
public
function
generateSearchLabels
()
{
$table
=
$this
->
getTableSchema
();
$labels
=
array
();
foreach
(
$table
->
columns
as
$column
)
{
if
(
!
strcasecmp
(
$column
->
name
,
'id'
))
{
$labels
[
$column
->
name
]
=
'ID'
;
}
else
{
$label
=
Inflector
::
camel2words
(
$column
->
name
);
if
(
strcasecmp
(
substr
(
$label
,
-
3
),
' id'
)
===
0
)
{
$label
=
substr
(
$label
,
0
,
-
3
)
.
' ID'
;
}
$labels
[
$column
->
name
]
=
$label
;
}
}
return
$labels
;
}
public
function
generateSearchConditions
()
{
$table
=
$this
->
getTableSchema
();
$conditions
=
array
();
foreach
(
$table
->
columns
as
$column
)
{
switch
(
$column
->
type
)
{
case
Schema
::
TYPE_SMALLINT
:
case
Schema
::
TYPE_INTEGER
:
case
Schema
::
TYPE_BIGINT
:
case
Schema
::
TYPE_BOOLEAN
:
case
Schema
::
TYPE_FLOAT
:
case
Schema
::
TYPE_DECIMAL
:
case
Schema
::
TYPE_MONEY
:
case
Schema
::
TYPE_DATE
:
case
Schema
::
TYPE_TIME
:
case
Schema
::
TYPE_DATETIME
:
case
Schema
::
TYPE_TIMESTAMP
:
$conditions
[]
=
"
\$
this->addCondition(
\$
query, '
{
$column
->
name
}
');"
;
break
;
default
:
$conditions
[]
=
"
\$
this->addCondition(
\$
query, '
{
$column
->
name
}
', true);"
;
break
;
}
}
return
$conditions
;
}
public
function
generateUrlParams
()
{
$pks
=
$this
->
getTableSchema
()
->
primaryKey
;
if
(
count
(
$pks
)
===
1
)
{
return
"'id' =>
\$
model->
{
$pks
[
0
]
}
"
;
}
else
{
$params
=
array
();
foreach
(
$pks
as
$pk
)
{
$params
[]
=
"'
$pk
' =>
\$
model->
$pk
"
;
}
return
implode
(
', '
,
$params
);
}
}
public
function
generateActionParams
()
{
$pks
=
$this
->
getTableSchema
()
->
primaryKey
;
if
(
count
(
$pks
)
===
1
)
{
return
'$id'
;
}
else
{
return
'$'
.
implode
(
', $'
,
$pks
);
}
}
public
function
generateActionParamComments
()
{
$table
=
$this
->
getTableSchema
();
$pks
=
$table
->
primaryKey
;
if
(
count
(
$pks
)
===
1
)
{
return
array
(
'@param '
.
$table
->
columns
[
$pks
[
0
]]
->
phpType
.
' $id'
);
}
else
{
$params
=
array
();
foreach
(
$pks
as
$pk
)
{
$params
[]
=
'@param '
.
$table
->
columns
[
$pk
]
->
phpType
.
' $'
.
$pk
;
}
return
$params
;
}
}
public
function
getTableSchema
()
{
/** @var ActiveRecord $class */
$class
=
$this
->
modelClass
;
return
$class
::
getTableSchema
();
}
}
framework/yii/gii/generators/crud/form.php
View file @
4cbdd7a6
...
...
@@ -6,11 +6,10 @@
*/
echo
$form
->
field
(
$generator
,
'modelClass'
);
echo
$form
->
field
(
$generator
,
'searchModelClass'
);
echo
$form
->
field
(
$generator
,
'controllerClass'
);
echo
$form
->
field
(
$generator
,
'baseControllerClass'
);
echo
$form
->
field
(
$generator
,
'moduleID'
);
echo
$form
->
field
(
$generator
,
'enableSearch'
)
->
checkbox
();
echo
$form
->
field
(
$generator
,
'searchModelClass'
);
echo
$form
->
field
(
$generator
,
'indexWidgetType'
)
->
dropDownList
(
array
(
'grid'
=>
'GridView'
,
'list'
=>
'ListView'
,
...
...
framework/yii/gii/generators/crud/templates/controller.php
View file @
4cbdd7a6
...
...
@@ -9,38 +9,22 @@ use yii\helpers\StringHelper;
* @var yii\gii\generators\crud\Generator $generator
*/
$pos
=
strrpos
(
$generator
->
controllerClass
,
'\\'
);
$ns
=
ltrim
(
substr
(
$generator
->
controllerClass
,
0
,
$pos
),
'\\'
);
$controllerClass
=
substr
(
$generator
->
controllerClass
,
$pos
+
1
);
$pos
=
strrpos
(
$generator
->
modelClass
,
'\\'
);
$modelClass
=
$pos
===
false
?
$generator
->
modelClass
:
substr
(
$generator
->
modelClass
,
$pos
+
1
);
/** @var \yii\db\ActiveRecord $class */
$class
=
$generator
->
modelClass
;
$pks
=
$class
::
primaryKey
();
$schema
=
$class
::
getTableSchema
();
if
(
count
(
$pks
)
===
1
)
{
$ids
=
'$id'
;
$params
=
"array('id' =>
\$
model->
{
$pks
[
0
]
}
)"
;
$paramComments
=
'@param '
.
$schema
->
columns
[
$pks
[
0
]]
->
phpType
.
' $id'
;
}
else
{
$ids
=
'$'
.
implode
(
', $'
,
$pks
);
$params
=
array
();
$paramComments
=
array
();
foreach
(
$pks
as
$pk
)
{
$paramComments
[]
=
'@param '
.
$schema
->
columns
[
$pk
]
->
phpType
.
' $'
.
$pk
;
$params
[]
=
"'
$pk
' =>
\$
model->
$pk
"
;
}
$params
=
implode
(
', '
,
$params
);
$paramComments
=
implode
(
"
\n\t
* "
,
$paramComments
);
}
$controllerClass
=
StringHelper
::
basename
(
$generator
->
controllerClass
);
$modelClass
=
StringHelper
::
basename
(
$generator
->
modelClass
);
$searchModelClass
=
StringHelper
::
basename
(
$generator
->
searchModelClass
);
$pks
=
$generator
->
getTableSchema
()
->
primaryKey
;
$urlParams
=
$generator
->
generateUrlParams
();
$actionParams
=
$generator
->
generateActionParams
();
$actionParamComments
=
$generator
->
generateActionParamComments
();
echo
"<?php
\n
"
;
?>
namespace
<?php
echo
$ns
;
?>
;
namespace
<?php
echo
StringHelper
::
dirname
(
ltrim
(
$generator
->
controllerClass
,
'\\'
))
;
?>
;
use
<?php
echo
ltrim
(
$generator
->
modelClass
,
'\\'
);
?>
;
use
<?php
echo
ltrim
(
$generator
->
searchModelClass
,
'\\'
);
?>
;
use yii\data\ActiveDataProvider;
use
<?php
echo
ltrim
(
$generator
->
baseControllerClass
,
'\\'
);
?>
;
use yii\web\HttpException;
...
...
@@ -56,23 +40,24 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
*/
public function actionIndex()
{
$
dataProvider = new ActiveDataProvider(array(
'query' =>
<?php
echo
$modelClass
;
?>
::find(),
));
$
searchModel = new
<?php
echo
$searchModelClass
;
?>
;
$dataProvider = $searchModel->search($_GET);
return $this->render('index', array(
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
));
}
/**
* Displays a single
<?php
echo
$modelClass
;
?>
model.
*
<?php
echo
$paramComments
.
"
\n
"
;
?>
*
<?php
echo
implode
(
"
\n\t
* "
,
$actionParamComments
)
.
"
\n
"
;
?>
* @return mixed
*/
public function actionView(
<?php
echo
$
id
s
;
?>
)
public function actionView(
<?php
echo
$
actionParam
s
;
?>
)
{
return $this->render('view', array(
'model' => $this->findModel(
<?php
echo
$
id
s
;
?>
),
'model' => $this->findModel(
<?php
echo
$
actionParam
s
;
?>
),
));
}
...
...
@@ -86,7 +71,7 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
$model = new
<?php
echo
$modelClass
;
?>
;
if ($model->load($_POST)
&&
$model->save()) {
return $this->redirect(array('view',
<?php
echo
$
p
arams
;
?>
));
return $this->redirect(array('view',
<?php
echo
$
urlP
arams
;
?>
));
} else {
return $this->render('create', array(
'model' => $model,
...
...
@@ -97,15 +82,15 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
/**
* Updates an existing
<?php
echo
$modelClass
;
?>
model.
* If update is successful, the browser will be redirected to the 'view' page.
*
<?php
echo
$paramComments
.
"
\n
"
;
?>
*
<?php
echo
implode
(
"
\n\t
* "
,
$actionParamComments
)
.
"
\n
"
;
?>
* @return mixed
*/
public function actionUpdate(
<?php
echo
$
id
s
;
?>
)
public function actionUpdate(
<?php
echo
$
actionParam
s
;
?>
)
{
$model = $this->findModel(
<?php
echo
$
id
s
;
?>
);
$model = $this->findModel(
<?php
echo
$
actionParam
s
;
?>
);
if ($model->load($_POST)
&&
$model->save()) {
return $this->redirect(array('view',
<?php
echo
$
p
arams
;
?>
));
return $this->redirect(array('view',
<?php
echo
$
urlP
arams
;
?>
));
} else {
return $this->render('update', array(
'model' => $model,
...
...
@@ -116,23 +101,23 @@ class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename(
/**
* Deletes an existing
<?php
echo
$modelClass
;
?>
model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*
<?php
echo
$paramComments
.
"
\n
"
;
?>
*
<?php
echo
implode
(
"
\n\t
* "
,
$actionParamComments
)
.
"
\n
"
;
?>
* @return mixed
*/
public function actionDelete(
<?php
echo
$
id
s
;
?>
)
public function actionDelete(
<?php
echo
$
actionParam
s
;
?>
)
{
$this->findModel(
<?php
echo
$
id
s
;
?>
)->delete();
$this->findModel(
<?php
echo
$
actionParam
s
;
?>
)->delete();
return $this->redirect(array('index'));
}
/**
* Finds the
<?php
echo
$modelClass
;
?>
model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
<?php
echo
$paramComments
.
"
\n
"
;
?>
*
<?php
echo
implode
(
"
\n\t
* "
,
$actionParamComments
)
.
"
\n
"
;
?>
* @return
<?php
echo
$modelClass
;
?>
the loaded model
* @throws HttpException if the model cannot be found
*/
protected function findModel(
<?php
echo
$
id
s
;
?>
)
protected function findModel(
<?php
echo
$
actionParam
s
;
?>
)
{
<?php
if
(
count
(
$pks
)
===
1
)
{
...
...
framework/yii/gii/generators/crud/templates/search.php
View file @
4cbdd7a6
<?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\t
public $"
,
$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));
}
}
}
framework/yii/gii/generators/crud/templates/views/_form.php
View file @
4cbdd7a6
...
...
@@ -9,8 +9,7 @@ use yii\helpers\StringHelper;
*/
/** @var \yii\db\ActiveRecord $model */
$class
=
$generator
->
modelClass
;
$model
=
new
$class
;
$model
=
new
$generator
->
modelClass
;
$safeAttributes
=
$model
->
safeAttributes
();
if
(
empty
(
$safeAttributes
))
{
$safeAttributes
=
$model
->
getTableSchema
()
->
columnNames
;
...
...
@@ -34,7 +33,7 @@ use yii\widgets\ActiveForm;
<?php
echo
'<?php'
;
?>
$form = ActiveForm::begin(); ?>
<?php
foreach
(
$safeAttributes
as
$attribute
)
{
echo
"
\t\t
<?php echo "
.
$generator
->
generateActiveField
(
$
model
,
$
attribute
)
.
" ?>
\n\n
"
;
echo
"
\t\t
<?php echo "
.
$generator
->
generateActiveField
(
$attribute
)
.
" ?>
\n\n
"
;
}
?>
<div
class=
"form-group"
>
<?php
echo
'<?php'
;
?>
echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', array('class' => 'btn btn-primary')); ?>
...
...
framework/yii/gii/generators/crud/templates/views/_search.php
View file @
4cbdd7a6
<?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 -->
\ No newline at end of file
</div>
framework/yii/gii/generators/crud/templates/views/create.php
View file @
4cbdd7a6
...
...
@@ -19,6 +19,8 @@ use yii\helpers\Html;
*/
$this->title = 'Create
<?php
echo
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
));
?>
';
$this->params['breadcrumbs'][] = array('label' => '
<?php
echo
Inflector
::
pluralize
(
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
)));
?>
', 'url' => array('index'));
$this->params['breadcrumbs'][] = $this->title;
?>
<div
class=
"
<?php
echo
Inflector
::
camel2id
(
StringHelper
::
basename
(
$generator
->
modelClass
));
?>
-create"
>
...
...
framework/yii/gii/generators/crud/templates/views/index.php
View file @
4cbdd7a6
...
...
@@ -8,19 +8,7 @@ use yii\helpers\StringHelper;
* @var yii\gii\generators\crud\Generator $generator
*/
/** @var \yii\db\ActiveRecord $model */
$class
=
$generator
->
modelClass
;
$pks
=
$class
::
primaryKey
();
if
(
count
(
$pks
)
===
1
)
{
$viewUrl
=
"array('view', 'id' =>
\$
model->
{
$pks
[
0
]
}
)"
;
}
else
{
$params
=
array
();
foreach
(
$pks
as
$pk
)
{
$params
[]
=
"'
$pk
' =>
\$
model->
$pk
"
;
}
$viewUrl
=
"array('view', "
.
implode
(
', '
,
$params
)
.
')'
;
}
$urlParams
=
$generator
->
generateUrlParams
();
$nameAttribute
=
$generator
->
getNameAttribute
();
echo
"<?php
\n
"
;
...
...
@@ -32,21 +20,50 @@ use <?php echo $generator->indexWidgetType === 'grid' ? 'yii\grid\GridView' : 'y
/**
* @var yii\base\View $this
* @var yii\data\ActiveDataProvider $dataProvider
* @var
<?php
echo
ltrim
(
$generator
->
searchModelClass
,
'\\'
);
?>
$searchModel
*/
$this->title = '
<?php
echo
Inflector
::
pluralize
(
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
)));
?>
';
$this->params['breadcrumbs'][] = $this->title;
?>
<div
class=
"
<?php
echo
Inflector
::
camel2id
(
StringHelper
::
basename
(
$generator
->
modelClass
));
?>
-index"
>
<h1>
<?php
echo
"<?php"
;
?>
echo Html::encode($this->title); ?>
</h1>
<?php
if
(
$generator
->
indexWidgetType
===
'grid'
)
:
?>
<?php
echo
'<?php'
;
?>
echo $this->render('_search', array('model' => $searchModel));
?>
<hr>
<div>
<?php
echo
'<?php'
;
?>
echo Html::a('Create
<?php
echo
StringHelper
::
basename
(
$generator
->
modelClass
);
?>
', array('create'), array('class' => 'btn btn-danger')); ?>
</div>
<?php
if
(
$generator
->
indexWidgetType
===
'grid'
)
:
?>
<?php
echo
"<?php"
;
?>
echo GridView::widget(array(
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => array(
<?php
$count
=
0
;
foreach
(
$generator
->
getTableSchema
()
->
columns
as
$column
)
{
$format
=
$generator
->
generateColumnFormat
(
$column
);
if
(
++
$count
<
6
)
{
echo
"
\t\t\t
'"
.
$column
->
name
.
(
$format
===
'text'
?
''
:
':'
.
$format
)
.
"',
\n
"
;
}
else
{
echo
"
\t\t\t
// '"
.
$column
->
name
.
(
$format
===
'text'
?
''
:
':'
.
$format
)
.
"',
\n
"
;
}
}
?>
),
)); ?>
<?php
else
:
?>
<?php
echo
"
\t
<?php"
;
?>
echo ListView::widget(array(
<?php
echo
"
<?php"
;
?>
echo ListView::widget(array(
'dataProvider' => $dataProvider,
'itemOptions' => array(
'class' => 'item',
),
'itemView' => function ($model, $key, $index, $widget) {
return Html::a(Html::encode($model->
<?php
echo
$nameAttribute
;
?>
),
<?php
echo
$viewUrl
;
?>
);
return Html::a(Html::encode($model->
<?php
echo
$nameAttribute
;
?>
),
array('view',
<?php
echo
$urlParams
;
?>
);
},
)); ?>
<?php
endif
;
?>
...
...
framework/yii/gii/generators/crud/templates/views/update.php
View file @
4cbdd7a6
...
...
@@ -8,6 +8,8 @@ use yii\helpers\StringHelper;
* @var yii\gii\generators\crud\Generator $generator
*/
$urlParams
=
$generator
->
generateUrlParams
();
echo
"<?php
\n
"
;
?>
...
...
@@ -19,6 +21,9 @@ use yii\helpers\Html;
*/
$this->title = 'Update
<?php
echo
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
));
?>
: ' . $model->
<?php
echo
$generator
->
getNameAttribute
();
?>
;
$this->params['breadcrumbs'][] = array('label' => '
<?php
echo
Inflector
::
pluralize
(
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
)));
?>
', 'url' => array('index'));
$this->params['breadcrumbs'][] = array('label' => $model->
<?php
echo
$generator
->
getNameAttribute
();
?>
, 'url' => array('view',
<?php
echo
$urlParams
;
?>
));
$this->params['breadcrumbs'][] = 'Update';
?>
<div
class=
"
<?php
echo
Inflector
::
camel2id
(
StringHelper
::
basename
(
$generator
->
modelClass
));
?>
-update"
>
...
...
framework/yii/gii/generators/crud/templates/views/view.php
View file @
4cbdd7a6
...
...
@@ -8,9 +8,7 @@ use yii\helpers\StringHelper;
* @var yii\gii\generators\crud\Generator $generator
*/
/** @var \yii\db\ActiveRecord $model */
$class
=
$generator
->
modelClass
;
$model
=
new
$class
;
$urlParams
=
$generator
->
generateUrlParams
();
echo
"<?php
\n
"
;
?>
...
...
@@ -24,16 +22,23 @@ use yii\widgets\DetailView;
*/
$this->title = $model->
<?php
echo
$generator
->
getNameAttribute
();
?>
;
$this->params['breadcrumbs'][] = array('label' => '
<?php
echo
Inflector
::
pluralize
(
Inflector
::
camel2words
(
StringHelper
::
basename
(
$generator
->
modelClass
)));
?>
', 'url' => array('index'));
$this->params['breadcrumbs'][] = $this->title;
?>
<div
class=
"
<?php
echo
Inflector
::
camel2id
(
StringHelper
::
basename
(
$generator
->
modelClass
));
?>
-view"
>
<h1>
<?php
echo
"<?php"
;
?>
echo Html::encode($this->title); ?>
</h1>
<div>
<?php
echo
'<?php'
;
?>
echo Html::a('Update', array('update',
<?php
echo
$urlParams
;
?>
), array('class' => 'btn btn-danger')); ?>
<?php
echo
'<?php'
;
?>
echo Html::a('Delete', array('delete',
<?php
echo
$urlParams
;
?>
), array('class' => 'btn btn-danger')); ?>
</div>
<?php
echo
'<?php'
;
?>
echo DetailView::widget(array(
'model' => $model,
'attributes' => array(
<?php
foreach
(
$
model
->
getTableSchema
()
->
columns
as
$column
)
{
foreach
(
$
generator
->
getTableSchema
()
->
columns
as
$column
)
{
$format
=
$generator
->
generateColumnFormat
(
$column
);
echo
"
\t\t\t
'"
.
$column
->
name
.
(
$format
===
'text'
?
''
:
':'
.
$format
)
.
"',
\n
"
;
}
...
...
framework/yii/gii/views/default/view.php
View file @
4cbdd7a6
...
...
@@ -40,7 +40,7 @@ foreach ($generator->templates as $name => $path) {
'form'
=>
$form
,
));
?>
<?php
echo
$form
->
field
(
$generator
,
'template'
)
->
sticky
()
->
label
(
array
(
'label'
=>
'Code Template'
)
)
->
label
(
'Code Template'
)
->
dropDownList
(
$templates
)
->
hint
(
'
Please select which set of the templates should be used to generated the code.
'
);
?>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment