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
2a152cb5
Commit
2a152cb5
authored
Aug 21, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
model generator WIP
parent
6f9ddcf4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
31 additions
and
141 deletions
+31
-141
Generator.php
framework/yii/gii/Generator.php
+4
-11
Generator.php
framework/yii/gii/generators/controller/Generator.php
+0
-1
Generator.php
framework/yii/gii/generators/model/Generator.php
+0
-0
form.php
framework/yii/gii/generators/model/form.php
+3
-1
model.php
framework/yii/gii/generators/model/templates/model.php
+24
-128
No files found.
framework/yii/gii/Generator.php
View file @
2a152cb5
...
...
@@ -294,11 +294,10 @@ abstract class Generator extends Model
}
/**
* Validates an attribute to make sure it is not taking a PHP reserved keyword.
* @param string $attribute the attribute to be validated
* @param array $params validation parameters
* @param string $value the attribute to be validated
* @return boolean whether the value is a reserved PHP keyword.
*/
public
function
validateReservedWord
(
$attribute
,
$params
)
public
function
isReservedKeyword
(
$value
)
{
static
$keywords
=
array
(
'__class__'
,
...
...
@@ -381,12 +380,6 @@ abstract class Generator extends Model
'while'
,
'xor'
,
);
$value
=
$this
->
$attribute
;
foreach
(
explode
(
'\\'
,
strtolower
(
$value
))
as
$name
)
{
if
(
in_array
(
$name
,
$keywords
))
{
$this
->
addError
(
$attribute
,
$this
->
getAttributeLabel
(
$attribute
)
.
' cannot take a reserved PHP keyword.'
);
return
;
}
}
return
in_array
(
strtolower
(
$value
),
$keywords
,
true
);
}
}
framework/yii/gii/generators/controller/Generator.php
View file @
2a152cb5
...
...
@@ -65,7 +65,6 @@ class Generator extends \yii\gii\Generator
array
(
'controller'
,
'match'
,
'pattern'
=>
'/^[a-z\\-\\/]*$/'
,
'message'
=>
'Only a-z, dashes (-) and slashes (/) are allowed.'
),
array
(
'actions'
,
'match'
,
'pattern'
=>
'/^[a-z\\-,\\s]*$/'
,
'message'
=>
'Only a-z, dashes (-), spaces and commas are allowed.'
),
array
(
'baseClass'
,
'match'
,
'pattern'
=>
'/^[\w\\\\]*$/'
,
'message'
=>
'Only word characters and backslashes are allowed.'
),
array
(
'baseClass'
,
'validateReservedWord'
),
array
(
'ns'
,
'match'
,
'pattern'
=>
'/^[\w\\\\]*$/'
,
'message'
=>
'Only word characters and backslashes are allowed.'
),
));
}
...
...
framework/yii/gii/generators/model/Generator.php
View file @
2a152cb5
This diff is collapsed.
Click to expand it.
framework/yii/gii/generators/model/form.php
View file @
2a152cb5
...
...
@@ -7,6 +7,8 @@
echo
$form
->
field
(
$generator
,
'tableName'
);
echo
$form
->
field
(
$generator
,
'modelClass'
);
echo
$form
->
field
(
$generator
,
'ns'
);
echo
$form
->
field
(
$generator
,
'baseClass'
);
echo
$form
->
field
(
$generator
,
'buildRelations'
)
->
checkbox
();
echo
$form
->
field
(
$generator
,
'db'
);
echo
$form
->
field
(
$generator
,
'generateRelations'
)
->
checkbox
();
echo
$form
->
field
(
$generator
,
'commentsAsLabels'
)
->
checkbox
();
framework/yii/gii/generators/model/templates/model.php
View file @
2a152cb5
<?php
/**
* This is the template for generating the model class of a specified table.
* - $this: the ModelCode object
*
* @var yii\base\View $this
* @var yii\gii\generators\model\Generator $generator
* @var string $tableName
* @var string $className
* @var yii\db\ColumnSchema[] $columns
* @var string[] $labels
*
* - $tableName: the table name for this class (prefix is already removed if necessary)
* - $modelClass: the model class name
* - $columns: list of table columns (name=>CDbColumnSchema)
...
...
@@ -9,51 +16,29 @@
* - $rules: list of validation rules
* - $relations: list of relations (name=>relation declaration)
*/
$pos
=
strrpos
(
$className
,
'\\'
);
$ns
=
ltrim
(
substr
(
$className
,
0
,
$pos
),
'\\'
);
$className
=
substr
(
$className
,
$pos
+
1
);
echo
"<?php
\n
"
;
?>
<?php
echo
"<?php
\n
"
;
?>
namespace
<?php
echo
$ns
;
?>
;
/**
* This is the model class for table "
<?php
echo
$tableName
;
?>
".
*
* The followings are the available columns in table '
<?php
echo
$tableName
;
?>
':
<?php
foreach
(
$columns
as
$column
)
:
?>
* @property
<?php
echo
$column
->
type
.
' $'
.
$column
->
name
.
"
\n
"
;
?>
<?php
endforeach
;
?>
<?php
if
(
!
empty
(
$relations
))
:
?>
* Attributes:
*
* The followings are the available model relations:
<?php
foreach
(
$relations
as
$name
=>
$relation
)
:
?>
* @property
<?php
if
(
preg_match
(
"~^array\(self::([^,]+), '([^']+)', '([^']+)'\)$~"
,
$relation
,
$matches
))
{
$relationType
=
$matches
[
1
];
$relationModel
=
$matches
[
2
];
switch
(
$relationType
){
case
'HAS_ONE'
:
echo
$relationModel
.
' $'
.
$name
.
"
\n
"
;
break
;
case
'BELONGS_TO'
:
echo
$relationModel
.
' $'
.
$name
.
"
\n
"
;
break
;
case
'HAS_MANY'
:
echo
$relationModel
.
'[] $'
.
$name
.
"
\n
"
;
break
;
case
'MANY_MANY'
:
echo
$relationModel
.
'[] $'
.
$name
.
"
\n
"
;
break
;
default
:
echo
'mixed $'
.
$name
.
"
\n
"
;
}
}
?>
<?php
foreach
(
$columns
as
$column
)
:
?>
* @property
<?php
echo
"
{
$column
->
phpType
}
\$
{
$column
->
name
}
\n
"
;
?>
<?php
endforeach
;
?>
<?php
endif
;
?>
*/
class
<?php
echo
$
modelClass
;
?>
extends
<?php
echo
$this
->
baseClass
.
"
\n
"
;
?>
class
<?php
echo
$
className
;
?>
extends
<?php
echo
'\\'
.
ltrim
(
$generator
->
baseClass
,
'\\'
)
.
"
\n
"
;
?>
{
/**
* @
return string the associated database table name
* @
inheritdoc
*/
public function tableName()
{
...
...
@@ -61,103 +46,14 @@ class <?php echo $modelClass; ?> extends <?php echo $this->baseClass."\n"; ?>
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
<?php
foreach
(
$rules
as
$rule
)
:
?>
<?php
echo
$rule
.
",
\n
"
;
?>
<?php
endforeach
;
?>
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('
<?php
echo
implode
(
', '
,
array_keys
(
$columns
));
?>
', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
<?php
foreach
(
$relations
as
$name
=>
$relation
)
:
?>
<?php
echo
"'
$name
' =>
$relation
,
\n
"
;
?>
<?php
endforeach
;
?>
);
}
/**
* @return array customized attribute labels (name=>label)
* @inheritdoc
*/
public function attributeLabels()
{
return array(
<?php
foreach
(
$labels
as
$name
=>
$label
)
:
?>
<?php
echo
"'
$name
' => '
$label
',
\n
"
;
?>
<?php
foreach
(
$labels
as
$name
=>
$label
)
:
?>
<?php
echo
"'
$name
' => '
"
.
addslashes
(
$label
)
.
"
',
\n
"
;
?>
<?php
endforeach
;
?>
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
<?php
foreach
(
$columns
as
$name
=>
$column
)
{
if
(
$column
->
type
===
'string'
)
{
echo
"
\t\t\$
criteria->compare('
$name
',
\$
this->
$name
,true);
\n
"
;
}
else
{
echo
"
\t\t\$
criteria->compare('
$name
',
\$
this->
$name
);
\n
"
;
}
}
?>
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
<?php
if
(
$connectionId
!=
'db'
)
:?>
/**
* @return CDbConnection the database connection used for this class
*/
public
function
getDbConnection
()
{
return
Yii
::
app
()
-><?
php
echo
$connectionId
?>
;
}
<?php
endif
?>
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return
<?php
echo
$modelClass
;
?>
the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
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