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
74ce82bd
Commit
74ce82bd
authored
May 18, 2008
by
romanb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial HYDRATE_SINGLE_SCALAR implementation & test.
parent
d0ea5705
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
111 additions
and
2 deletions
+111
-2
Exception.php
lib/Doctrine/Hydrator/Exception.php
+6
-1
HydratorNew.php
lib/Doctrine/HydratorNew.php
+17
-1
BasicHydrationTest.php
tests/Orm/Hydration/BasicHydrationTest.php
+80
-0
Doctrine_HydratorMockStatement.php
tests/lib/mocks/Doctrine_HydratorMockStatement.php
+8
-0
No files found.
lib/Doctrine/Hydrator/Exception.php
View file @
74ce82bd
...
@@ -18,7 +18,7 @@
...
@@ -18,7 +18,7 @@
* and is licensed under the LGPL. For more information, see
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
* <http://www.phpdoctrine.org>.
*/
*/
Doctrine
::
autoload
(
'Doctrine_Exception'
);
/**
/**
* Doctrine_Hydrator_Exception
* Doctrine_Hydrator_Exception
*
*
...
@@ -41,4 +41,9 @@ class Doctrine_Hydrator_Exception extends Doctrine_Exception
...
@@ -41,4 +41,9 @@ class Doctrine_Hydrator_Exception extends Doctrine_Exception
{
{
return
new
self
(
"Hydration failed. Found a non-existent field '
$field
'."
);
return
new
self
(
"Hydration failed. Found a non-existent field '
$field
'."
);
}
}
public
static
function
nonUniqueResult
()
{
return
new
self
(
"Hydration failed. Non-unique result returned."
);
}
}
}
\ No newline at end of file
lib/Doctrine/HydratorNew.php
View file @
74ce82bd
...
@@ -77,6 +77,7 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
...
@@ -77,6 +77,7 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
* )
* )
* )
* )
* @return mixed The created object/array graph.
* @return mixed The created object/array graph.
* @throws Doctrine_Hydrator_Exception If the hydration process failed.
*/
*/
public
function
hydrateResultSet
(
$parserResult
)
public
function
hydrateResultSet
(
$parserResult
)
{
{
...
@@ -147,9 +148,20 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
...
@@ -147,9 +148,20 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
$idTemplate
[
$dqlAlias
]
=
''
;
$idTemplate
[
$dqlAlias
]
=
''
;
}
}
// Process result set
$cache
=
array
();
$cache
=
array
();
// Evaluate HYDRATE_SINGLE_SCALAR
if
(
$hydrationMode
==
Doctrine
::
HYDRATE_SINGLE_SCALAR
)
{
$result
=
$stmt
->
fetchAll
(
PDO
::
FETCH_ASSOC
);
if
(
count
(
$result
)
>
1
||
count
(
$result
[
0
])
>
1
)
{
throw
Doctrine_Hydrator_Exception
::
nonUniqueResult
();
}
return
array_shift
(
$this
->
_gatherScalarRowData
(
$result
[
0
],
$cache
));
}
// Process result set
while
(
$data
=
$stmt
->
fetch
(
Doctrine
::
FETCH_ASSOC
))
{
while
(
$data
=
$stmt
->
fetch
(
Doctrine
::
FETCH_ASSOC
))
{
// Evaluate HYDRATE_SCALAR
if
(
$hydrationMode
==
Doctrine
::
HYDRATE_SCALAR
)
{
if
(
$hydrationMode
==
Doctrine
::
HYDRATE_SCALAR
)
{
$result
[]
=
$this
->
_gatherScalarRowData
(
$data
,
$cache
);
$result
[]
=
$this
->
_gatherScalarRowData
(
$data
,
$cache
);
continue
;
continue
;
...
@@ -372,6 +384,8 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
...
@@ -372,6 +384,8 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
*
*
* @return array An array with all the fields (name => value) of the data row,
* @return array An array with all the fields (name => value) of the data row,
* grouped by their component (alias).
* grouped by their component (alias).
* @todo Significant code duplication with _gatherScalarRowData(). Good refactoring
* possible without sacrificing performance?
*/
*/
protected
function
_gatherRowData
(
&
$data
,
&
$cache
,
&
$id
,
&
$nonemptyComponents
)
protected
function
_gatherRowData
(
&
$data
,
&
$cache
,
&
$id
,
&
$nonemptyComponents
)
{
{
...
@@ -455,6 +469,8 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
...
@@ -455,6 +469,8 @@ class Doctrine_HydratorNew extends Doctrine_Hydrator_Abstract
* @param array $data
* @param array $data
* @param array $cache
* @param array $cache
* @return array The processed row.
* @return array The processed row.
* @todo Significant code duplication with _gatherRowData(). Good refactoring
* possible without sacrificing performance?
*/
*/
private
function
_gatherScalarRowData
(
&
$data
,
&
$cache
)
private
function
_gatherScalarRowData
(
&
$data
,
&
$cache
)
{
{
...
...
tests/Orm/Hydration/BasicHydrationTest.php
View file @
74ce82bd
...
@@ -856,6 +856,86 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
...
@@ -856,6 +856,86 @@ class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
}
}
/** Result set provider for the HYDRATE_SINGLE_SCALAR tests */
public
static
function
singleScalarResultSetProvider
()
{
return
array
(
// valid
array
(
'name'
=>
'result1'
,
'resultSet'
=>
array
(
array
(
'u__name'
=>
'romanb'
)
)),
// valid
array
(
'name'
=>
'result2'
,
'resultSet'
=>
array
(
array
(
'u__id'
=>
'1'
)
)),
// invalid
array
(
'name'
=>
'result3'
,
'resultSet'
=>
array
(
array
(
'u__id'
=>
'1'
,
'u__name'
=>
'romanb'
)
)),
// invalid
array
(
'name'
=>
'result4'
,
'resultSet'
=>
array
(
array
(
'u__id'
=>
'1'
),
array
(
'u__id'
=>
'2'
)
)),
);
}
/**
* select u.name from CmsUser u where u.id = 1
*
* @dataProvider singleScalarResultSetProvider
*/
public
function
testHydrateSingleScalar
(
$name
,
$resultSet
)
{
// Faked query components
$queryComponents
=
array
(
'u'
=>
array
(
'table'
=>
$this
->
_em
->
getClassMetadata
(
'CmsUser'
),
'mapper'
=>
$this
->
_em
->
getEntityPersister
(
'CmsUser'
),
'parent'
=>
null
,
'relation'
=>
null
,
'map'
=>
null
)
);
// Faked table alias map
$tableAliasMap
=
array
(
'u'
=>
'u'
);
$stmt
=
new
Doctrine_HydratorMockStatement
(
$resultSet
);
$hydrator
=
new
Doctrine_HydratorNew
(
$this
->
_em
);
if
(
$name
==
'result1'
)
{
$result
=
$hydrator
->
hydrateResultSet
(
$this
->
_createParserResult
(
$stmt
,
$queryComponents
,
$tableAliasMap
,
Doctrine
::
HYDRATE_SINGLE_SCALAR
));
$this
->
assertEquals
(
'romanb'
,
$result
);
}
else
if
(
$name
==
'result2'
)
{
$result
=
$hydrator
->
hydrateResultSet
(
$this
->
_createParserResult
(
$stmt
,
$queryComponents
,
$tableAliasMap
,
Doctrine
::
HYDRATE_SINGLE_SCALAR
));
$this
->
assertEquals
(
1
,
$result
);
}
else
if
(
$name
==
'result3'
||
$name
==
'result4'
)
{
try
{
$result
=
$hydrator
->
hydrateResultSet
(
$this
->
_createParserResult
(
$stmt
,
$queryComponents
,
$tableAliasMap
,
Doctrine
::
HYDRATE_SINGLE_SCALAR
));
$this
->
fail
();
}
catch
(
Doctrine_Hydrator_Exception
$ex
)
{}
}
}
}
}
tests/lib/mocks/Doctrine_HydratorMockStatement.php
View file @
74ce82bd
...
@@ -33,6 +33,14 @@ class Doctrine_HydratorMockStatement
...
@@ -33,6 +33,14 @@ class Doctrine_HydratorMockStatement
return
$this
->
_resultSet
;
return
$this
->
_resultSet
;
}
}
public
function
fetchColumn
(
$columnNumber
=
0
)
{
$row
=
array_shift
(
$this
->
_resultSet
);
if
(
!
is_array
(
$row
))
return
false
;
$val
=
array_shift
(
$row
);
return
$val
!==
null
?
$val
:
false
;
}
/**
/**
* Fetches the next row in the result set.
* Fetches the next row in the result set.
*
*
...
...
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