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
0899b8d4
Commit
0899b8d4
authored
Jun 25, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changed updateAttributes to be more simple update
fixes #3233
parent
2cab70b0
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
28 additions
and
11 deletions
+28
-11
UPGRADE.md
framework/UPGRADE.md
+5
-0
BaseActiveRecord.php
framework/db/BaseActiveRecord.php
+16
-4
Customer.php
tests/unit/data/ar/Customer.php
+2
-2
Customer.php
tests/unit/data/ar/elasticsearch/Customer.php
+2
-2
Customer.php
tests/unit/data/ar/redis/Customer.php
+2
-2
ActiveRecordTestTrait.php
tests/unit/framework/ar/ActiveRecordTestTrait.php
+1
-1
No files found.
framework/UPGRADE.md
View file @
0899b8d4
...
@@ -59,3 +59,7 @@ Upgrade from Yii 2.0 Beta
...
@@ -59,3 +59,7 @@ Upgrade from Yii 2.0 Beta
*
The behavior and signature of
`ActiveRecord::afterSave()`
has changed.
`ActiveRecord::$isNewRecord`
will now always be
*
The behavior and signature of
`ActiveRecord::afterSave()`
has changed.
`ActiveRecord::$isNewRecord`
will now always be
false in afterSave and also dirty attributes are not available. This change has been made to have a more consistent and
false in afterSave and also dirty attributes are not available. This change has been made to have a more consistent and
expected behavior. The changed attributes are now available in the new parameter of afterSave()
`$changedAttributes`
.
expected behavior. The changed attributes are now available in the new parameter of afterSave()
`$changedAttributes`
.
*
`ActiveRecord::updateAttributes()`
has been changed to not trigger events and not respect optimistic locking anymore to
differentiate it more from calling
`update(false)`
and to ensure it can be used in
`afterSave()`
without triggering infinite
loops.
\ No newline at end of file
framework/db/BaseActiveRecord.php
View file @
0899b8d4
...
@@ -644,16 +644,16 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
...
@@ -644,16 +644,16 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* Updates the specified attributes.
* Updates the specified attributes.
*
*
* This method is a shortcut to [[update()]] when data validation is not needed
* This method is a shortcut to [[update()]] when data validation is not needed
* and only a
list of
attributes need to be updated.
* and only a
small set
attributes need to be updated.
*
*
* You may specify the attributes to be updated as name list or name-value pairs.
* You may specify the attributes to be updated as name list or name-value pairs.
* If the latter, the corresponding attribute values will be modified accordingly.
* If the latter, the corresponding attribute values will be modified accordingly.
* The method will then save the specified attributes into database.
* The method will then save the specified attributes into database.
*
*
* Note that this method will
NOT perform data validation
.
* Note that this method will
**not** perform data validation and will **not** trigger events
.
*
*
* @param array $attributes the attributes (names or name-value pairs) to be updated
* @param array $attributes the attributes (names or name-value pairs) to be updated
* @return integer
|boolean the number of rows affected, or false if [[beforeSave()]] stops the updating process
.
* @return integer
the number of rows affected
.
*/
*/
public
function
updateAttributes
(
$attributes
)
public
function
updateAttributes
(
$attributes
)
{
{
...
@@ -666,7 +666,19 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
...
@@ -666,7 +666,19 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
$attrs
[]
=
$name
;
$attrs
[]
=
$name
;
}
}
}
}
return
$this
->
updateInternal
(
$attrs
);
$values
=
$this
->
getDirtyAttributes
(
$attributes
);
if
(
empty
(
$values
))
{
return
0
;
}
$rows
=
$this
->
updateAll
(
$values
,
$this
->
getOldPrimaryKey
(
true
));
foreach
(
$values
as
$name
=>
$value
)
{
$this
->
_oldAttributes
[
$name
]
=
$this
->
_attributes
[
$name
];
}
return
$rows
;
}
}
/**
/**
...
...
tests/unit/data/ar/Customer.php
View file @
0899b8d4
...
@@ -58,11 +58,11 @@ class Customer extends ActiveRecord
...
@@ -58,11 +58,11 @@ class Customer extends ActiveRecord
})
->
orderBy
(
'id'
);
})
->
orderBy
(
'id'
);
}
}
public
function
afterSave
(
$insert
)
public
function
afterSave
(
$insert
,
$changedAttributes
)
{
{
ActiveRecordTest
::
$afterSaveInsert
=
$insert
;
ActiveRecordTest
::
$afterSaveInsert
=
$insert
;
ActiveRecordTest
::
$afterSaveNewRecord
=
$this
->
isNewRecord
;
ActiveRecordTest
::
$afterSaveNewRecord
=
$this
->
isNewRecord
;
parent
::
afterSave
(
$insert
);
parent
::
afterSave
(
$insert
,
$changedAttributes
);
}
}
/**
/**
...
...
tests/unit/data/ar/elasticsearch/Customer.php
View file @
0899b8d4
...
@@ -40,11 +40,11 @@ class Customer extends ActiveRecord
...
@@ -40,11 +40,11 @@ class Customer extends ActiveRecord
return
$this
->
hasMany
(
OrderWithNullFK
::
className
(),
[
'customer_id'
=>
'id'
])
->
orderBy
(
'created_at'
);
return
$this
->
hasMany
(
OrderWithNullFK
::
className
(),
[
'customer_id'
=>
'id'
])
->
orderBy
(
'created_at'
);
}
}
public
function
afterSave
(
$insert
)
public
function
afterSave
(
$insert
,
$changedAttributes
)
{
{
ActiveRecordTest
::
$afterSaveInsert
=
$insert
;
ActiveRecordTest
::
$afterSaveInsert
=
$insert
;
ActiveRecordTest
::
$afterSaveNewRecord
=
$this
->
isNewRecord
;
ActiveRecordTest
::
$afterSaveNewRecord
=
$this
->
isNewRecord
;
parent
::
afterSave
(
$insert
);
parent
::
afterSave
(
$insert
,
$changedAttributes
);
}
}
/**
/**
...
...
tests/unit/data/ar/redis/Customer.php
View file @
0899b8d4
...
@@ -38,11 +38,11 @@ class Customer extends ActiveRecord
...
@@ -38,11 +38,11 @@ class Customer extends ActiveRecord
/**
/**
* @inheritdoc
* @inheritdoc
*/
*/
public
function
afterSave
(
$insert
)
public
function
afterSave
(
$insert
,
$changedAttributes
)
{
{
ActiveRecordTest
::
$afterSaveInsert
=
$insert
;
ActiveRecordTest
::
$afterSaveInsert
=
$insert
;
ActiveRecordTest
::
$afterSaveNewRecord
=
$this
->
isNewRecord
;
ActiveRecordTest
::
$afterSaveNewRecord
=
$this
->
isNewRecord
;
parent
::
afterSave
(
$insert
);
parent
::
afterSave
(
$insert
,
$changedAttributes
);
}
}
/**
/**
...
...
tests/unit/framework/ar/ActiveRecordTestTrait.php
View file @
0899b8d4
...
@@ -816,7 +816,7 @@ trait ActiveRecordTestTrait
...
@@ -816,7 +816,7 @@ trait ActiveRecordTestTrait
$this
->
afterSave
();
$this
->
afterSave
();
$this
->
assertNotNull
(
$customer
->
id
);
$this
->
assertNotNull
(
$customer
->
id
);
$this
->
assert
Tru
e
(
static
::
$afterSaveNewRecord
);
$this
->
assert
Fals
e
(
static
::
$afterSaveNewRecord
);
$this
->
assertTrue
(
static
::
$afterSaveInsert
);
$this
->
assertTrue
(
static
::
$afterSaveInsert
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
$this
->
assertFalse
(
$customer
->
isNewRecord
);
}
}
...
...
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