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
c500f710
Commit
c500f710
authored
May 10, 2007
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
--no commit message
--no commit message
parent
f99e3dd4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
115 additions
and
23 deletions
+115
-23
Hydrate.php
draft/new-core/Hydrate.php
+74
-7
NewCoreTestCase.php
tests/NewCoreTestCase.php
+41
-16
No files found.
draft/new-core/Hydrate.php
View file @
c500f710
...
...
@@ -145,6 +145,10 @@ class Doctrine_Hydrate2 extends Doctrine_Access
{
return
$this
->
tableAliases
;
}
public
function
setTableAliases
(
array
$aliases
)
{
$this
->
tableAliases
=
$aliases
;
}
/**
* copyAliases
*
...
...
@@ -299,7 +303,6 @@ class Doctrine_Hydrate2 extends Doctrine_Access
}
$stmt
=
$this
->
conn
->
execute
(
$query
,
$params
);
return
$this
->
parseData
(
$stmt
);
}
...
...
@@ -311,6 +314,31 @@ class Doctrine_Hydrate2 extends Doctrine_Access
{
return
$this
->
_aliasMap
;
}
/**
* mapAggregateValues
* map the aggregate values of given dataset row to a given record
*
* @param Doctrine_Record $record
* @param array $row
*/
public
function
mapAggregateValues
(
$record
,
array
$row
)
{
// aggregate values have numeric keys
if
(
isset
(
$row
[
0
]))
{
// map each aggregate value
foreach
(
$row
as
$index
=>
$value
)
{
$agg
=
false
;
if
(
isset
(
$this
->
pendingAggregates
[
$alias
][
$index
]))
{
$agg
=
$this
->
pendingAggregates
[
$alias
][
$index
][
3
];
}
elseif
(
isset
(
$this
->
subqueryAggregates
[
$alias
][
$index
]))
{
$agg
=
$this
->
subqueryAggregates
[
$alias
][
$index
];
}
$record
->
mapValue
(
$agg
,
$value
);
}
}
return
$record
;
}
/**
* execute
* executes the dql query and populates all collections
...
...
@@ -320,12 +348,17 @@ class Doctrine_Hydrate2 extends Doctrine_Access
*/
public
function
execute
(
$params
=
array
(),
$return
=
Doctrine
::
FETCH_RECORD
)
{
$array
=
$this
->
_fetch
(
$params
=
array
(),
$return
=
Doctrine
::
FETCH_RECORD
);
$array
=
(
array
)
$this
->
_fetch
(
$params
=
array
(),
$return
=
Doctrine
::
FETCH_RECORD
);
$coll
=
new
Doctrine_Collection
(
key
(
$this
->
_aliasMap
));
$prev
[
$root
]
=
$coll
;
if
(
empty
(
$this
->
_aliasMap
))
{
throw
new
Doctrine_Hydrate_Exception
(
"Couldn't execute query. Component alias map was empty."
);
}
$previd
=
array
();
$rootMap
=
current
(
$this
->
_aliasMap
);
$coll
=
new
Doctrine_Collection
(
$rootMap
[
'table'
]);
$prev
[
key
(
$this
->
_aliasMap
)]
=
$coll
;
$prevRow
=
array
();
/**
* iterate over the fetched data
* here $data is a two dimensional array
...
...
@@ -334,13 +367,47 @@ class Doctrine_Hydrate2 extends Doctrine_Access
/**
* remove duplicated data rows and map data into objects
*/
foreach
(
$data
as
$
key
=>
$row
)
{
foreach
(
$data
as
$
tableAlias
=>
$row
)
{
if
(
empty
(
$row
))
{
continue
;
}
if
(
!
isset
(
$this
->
tableAliases
[
$tableAlias
]))
{
throw
new
Doctrine_Hydrate_Exception
(
'Unknown table alias '
.
$tableAlias
);
}
$alias
=
$this
->
tableAliases
[
$tableAlias
];
$map
=
$this
->
_aliasMap
[
$alias
];
if
(
$prevRow
[
$tableAlias
]
!==
$row
)
{
// set internal data
$this
->
tables
[
$name
]
->
setData
(
$row
);
// initialize a new record
$record
=
$this
->
tables
[
$name
]
->
getRecord
();
$this
->
mapAggregateValues
(
$record
,
$row
);
if
(
$name
==
$root
)
{
// add record into root collection
$coll
->
add
(
$record
);
unset
(
$previd
);
}
else
{
$prev
=
$this
->
addRelated
(
$prev
,
$name
,
$record
);
}
// following statement is needed to ensure that mappings
// are being done properly when the result set doesn't
// contain the rows in 'right order'
if
(
$prev
[
$name
]
!==
$record
)
{
$prev
[
$name
]
=
$record
;
}
}
$prevRow
[
$tableAlias
]
=
$row
;
}
}
}
/**
...
...
tests/NewCoreTestCase.php
View file @
c500f710
...
...
@@ -38,27 +38,52 @@ require_once('../draft/new-core/Collection.php');
*/
class
Doctrine_NewCore_TestCase
extends
Doctrine_UnitTestCase
class
Doctrine_NewCore_TestCase
extends
Doctrine_UnitTestCase
{
protected
$testData1
=
array
(
array
(
'e'
=>
array
(
'id'
=>
1
,
'name'
=>
'zYne'
),
'p'
=>
array
(
'id'
=>
1
,
'phonenumber'
=>
'123 123'
,
'user_id'
=>
1
)
),
array
(
'e'
=>
array
(
'id'
=>
2
,
'name'
=>
'John'
),
'p'
=>
array
(
'id'
=>
2
,
'phonenumber'
=>
'222 222'
,
'user_id'
=>
2
)
),
array
(
'e'
=>
array
(
'id'
=>
3
,
'name'
=>
'Arnold'
),
'p'
=>
array
(
'id'
=>
3
,
'phonenumber'
=>
'333 333'
,
'user_id'
=>
3
)
)
);
public
function
testExecuteForEmptyAliasMapThrowsException
()
{
$h
=
new
Doctrine_Hydrate_Mock
();
try
{
$h
->
execute
();
$this
->
fail
(
'Should throw exception'
);
}
catch
(
Doctrine_Hydrate_Exception
$e
)
{
$this
->
pass
();
}
}
public
function
testExecuteForEmptyTableAliasMapThrowsException
()
{
$h
=
new
Doctrine_Hydrate_Mock
();
$h
->
setData
(
$this
->
testData1
);
$h
->
setAliasMap
(
array
(
'u'
=>
array
(
'table'
=>
$this
->
conn
->
getTable
(
'User'
))));
try
{
$h
->
execute
();
$this
->
fail
(
'Should throw exception'
);
}
catch
(
Doctrine_Hydrate_Exception
$e
)
{
$this
->
pass
();
}
}
public
function
testHydrate
()
{
$h
=
new
Doctrine_Hydrate_Mock
();
$h
->
setData
(
array
(
array
(
'e'
=>
array
(
'id'
=>
1
,
'name'
=>
'zYne'
),
'p'
=>
array
(
'id'
=>
1
,
'phonenumber'
=>
'123 123'
,
'user_id'
=>
1
)
),
array
(
'e'
=>
array
(
'id'
=>
2
,
'name'
=>
'John'
),
'p'
=>
array
(
'id'
=>
2
,
'phonenumber'
=>
'222 222'
,
'user_id'
=>
2
)
),
array
(
'e'
=>
array
(
'id'
=>
3
,
'name'
=>
'Arnold'
),
'p'
=>
array
(
'id'
=>
3
,
'phonenumber'
=>
'333 333'
,
'user_id'
=>
3
)
)
)
);
$h
->
setData
(
$this
->
testData1
);
$h
->
setAliasMap
(
array
(
'u'
=>
array
(
'table'
=>
$this
->
conn
->
getTable
(
'User'
))));
$h
->
setTableAliases
(
array
(
'e'
=>
'u'
));
}
}
class
Doctrine_Hydrate_Mock
extends
Doctrine_Hydrate2
...
...
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