Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Tomáš Trávníček
doctrine-dbal
Commits
a1a80938
Commit
a1a80938
authored
Feb 24, 2010
by
romanb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] First draft for onFlush event.
parent
7badced1
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
229 additions
and
9 deletions
+229
-9
AnnotationReader.php
lib/Doctrine/Common/Annotations/AnnotationReader.php
+3
-3
Parser.php
lib/Doctrine/Common/Annotations/Parser.php
+1
-1
LifecycleEventArgs.php
lib/Doctrine/ORM/Event/LifecycleEventArgs.php
+2
-1
OnFlushEventArgs.php
lib/Doctrine/ORM/Event/OnFlushEventArgs.php
+78
-0
Events.php
lib/Doctrine/ORM/Events.php
+11
-0
StandardEntityPersister.php
lib/Doctrine/ORM/Persisters/StandardEntityPersister.php
+2
-2
UnitOfWork.php
lib/Doctrine/ORM/UnitOfWork.php
+37
-1
ParserTest.php
tests/Doctrine/Tests/Common/Annotations/ParserTest.php
+1
-1
FlushEventTest.php
tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php
+94
-0
No files found.
lib/Doctrine/Common/Annotations/AnnotationReader.php
View file @
a1a80938
...
...
@@ -72,7 +72,7 @@ class AnnotationReader
$this
->
_parser
=
new
Parser
;
$this
->
_cache
=
$cache
?:
new
Doctrine\Common\Cache\ArrayCache
;
}
/**
* Sets the default namespace that the AnnotationReader should assume for annotations
* with not fully qualified names.
...
...
@@ -83,7 +83,7 @@ class AnnotationReader
{
$this
->
_parser
->
setDefaultAnnotationNamespace
(
$defaultNamespace
);
}
/**
* Sets an alias for an annotation namespace.
*
...
...
@@ -94,7 +94,7 @@ class AnnotationReader
{
$this
->
_parser
->
setAnnotationNamespaceAlias
(
$namespace
,
$alias
);
}
/**
* Gets the annotations applied to a class.
*
...
...
lib/Doctrine/Common/Annotations/Parser.php
View file @
a1a80938
...
...
@@ -249,7 +249,7 @@ class Parser
$name
=
implode
(
'\\'
,
$nameParts
);
}
// I
f
it really an annotation class?
// I
s
it really an annotation class?
if
(
(
!
$this
->
_isNestedAnnotation
&&
$this
->
_lexer
->
lookahead
!=
null
&&
!
$this
->
_lexer
->
isNextToken
(
Lexer
::
T_OPEN_PARENTHESIS
)
&&
...
...
lib/Doctrine/ORM/Event/LifecycleEventArgs.php
View file @
a1a80938
...
...
@@ -22,7 +22,8 @@
namespace
Doctrine\ORM\Event
;
/**
* Lifecycle Events are triggered by the UnitOfWork
* Lifecycle Events are triggered by the UnitOfWork during lifecycle transitions
* of entities.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
...
...
lib/Doctrine/ORM/Event/OnFlushEventArgs.php
0 → 100644
View file @
a1a80938
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace
Doctrine\ORM\Event
;
/**
* Provides event arguments for the preFlush event.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.0
* @version $Revision$
* @author Roman Borschel <roman@code-factory.de>
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class
OnFlushEventArgs
extends
\Doctrine\Common\EventArgs
{
/**
* @var EntityManager
*/
private
$_em
;
//private $_entitiesToPersist = array();
//private $_entitiesToRemove = array();
public
function
__construct
(
$em
)
{
$this
->
_em
=
$em
;
}
/**
* @return EntityManager
*/
public
function
getEntityManager
()
{
return
$this
->
_em
;
}
/*
public function addEntityToPersist($entity)
{
}
public function addEntityToRemove($entity)
{
}
public function addEntityToUpdate($entity)
{
}
public function getEntitiesToPersist()
{
return $this->_entitiesToPersist;
}
*/
}
\ No newline at end of file
lib/Doctrine/ORM/Events.php
View file @
a1a80938
...
...
@@ -108,4 +108,15 @@ final class Events
* @var string
*/
const
loadClassMetadata
=
'loadClassMetadata'
;
/**
* The onFlush event occurs when the EntityManager#flush() operation is invoked,
* after any changes to managed entities have been determined but before any
* actual database operations are executed. The event is only raised if there is
* actually something to do for the underlying UnitOfWork. If nothing needs to be done,
* the onFlush event is not raised.
*
* @var string
*/
const
onFlush
=
'onFlush'
;
}
\ No newline at end of file
lib/Doctrine/ORM/Persisters/StandardEntityPersister.php
View file @
a1a80938
...
...
@@ -339,10 +339,10 @@ class StandardEntityPersister
if
(
isset
(
$this
->
_class
->
associationMappings
[
$field
]))
{
$assocMapping
=
$this
->
_class
->
associationMappings
[
$field
];
// Only owning side of x-1 associations can have a FK column.
if
(
!
$assocMapping
->
isO
neToOne
()
||
!
$assocMapping
->
isOwningSide
)
{
if
(
!
$assocMapping
->
isO
wningSide
||
!
$assocMapping
->
isOneToOne
()
)
{
continue
;
}
if
(
$newVal
!==
null
)
{
$oid
=
spl_object_hash
(
$newVal
);
if
(
isset
(
$this
->
_queuedInserts
[
$oid
])
||
$uow
->
isScheduledForInsert
(
$newVal
))
{
...
...
lib/Doctrine/ORM/UnitOfWork.php
View file @
a1a80938
...
...
@@ -271,6 +271,11 @@ class UnitOfWork implements PropertyChangedListener
}
}
// Raise onFlush
if
(
$this
->
_evm
->
hasListeners
(
Events
::
onFlush
))
{
$this
->
_evm
->
dispatchEvent
(
Events
::
onFlush
,
new
Event\OnFlushEventArgs
(
$this
->
_em
));
}
// Now we need a commit order to maintain referential integrity
$commitOrder
=
$this
->
_getCommitOrder
();
...
...
@@ -945,7 +950,8 @@ class UnitOfWork implements PropertyChangedListener
}
/**
* Registers a deleted entity.
* INTERNAL:
* Schedules an entity for deletion.
*
* @param object $entity
*/
...
...
@@ -2064,4 +2070,34 @@ class UnitOfWork implements PropertyChangedListener
$this
->
_entityUpdates
[
$oid
]
=
$entity
;
}
}
/**
* Gets the currently scheduled entity insertions in this UnitOfWork.
*
* @return array
*/
public
function
getScheduledEntityInsertions
()
{
return
$this
->
_entityInsertions
;
}
/**
* Gets the currently scheduled entity updates in this UnitOfWork.
*
* @return array
*/
public
function
getScheduledEntityUpdates
()
{
return
$this
->
_entityUpdates
;
}
/**
* Gets the currently scheduled entity deletions in this UnitOfWork.
*
* @return array
*/
public
function
getScheduledEntityDeletions
()
{
return
$this
->
_entityDeletions
;
}
}
tests/Doctrine/Tests/Common/Annotations/ParserTest.php
View file @
a1a80938
...
...
@@ -106,7 +106,7 @@ DOCBLOCK;
*/
public
function
testAnnotationNamespaceAlias
()
{
$parser
=
new
Parser
;
$parser
=
$this
->
createTestParser
()
;
$parser
->
setAnnotationNamespaceAlias
(
'Doctrine\Tests\Common\Annotations\\'
,
'alias'
);
$docblock
=
<<<DOCBLOCK
/**
...
...
tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php
0 → 100644
View file @
a1a80938
<?php
namespace
Doctrine\Tests\ORM\Functional
;
use
Doctrine\Tests\Models\CMS\CmsUser
;
use
Doctrine\Tests\Models\CMS\CmsPhonenumber
;
use
Doctrine\ORM\Event\OnFlushEventArgs
;
use
Doctrine\ORM\Events
;
require_once
__DIR__
.
'/../../TestInit.php'
;
/**
* FlushEventTest
*
* @author robo
*/
class
FlushEventTest
extends
\Doctrine\Tests\OrmFunctionalTestCase
{
protected
function
setUp
()
{
$this
->
useModelSet
(
'cms'
);
parent
::
setUp
();
}
public
function
testPersistNewEntitiesOnPreFlush
()
{
//$this->_em->getConnection()->getConfiguration()->setSqlLogger(new \Doctrine\DBAL\Logging\EchoSqlLogger);
$this
->
_em
->
getEventManager
()
->
addEventListener
(
Events
::
onFlush
,
new
OnFlushListener
);
$user
=
new
CmsUser
;
$user
->
username
=
'romanb'
;
$user
->
name
=
'Roman'
;
$user
->
status
=
'Dev'
;
$this
->
_em
->
persist
(
$user
);
$this
->
assertEquals
(
0
,
$user
->
phonenumbers
->
count
());
$this
->
_em
->
flush
();
$this
->
assertEquals
(
1
,
$user
->
phonenumbers
->
count
());
$this
->
assertTrue
(
$this
->
_em
->
contains
(
$user
->
phonenumbers
->
get
(
0
)));
$this
->
assertTrue
(
$user
->
phonenumbers
->
get
(
0
)
->
getUser
()
===
$user
);
$this
->
assertFalse
(
$user
->
phonenumbers
->
isDirty
());
// Can be used together with SQL Logging to check that a subsequent flush has
// nothing to do. This proofs the correctness of the changes that happened in onFlush.
//echo "SECOND FLUSH";
//$this->_em->flush();
}
}
class
OnFlushListener
{
public
function
onFlush
(
OnFlushEventArgs
$args
)
{
//echo "---preFlush".PHP_EOL;
$em
=
$args
->
getEntityManager
();
$uow
=
$em
->
getUnitOfWork
();
foreach
(
$uow
->
getScheduledEntityInsertions
()
as
$entity
)
{
if
(
$entity
instanceof
CmsUser
)
{
// Adds a phonenumber to every newly persisted CmsUser ...
$phone
=
new
CmsPhonenumber
;
$phone
->
phonenumber
=
12345
;
// Update object model
$entity
->
addPhonenumber
(
$phone
);
// Invoke regular persist call
$em
->
persist
(
$phone
);
// Explicitly calculate the changeset since onFlush is raised
// after changeset calculation!
$uow
->
computeChangeSet
(
$em
->
getClassMetadata
(
get_class
(
$phone
)),
$phone
);
// Take a snapshot because the UoW wont do this for us, because
// the UoW did not visit this collection.
// Alternatively we could provide an ->addVisitedCollection() method
// on the UoW.
$entity
->
getPhonenumbers
()
->
takeSnapshot
();
}
/*foreach ($uow->getEntityChangeSet($entity) as $field => $change) {
list ($old, $new) = $change;
var_dump($old);
}*/
}
}
}
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