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
73ad0ac4
Commit
73ad0ac4
authored
Jan 09, 2009
by
romanb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more tests and first basic update/delete
parent
b6e385d2
Changes
12
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
221 additions
and
85 deletions
+221
-85
EntityManager.php
lib/Doctrine/ORM/EntityManager.php
+2
-2
ClassExporter.php
lib/Doctrine/ORM/Export/ClassExporter.php
+17
-0
AssociationMapping.php
lib/Doctrine/ORM/Mapping/AssociationMapping.php
+5
-1
AnnotationDriver.php
lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
+1
-0
AbstractEntityPersister.php
lib/Doctrine/ORM/Persisters/AbstractEntityPersister.php
+29
-16
UnitOfWork.php
lib/Doctrine/ORM/UnitOfWork.php
+112
-41
BasicCRUDTest.php
tests/Orm/Functional/BasicCRUDTest.php
+25
-17
UnitOfWorkTest.php
tests/Orm/UnitOfWorkTest.php
+11
-5
Doctrine_EntityManagerMock.php
tests/lib/mocks/Doctrine_EntityManagerMock.php
+1
-0
Doctrine_UnitOfWorkMock.php
tests/lib/mocks/Doctrine_UnitOfWorkMock.php
+2
-2
CmsPhonenumber.php
tests/models/cms/CmsPhonenumber.php
+6
-1
CmsUser.php
tests/models/cms/CmsUser.php
+10
-0
No files found.
lib/Doctrine/ORM/EntityManager.php
View file @
73ad0ac4
...
...
@@ -443,9 +443,9 @@ class Doctrine_ORM_EntityManager
/**
* Deletes the persistent state of the given entity.
*
* @param
Doctrine\ORM\Entity
$entity
* @param
object
$entity
*/
public
function
delete
(
Doctrine_ORM_Entity
$entity
)
public
function
delete
(
$entity
)
{
$this
->
_errorIfNotActiveOrClosed
();
$this
->
_unitOfWork
->
delete
(
$entity
);
...
...
lib/Doctrine/ORM/Export/ClassExporter.php
View file @
73ad0ac4
...
...
@@ -80,6 +80,23 @@ class Doctrine_ORM_Export_ClassExporter
$columns
[
$mapping
[
'columnName'
]]
=
$column
;
}
foreach
(
$class
->
getAssociationMappings
()
as
$mapping
)
{
if
(
$mapping
->
isOneToOne
()
&&
$mapping
->
isOwningSide
())
{
foreach
(
$mapping
->
getSourceToTargetKeyColumns
()
as
$sourceColumn
=>
$targetColumn
)
{
$column
=
array
();
$column
[
'name'
]
=
$sourceColumn
;
$column
[
'type'
]
=
$this
->
_em
->
getClassMetadata
(
$mapping
->
getTargetEntityName
())
->
getTypeOfColumn
(
$targetColumn
);
$columns
[
$sourceColumn
]
=
$column
;
}
}
else
if
(
$mapping
->
isOneToMany
()
&&
$mapping
->
usesJoinTable
())
{
//... create join table, one-many through join table supported later
throw
new
Doctrine_Exception
(
"Not yet implemented."
);
}
else
if
(
$mapping
->
isManyToMany
()
&&
$mapping
->
isOwningSide
())
{
//... create join table
}
}
$this
->
_sm
->
createTable
(
$class
->
getTableName
(),
$columns
,
$options
);
}
}
...
...
lib/Doctrine/ORM/Mapping/AssociationMapping.php
View file @
73ad0ac4
...
...
@@ -26,7 +26,6 @@
*
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
* @todo Rename to AssociationMapping.
*/
abstract
class
Doctrine_ORM_Mapping_AssociationMapping
{
...
...
@@ -362,6 +361,11 @@ abstract class Doctrine_ORM_Mapping_AssociationMapping
return
false
;
}
public
function
usesJoinTable
()
{
return
(
bool
)
$this
->
_joinTable
;
}
abstract
public
function
lazyLoadFor
(
$entity
,
$entityManager
);
}
lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
View file @
73ad0ac4
...
...
@@ -77,6 +77,7 @@ class Doctrine_ORM_Mapping_Driver_AnnotationDriver {
}
else
if
(
$oneToManyAnnot
=
$property
->
getAnnotation
(
'DoctrineOneToMany'
))
{
$mapping
[
'mappedBy'
]
=
$oneToManyAnnot
->
mappedBy
;
$mapping
[
'targetEntity'
]
=
$oneToManyAnnot
->
targetEntity
;
$mapping
[
'cascade'
]
=
$oneToManyAnnot
->
cascade
;
$metadata
->
mapOneToMany
(
$mapping
);
}
else
if
(
$manyToOneAnnot
=
$property
->
getAnnotation
(
'DoctrineManyToOne'
))
{
$mapping
[
'joinColumns'
]
=
$manyToOneAnnot
->
joinColumns
;
...
...
lib/Doctrine/ORM/Persisters/AbstractEntityPersister.php
View file @
73ad0ac4
...
...
@@ -100,11 +100,17 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
/**
* Updates an entity.
*
* @param
Doctrine\ORM\Entity
$entity The entity to update.
* @param
object
$entity The entity to update.
* @return void
*/
public
function
update
(
Doctrine_ORM_Entity
$entity
)
public
function
update
(
$entity
)
{
$updateData
=
array
();
$this
->
_prepareData
(
$entity
,
$updateData
);
$id
=
array_combine
(
$this
->
_classMetadata
->
getIdentifierFieldNames
(),
$this
->
_em
->
getUnitOfWork
()
->
getEntityIdentifier
(
$entity
));
$this
->
_conn
->
update
(
$this
->
_classMetadata
->
getTableName
(),
$updateData
,
$id
);
/*$dataChangeSet = $entity->_getDataChangeSet();
$referenceChangeSet = $entity->_getReferenceChangeSet();
...
...
@@ -126,12 +132,14 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
/**
* Deletes an entity.
*
* @param
Doctrine\ORM\Entity
$entity The entity to delete.
* @param
object
$entity The entity to delete.
* @return void
*/
public
function
delete
(
Doctrine_ORM_Entity
$entity
)
public
function
delete
(
$entity
)
{
//TODO: perform delete
$id
=
array_combine
(
$this
->
_classMetadata
->
getIdentifierFieldNames
(),
$this
->
_em
->
getUnitOfWork
()
->
getEntityIdentifier
(
$entity
));
$this
->
_conn
->
delete
(
$this
->
_classMetadata
->
getTableName
(),
$id
);
}
/**
...
...
@@ -232,7 +240,7 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
*/
protected
function
_prepareData
(
$entity
,
array
&
$result
,
$isInsert
=
false
)
{
foreach
(
$this
->
_em
->
getUnitOfWork
()
->
get
Data
ChangeSet
(
$entity
)
as
$field
=>
$change
)
{
foreach
(
$this
->
_em
->
getUnitOfWork
()
->
get
Entity
ChangeSet
(
$entity
)
as
$field
=>
$change
)
{
if
(
is_array
(
$change
))
{
list
(
$oldVal
,
$newVal
)
=
each
(
$change
);
}
else
{
...
...
@@ -244,16 +252,21 @@ abstract class Doctrine_ORM_Persisters_AbstractEntityPersister
$columnName
=
$this
->
_classMetadata
->
getColumnName
(
$field
);
if
(
$this
->
_classMetadata
->
hasAssociation
(
$field
))
{
$assocMapping
=
$this
->
_classMetadata
->
getAssociationMapping
(
$field
);
if
(
!
$assocMapping
->
isOneToOne
()
||
$assocMapping
->
isInverseSide
())
{
//echo "NOT TO-ONE OR INVERSE!";
continue
;
}
foreach
(
$assocMapping
->
getSourceToTargetKeyColumns
()
as
$sourceColumn
=>
$targetColumn
)
{
//TODO: throw exc if field not set
$otherClass
=
$this
->
_em
->
getClassMetadata
(
$assocMapping
->
getTargetEntityName
());
$result
[
$sourceColumn
]
=
$otherClass
->
getReflectionProperty
(
$otherClass
->
getFieldName
(
$targetColumn
))
->
getValue
(
$newVal
);
if
(
$newVal
!==
null
)
{
$assocMapping
=
$this
->
_classMetadata
->
getAssociationMapping
(
$field
);
if
(
!
$assocMapping
->
isOneToOne
()
||
$assocMapping
->
isInverseSide
())
{
//echo "NOT TO-ONE OR INVERSE!";
continue
;
}
//echo "HERE!!!";
foreach
(
$assocMapping
->
getSourceToTargetKeyColumns
()
as
$sourceColumn
=>
$targetColumn
)
{
//TODO: throw exc if field not set
$otherClass
=
$this
->
_em
->
getClassMetadata
(
$assocMapping
->
getTargetEntityName
());
$result
[
$sourceColumn
]
=
$otherClass
->
getReflectionProperty
(
$otherClass
->
getFieldName
(
$targetColumn
))
->
getValue
(
$newVal
);
}
}
else
if
(
!
$isInsert
)
{
echo
"NO INSERT AND NEWVAL NULL ON 1-1 ASSOC, OWNING SIDE"
;
}
}
else
if
(
is_null
(
$newVal
))
{
$result
[
$columnName
]
=
null
;
...
...
lib/Doctrine/ORM/UnitOfWork.php
View file @
73ad0ac4
This diff is collapsed.
Click to expand it.
tests/Orm/Functional/BasicCRUDTest.php
View file @
73ad0ac4
...
...
@@ -8,14 +8,14 @@ require_once 'lib/DoctrineTestInit.php';
* @author robo
*/
class
Orm_Functional_BasicCRUDTest
extends
Doctrine_OrmFunctionalTestCase
{
public
function
test
Foo
()
{
public
function
test
SingleEntityCRUD
()
{
$em
=
$this
->
_getEntityManager
();
$exporter
=
new
Doctrine_ORM_Export_ClassExporter
(
$em
);
$exporter
->
exportClasses
(
array
(
$em
->
getClassMetadata
(
'CmsUser'
),
$em
->
getClassMetadata
(
'CmsPhonenumber'
)
));
$em
->
getClassMetadata
(
'CmsUser'
),
$em
->
getClassMetadata
(
'CmsPhonenumber'
)
));
// Create
$user
=
new
CmsUser
;
...
...
@@ -24,25 +24,33 @@ class Orm_Functional_BasicCRUDTest extends Doctrine_OrmFunctionalTestCase {
$this
->
assertTrue
(
is_numeric
(
$user
->
id
));
$this
->
assertTrue
(
$em
->
contains
(
$user
));
$user2
=
new
CmsUser
;
$user2
->
name
=
'jwage'
;
$em
->
save
(
$user2
);
$this
->
assertTrue
(
is_numeric
(
$user2
->
id
));
$this
->
assertTrue
(
$em
->
contains
(
$user2
));
// Read
$user3
=
$em
->
find
(
'CmsUser'
,
$user
->
id
);
$this
->
assertTrue
(
$user
===
$user3
);
$user4
=
$em
->
find
(
'CmsUser'
,
$user2
->
id
);
$this
->
assertTrue
(
$user2
===
$user4
);
$user2
=
$em
->
find
(
'CmsUser'
,
$user
->
id
);
$this
->
assertTrue
(
$user
===
$user2
);
// Add a phonenumber
$ph
=
new
CmsPhonenumber
;
$ph
->
phonenumber
=
"12345"
;
$user
->
addPhonenumber
(
$ph
);
$em
->
flush
();
$this
->
assertTrue
(
$em
->
contains
(
$ph
));
$this
->
assertTrue
(
$em
->
contains
(
$user
));
$user
->
phonenumbers
[]
=
$ph
;
// Update
$user
->
name
=
'guilherme'
;
$em
->
flush
();
$this
->
assertEquals
(
'guilherme'
,
$user
->
name
);
// Delete
$em
->
delete
(
$user
);
$this
->
assertTrue
(
$em
->
getUnitOfWork
()
->
isRegisteredRemoved
(
$user
));
$em
->
flush
();
$this
->
assertFalse
(
$em
->
getUnitOfWork
()
->
isRegisteredRemoved
(
$user
));
}
//var_dump($em->getUnitOfWork())
public
function
testMore
()
{
}
}
tests/Orm/UnitOfWorkTest.php
View file @
73ad0ac4
...
...
@@ -119,15 +119,21 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
$this
->
assertEquals
(
0
,
count
(
$avatarPersister
->
getDeletes
()));
}
public
function
testCompute
DataChangeSet
()
public
function
testCompute
EntityChangeSets
()
{
// We need an ID generator for ForumAvatar, because we attach a NEW ForumAvatar
// to a (faked) MANAGED instance. During changeset computation this will result
// in the UnitOfWork requesting the Id generator of ForumAvatar.
$avatarIdGeneratorMock
=
new
Doctrine_IdentityIdGeneratorMock
(
$this
->
_emMock
);
$this
->
_emMock
->
setIdGenerator
(
'ForumAvatar'
,
$avatarIdGeneratorMock
);
$user1
=
new
ForumUser
();
$user1
->
id
=
1
;
$user1
->
username
=
"romanb"
;
$user1
->
avatar
=
new
ForumAvatar
();
// Fake managed state
$this
->
_unitOfWork
->
setEntityState
(
$user1
,
Doctrine_ORM_UnitOfWork
::
STATE_MANAGED
);
$user2
=
new
ForumUser
();
$user2
->
id
=
2
;
$user2
->
username
=
"jwage"
;
...
...
@@ -143,10 +149,10 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
));
// Go
$this
->
_unitOfWork
->
compute
DataChangeSet
(
array
(
$user1
,
$user2
));
$this
->
_unitOfWork
->
compute
EntityChangeSets
(
array
(
$user1
,
$user2
));
// Verify
$user1ChangeSet
=
$this
->
_unitOfWork
->
get
Data
ChangeSet
(
$user1
);
$user1ChangeSet
=
$this
->
_unitOfWork
->
get
Entity
ChangeSet
(
$user1
);
$this
->
assertTrue
(
is_array
(
$user1ChangeSet
));
$this
->
assertEquals
(
2
,
count
(
$user1ChangeSet
));
$this
->
assertTrue
(
isset
(
$user1ChangeSet
[
'username'
]));
...
...
@@ -154,7 +160,7 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
$this
->
assertTrue
(
isset
(
$user1ChangeSet
[
'avatar'
]));
$this
->
assertSame
(
array
(
null
=>
$user1
->
avatar
),
$user1ChangeSet
[
'avatar'
]);
$user2ChangeSet
=
$this
->
_unitOfWork
->
get
Data
ChangeSet
(
$user2
);
$user2ChangeSet
=
$this
->
_unitOfWork
->
get
Entity
ChangeSet
(
$user2
);
$this
->
assertTrue
(
is_array
(
$user2ChangeSet
));
$this
->
assertEquals
(
1
,
count
(
$user2ChangeSet
));
$this
->
assertTrue
(
isset
(
$user2ChangeSet
[
'username'
]));
...
...
tests/lib/mocks/Doctrine_EntityManagerMock.php
View file @
73ad0ac4
...
...
@@ -81,6 +81,7 @@ class Doctrine_EntityManagerMock extends Doctrine_ORM_EntityManager
$this
->
_idGenerators
[
$className
]
=
$generator
;
}
/** @override */
public
function
getIdGenerator
(
$className
)
{
if
(
isset
(
$this
->
_idGenerators
[
$className
]))
{
...
...
tests/lib/mocks/Doctrine_UnitOfWorkMock.php
View file @
73ad0ac4
...
...
@@ -14,10 +14,10 @@ class Doctrine_UnitOfWorkMock extends Doctrine_ORM_UnitOfWork {
* @param <type> $entity
* @override
*/
public
function
get
Data
ChangeSet
(
$entity
)
{
public
function
get
Entity
ChangeSet
(
$entity
)
{
$oid
=
spl_object_hash
(
$entity
);
return
isset
(
$this
->
_mockDataChangeSets
[
$oid
])
?
$this
->
_mockDataChangeSets
[
$oid
]
:
parent
::
get
Data
ChangeSet
(
$entity
);
$this
->
_mockDataChangeSets
[
$oid
]
:
parent
::
get
Entity
ChangeSet
(
$entity
);
}
/* MOCK API */
...
...
tests/models/cms/CmsPhonenumber.php
View file @
73ad0ac4
...
...
@@ -3,7 +3,7 @@
/**
* @DoctrineEntity
*/
class
CmsPhonenumber
implements
Doctrine_ORM_Entity
class
CmsPhonenumber
{
/**
* @DoctrineColumn(type="varchar", length=50)
...
...
@@ -14,4 +14,9 @@ class CmsPhonenumber implements Doctrine_ORM_Entity
* @DoctrineManyToOne(targetEntity="CmsUser", joinColumns={"user_id" = "id"})
*/
public
$user
;
public
function
setUser
(
CmsUser
$user
)
{
$this
->
user
=
$user
;
$user
->
addPhonenumber
(
$this
);
}
}
tests/models/cms/CmsUser.php
View file @
73ad0ac4
...
...
@@ -33,4 +33,14 @@ class CmsUser
* @DoctrineOneToMany(targetEntity="CmsArticle", mappedBy="user")
*/
public
$articles
;
/**
* Adds a phonenumber to the user.
*
* @param <type> $phone
*/
public
function
addPhonenumber
(
CmsPhonenumber
$phone
)
{
$this
->
phonenumbers
[]
=
$phone
;
$phone
->
user
=
$this
;
}
}
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