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
7e5e3155
Commit
7e5e3155
authored
Mar 20, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'ar-default-values'
parents
3ab3f996
c24ae25b
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
79 additions
and
3 deletions
+79
-3
active-record.md
docs/guide/active-record.md
+9
-0
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
ActiveRecord.php
framework/db/ActiveRecord.php
+15
-0
Schema.php
framework/db/pgsql/Schema.php
+7
-1
Schema.php
framework/db/sqlite/Schema.php
+2
-2
Type.php
tests/unit/data/ar/Type.php
+32
-0
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+13
-0
No files found.
docs/guide/active-record.md
View file @
7e5e3155
...
...
@@ -198,6 +198,15 @@ Customer::updateAllCounters(['age' => 1]);
> Info: The `save()` method will either perform an `INSERT` or `UPDATE` SQL statement, depending
on whether the ActiveRecord being saved is new or not by checking
`ActiveRecord::isNewRecord`
.
In order to load default values from database schema you may call
`loadDefaultValues()`
method:
```
php
$customer
=
new
Customer
();
$customer
->
loadDefaultValues
();
$customer
->
name
=
'Alexander'
;
$customer
->
save
();
```
Data Input and Validation
-------------------------
...
...
framework/CHANGELOG.md
View file @
7e5e3155
...
...
@@ -146,6 +146,7 @@ Yii Framework 2 Change Log
-
Enh #2661: Added boolean column type support for SQLite (qiangxue)
-
Enh #2670: Changed
`console\Controller::globalOptions()`
to
`options($actionId)`
to (make it possible to) differentiate options per action (hqx)
-
Enh #2714: Added support for formatting time intervals relative to the current time with
`yii\base\Formatter`
(drenty)
-
Enh #2726: Added
`yii\db\ActiveRecord::loadDefaultValues()`
that fills default values from DB schema (samdark)
-
Enh #2729: Added
`FilterValidator::skipOnArray`
so that filters like
`trim`
will not fail for array inputs (qiangxue)
-
Enh #2735: Added support for
`DateTimeInterface`
in
`Formatter`
(ivokund)
-
Enh #2756: Added support for injecting custom
`isEmpty`
check for all validators (qiangxue)
...
...
framework/db/ActiveRecord.php
View file @
7e5e3155
...
...
@@ -94,6 +94,21 @@ class ActiveRecord extends BaseActiveRecord
const
OP_ALL
=
0x07
;
/**
* Loads default values from database table schema
*
* @return static model instance
*/
public
function
loadDefaultValues
()
{
foreach
(
$this
->
getTableSchema
()
->
columns
as
$column
)
{
if
(
$column
->
defaultValue
)
{
$this
->
{
$column
->
name
}
=
$column
->
defaultValue
;
}
}
return
$this
;
}
/**
* Returns the database connection used by this AR class.
* By default, the "db" application component is used as the database connection.
* You may override this method if you want to use a different database connection.
...
...
framework/db/pgsql/Schema.php
View file @
7e5e3155
...
...
@@ -373,6 +373,12 @@ SQL;
$table
->
sequenceName
=
preg_replace
([
'/nextval/'
,
'/::/'
,
'/regclass/'
,
'/\'\)/'
,
'/\(\'/'
],
''
,
$column
->
defaultValue
);
}
}
if
(
$column
->
defaultValue
)
{
if
(
preg_match
(
"/^'(.*?)'::/"
,
$column
->
defaultValue
,
$matches
)
||
preg_match
(
"/^(.*?)::/"
,
$column
->
defaultValue
,
$matches
))
{
$column
->
defaultValue
=
$matches
[
1
];
}
}
}
return
true
;
...
...
@@ -398,7 +404,6 @@ SQL;
$column
->
precision
=
$info
[
'numeric_precision'
];
$column
->
scale
=
$info
[
'numeric_scale'
];
$column
->
size
=
$info
[
'size'
];
if
(
isset
(
$this
->
typeMap
[
$column
->
dbType
]))
{
$column
->
type
=
$this
->
typeMap
[
$column
->
dbType
];
}
else
{
...
...
@@ -406,6 +411,7 @@ SQL;
}
$column
->
phpType
=
$this
->
getColumnPhpType
(
$column
);
return
$column
;
}
}
framework/db/sqlite/Schema.php
View file @
7e5e3155
...
...
@@ -238,9 +238,9 @@ class Schema extends \yii\db\Schema
}
$column
->
phpType
=
$this
->
getColumnPhpType
(
$column
);
$value
=
$info
[
'dflt_value'
]
;
$value
=
trim
(
$info
[
'dflt_value'
],
"'
\"
"
)
;
if
(
$column
->
type
===
'string'
)
{
$column
->
defaultValue
=
trim
(
$value
,
"'
\"
"
)
;
$column
->
defaultValue
=
$value
;
}
else
{
$column
->
defaultValue
=
$column
->
typecast
(
strcasecmp
(
$value
,
'null'
)
?
$value
:
null
);
}
...
...
tests/unit/data/ar/Type.php
0 → 100644
View file @
7e5e3155
<?php
namespace
yiiunit\data\ar
;
/**
* Model representing tbl_type table
*
* @property int $int_col
* @property int $int_col2 DEFAULT 1
* @property string $char_col
* @property string $char_col2 DEFAULT 'something'
* @property string $char_col3
* @property float $float_col
* @property float $float_col2 DEFAULT '1.23'
* @property string $blob_col
* @property float $numeric_col DEFAULT '33.22'
* @property string $time DEFAULT '2002-01-01 00:00:00'
* @property boolean $bool_col
* @property boolean $bool_col2 DEFAULT 1
*/
class
Type
extends
ActiveRecord
{
/**
* @inheritdoc
*/
public
static
function
tableName
()
{
return
'tbl_type'
;
}
}
\ No newline at end of file
tests/unit/framework/db/ActiveRecordTest.php
View file @
7e5e3155
...
...
@@ -8,6 +8,7 @@ use yiiunit\data\ar\OrderItem;
use
yiiunit\data\ar\Order
;
use
yiiunit\data\ar\Item
;
use
yiiunit\data\ar\Profile
;
use
yiiunit\data\ar\Type
;
use
yiiunit\framework\ar\ActiveRecordTestTrait
;
/**
...
...
@@ -486,4 +487,16 @@ class ActiveRecordTest extends DatabaseTestCase
$this
->
assertTrue
(
$orders
[
1
][
'customer2'
][
'orders2'
][
0
][
'id'
]
===
$orders
[
0
][
'id'
]);
$this
->
assertTrue
(
$orders
[
1
][
'customer2'
][
'orders2'
][
1
][
'id'
]
===
$orders
[
1
][
'id'
]);
}
public
function
testDefaultValues
()
{
$model
=
new
Type
();
$model
->
loadDefaultValues
();
$this
->
assertEquals
(
1
,
$model
->
int_col2
);
$this
->
assertEquals
(
'something'
,
$model
->
char_col2
);
$this
->
assertEquals
(
1.23
,
$model
->
float_col2
);
$this
->
assertEquals
(
33.22
,
$model
->
numeric_col
);
$this
->
assertEquals
(
'2002-01-01 00:00:00'
,
$model
->
time
);
$this
->
assertEquals
(
true
,
$model
->
bool_col2
);
}
}
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