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
f9938ea6
Commit
f9938ea6
authored
May 25, 2008
by
romanb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
custom/magic accessors + test
parent
2429605f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
102 additions
and
9 deletions
+102
-9
ClassMetadata.php
lib/Doctrine/ClassMetadata.php
+1
-1
Entity.php
lib/Doctrine/Entity.php
+34
-7
EntityManager.php
lib/Doctrine/EntityManager.php
+2
-0
AccessorTestCase.php
tests/Orm/Entity/AccessorTestCase.php
+57
-0
ForumUser.php
tests/models/forum/ForumUser.php
+8
-1
No files found.
lib/Doctrine/ClassMetadata.php
View file @
f9938ea6
...
...
@@ -951,7 +951,7 @@ class Doctrine_ClassMetadata extends Doctrine_Configurable implements Serializab
{
$columnName
=
$this
->
getColumnName
(
$fieldName
);
return
isset
(
$this
->
_mappedColumns
[
$columnName
][
'mutator'
])
?
$this
->
_mappedColumns
[
$columnName
][
'mutator'
]
:
null
;
$this
->
_mappedColumns
[
$columnName
][
'mutator'
]
:
null
;
}
/**
...
...
lib/Doctrine/Entity.php
View file @
f9938ea6
...
...
@@ -248,7 +248,7 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite
}
//--
self
::
$_useAutoAccessorOverride
=
fals
e
;
// @todo read from attribute the first time
self
::
$_useAutoAccessorOverride
=
tru
e
;
// @todo read from attribute the first time
}
/**
...
...
@@ -994,6 +994,9 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite
*/
public
function
get
(
$fieldName
,
$load
=
false
)
{
if
(
$getter
=
$this
->
_getCustomAccessor
(
$fieldName
))
{
return
$this
->
$getter
();
}
$this
->
_invokeCustomAccessor
(
$fieldName
);
// Use built-in accessor functionality
...
...
@@ -1027,7 +1030,29 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite
}
}
private
function
_invokeCustomAccessor
(
$fieldName
)
private
function
_getCustomMutator
(
$fieldName
)
{
if
(
!
isset
(
self
::
$_mutatorCache
[
$this
->
_entityName
][
$fieldName
]))
{
if
(
self
::
$_useAutoAccessorOverride
)
{
$setterMethod
=
'set'
.
Doctrine
::
classify
(
$fieldName
);
if
(
method_exists
(
$this
,
$setterMethod
))
{
self
::
$_mutatorCache
[
$this
->
_entityName
][
$fieldName
]
=
$setterMethod
;
}
else
{
self
::
$_mutatorCache
[
$this
->
_entityName
][
$fieldName
]
=
false
;
}
}
if
(
$setter
=
$this
->
_class
->
getCustomMutator
(
$fieldName
))
{
self
::
$_mutatorCache
[
$this
->
_entityName
][
$fieldName
]
=
$setter
;
}
else
if
(
!
isset
(
self
::
$_mutatorCache
[
$this
->
_entityName
][
$fieldName
]))
{
self
::
$_mutatorCache
[
$this
->
_entityName
][
$fieldName
]
=
false
;
}
}
return
self
::
$_mutatorCache
[
$this
->
_entityName
][
$fieldName
];
}
private
function
_getCustomAccessor
(
$fieldName
)
{
if
(
!
isset
(
self
::
$_accessorCache
[
$this
->
_entityName
][
$fieldName
]))
{
if
(
self
::
$_useAutoAccessorOverride
)
{
...
...
@@ -1044,10 +1069,8 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite
self
::
$_accessorCache
[
$this
->
_entityName
][
$fieldName
]
=
false
;
}
}
// invoke custom accessor, if it exists.
if
(
$getter
=
self
::
$_accessorCache
[
$this
->
_entityName
][
$fieldName
])
{
return
$this
->
$getter
();
}
return
self
::
$_accessorCache
[
$this
->
_entityName
][
$fieldName
];
}
public
function
getClassName
()
...
...
@@ -1070,7 +1093,11 @@ abstract class Doctrine_Entity extends Doctrine_Access implements Countable, Ite
* @return Doctrine_Entity
*/
public
function
set
(
$fieldName
,
$value
,
$load
=
false
)
{
{
if
(
$setter
=
$this
->
_getCustomMutator
(
$fieldName
))
{
return
$this
->
$setter
(
$value
);
}
if
(
$this
->
_class
->
hasField
(
$fieldName
))
{
if
(
$value
instanceof
Doctrine_Entity
)
{
$type
=
$this
->
_class
->
getTypeOf
(
$fieldName
);
...
...
lib/Doctrine/EntityManager.php
View file @
f9938ea6
...
...
@@ -19,6 +19,8 @@
* <http://www.phpdoctrine.org>.
*/
#namespace Doctrine::ORM;
/**
* The EntityManager is a central access point to ORM functionality.
*
...
...
tests/Orm/Entity/AccessorTestCase.php
0 → 100644
View file @
f9938ea6
<?php
require_once
'lib/DoctrineTestInit.php'
;
class
Orm_Entity_AccessorTestCase
extends
Doctrine_OrmTestCase
{
public
function
testGetterSetterOverride
()
{
$em
=
new
Doctrine_EntityManager
(
new
Doctrine_Connection_Mock
());
$entity1
=
new
CustomAccessorMutatorTestEntity
();
$entity1
->
username
=
'romanb'
;
$this
->
assertEquals
(
'romanb?!'
,
$entity1
->
username
);
$entity2
=
new
MagicAccessorMutatorTestEntity
();
$entity2
->
username
=
'romanb'
;
$this
->
assertEquals
(
'romanb?!'
,
$entity1
->
username
);
}
}
class
CustomAccessorMutatorTestEntity
extends
Doctrine_Entity
{
public
static
function
initMetadata
(
$class
)
{
$class
->
mapColumn
(
'username'
,
'string'
,
50
,
array
(
'accessor'
=>
'getUsernameCustom'
,
'mutator'
=>
'setUsernameCustom'
));
}
public
function
getUsernameCustom
()
{
return
$this
->
rawGetField
(
'username'
)
.
"!"
;
}
public
function
setUsernameCustom
(
$username
)
{
$this
->
rawSetField
(
'username'
,
$username
.
"?"
);
}
}
class
MagicAccessorMutatorTestEntity
extends
Doctrine_Entity
{
public
static
function
initMetadata
(
$class
)
{
$class
->
mapColumn
(
'username'
,
'string'
,
50
,
array
());
}
public
function
getUsername
()
{
return
$this
->
rawGetField
(
'username'
)
.
"!"
;
}
public
function
setUsername
(
$username
)
{
$this
->
rawSetField
(
'username'
,
$username
.
"?"
);
}
}
\ No newline at end of file
tests/models/forum/ForumUser.php
View file @
f9938ea6
...
...
@@ -20,7 +20,9 @@ class ForumUser extends Doctrine_Entity
$class
->
mapColumn
(
'id'
,
'integer'
,
4
,
array
(
'primary'
=>
true
,
'autoincrement'
=>
true
));
$class
->
mapColumn
(
'username'
,
'string'
,
50
,
array
(
'accessor'
=>
'getUsernameCustom'
));
$class
->
mapColumn
(
'username'
,
'string'
,
50
,
array
(
'accessor'
=>
'getUsernameCustom'
,
'mutator'
=>
'setUsernameCustom'
));
}
...
...
@@ -29,4 +31,9 @@ class ForumUser extends Doctrine_Entity
return
$this
->
rawGetField
(
'username'
)
.
"!"
;
}
public
function
setUsernameCustom
(
$username
)
{
$this
->
rawSetField
(
'username'
,
$username
.
"?"
);
}
}
\ No newline at end of file
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