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
b98130db
Commit
b98130db
authored
Sep 22, 2007
by
Jonathan.Wage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes for doctrine resource.
parent
5aa73029
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
121 additions
and
93 deletions
+121
-93
Collection.php
lib/Doctrine/Collection.php
+5
-2
Xml.php
lib/Doctrine/Parser/Xml.php
+1
-1
Record.php
lib/Doctrine/Record.php
+9
-7
Resource.php
lib/Doctrine/Resource.php
+24
-1
Client.php
lib/Doctrine/Resource/Client.php
+5
-0
Collection.php
lib/Doctrine/Resource/Collection.php
+15
-1
Query.php
lib/Doctrine/Resource/Query.php
+2
-25
Record.php
lib/Doctrine/Resource/Record.php
+14
-12
Server.php
lib/Doctrine/Resource/Server.php
+26
-43
index.php
playground/index.php
+20
-1
No files found.
lib/Doctrine/Collection.php
View file @
b98130db
...
...
@@ -589,12 +589,15 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
*
* @param boolean $deep
*/
public
function
toArray
(
$deep
=
false
)
public
function
toArray
(
$deep
=
false
,
$prefixKey
=
false
)
{
if
(
$deep
)
{
$data
=
array
();
foreach
(
$this
->
data
as
$key
=>
$record
)
{
$data
[
$key
]
=
$record
->
toArray
(
$deep
);
$key
=
$prefixKey
?
get_class
(
$record
)
.
'_'
.
$key
:
$key
;
$data
[
$key
]
=
$record
->
toArray
(
$deep
,
$prefixKey
);
}
return
$data
;
}
else
{
...
...
lib/Doctrine/Parser/Xml.php
View file @
b98130db
...
...
@@ -108,7 +108,7 @@ class Doctrine_Parser_Xml extends Doctrine_Parser
if
(
is_array
(
$return
))
{
return
$return
;
}
else
{
return
$false
;
return
array
()
;
}
}
}
\ No newline at end of file
lib/Doctrine/Record.php
View file @
b98130db
...
...
@@ -1118,7 +1118,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
* @param boolean $deep - Return also the relations
* @return array
*/
public
function
toArray
(
$deep
=
false
)
public
function
toArray
(
$deep
=
false
,
$prefixKey
=
false
)
{
$a
=
array
();
...
...
@@ -1134,18 +1134,20 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
}
if
(
$deep
)
{
foreach
(
$this
->
_references
as
$key
=>
$relation
)
{
$a
[
$key
]
=
$relation
->
toArray
(
$deep
);
$a
[
$key
]
=
$relation
->
toArray
(
$deep
,
$prefixKey
);
}
}
return
array_merge
(
$a
,
$this
->
_values
);
}
public
function
fromArray
(
$array
)
{
foreach
(
$array
as
$key
=>
$value
)
{
if
(
!
$this
->
getTable
()
->
hasRelation
(
$key
)
&&
$this
->
getTable
()
->
hasColumn
(
$key
))
{
$this
->
$key
=
$value
;
}
else
{
$this
->
$key
->
fromArray
(
$value
);
if
(
is_array
(
$array
))
{
foreach
(
$array
as
$key
=>
$value
)
{
if
(
is_array
(
$value
))
{
$this
->
$key
->
fromArray
(
$value
);
}
else
{
$this
->
$key
=
$value
;
}
}
}
}
...
...
lib/Doctrine/Resource.php
View file @
b98130db
...
...
@@ -6,6 +6,29 @@ class Doctrine_Resource
$url
.=
strstr
(
$url
,
'?'
)
?
'&'
:
'?'
;
$url
.=
http_build_query
(
$request
);
return
file_get_contents
(
$url
);
$response
=
file_get_contents
(
$url
);
return
$response
;
}
public
function
hydrate
(
array
$array
,
$model
,
$config
,
$passedKey
=
null
)
{
$collection
=
new
Doctrine_Resource_Collection
(
$model
,
$config
);
foreach
(
$array
as
$record
)
{
$r
=
new
Doctrine_Resource_Record
(
$model
,
$config
);
foreach
(
$record
as
$key
=>
$value
)
{
if
(
is_array
(
$value
))
{
$r
->
data
[
$key
]
=
$this
->
hydrate
(
$value
,
$model
,
$config
,
$key
);
}
else
{
$r
->
data
[
$key
]
=
$value
;
}
}
$collection
->
data
[]
=
$r
;
}
return
$collection
;
}
}
\ No newline at end of file
lib/Doctrine/Resource/Client.php
View file @
b98130db
...
...
@@ -17,4 +17,9 @@ class Doctrine_Resource_Client extends Doctrine_Resource
{
return
new
Doctrine_Resource_Record
(
$model
,
$this
->
config
);
}
public
function
newCollection
(
$model
)
{
return
new
Doctrine_Resource_Collection
(
$model
,
$this
->
config
);
}
}
\ No newline at end of file
lib/Doctrine/Resource/Collection.php
View file @
b98130db
...
...
@@ -5,9 +5,10 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
public
$config
=
array
();
public
$model
=
null
;
public
function
__construct
(
$model
)
public
function
__construct
(
$model
,
$config
)
{
$this
->
model
=
$model
;
$this
->
config
=
$config
;
}
public
function
count
()
...
...
@@ -15,6 +16,18 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
return
count
(
$data
);
}
public
function
get
(
$get
)
{
if
(
isset
(
$this
->
data
[
$get
]))
{
return
$this
->
data
[
$get
];
}
}
public
function
set
(
$set
,
$value
)
{
$this
->
data
[
$set
]
=
$value
;
}
public
function
getIterator
()
{
$data
=
$this
->
data
;
...
...
@@ -30,6 +43,7 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
public
function
toArray
()
{
$array
=
array
();
foreach
(
$this
->
data
as
$key
=>
$record
)
{
$array
[
$key
]
=
$record
->
toArray
();
}
...
...
lib/Doctrine/Resource/Query.php
View file @
b98130db
...
...
@@ -54,8 +54,8 @@ class Doctrine_Resource_Query extends Doctrine_Resource
{
$array
=
Doctrine_Parser
::
load
(
$response
,
$this
->
getFormat
());
$hydrated
=
$this
->
hydrate
(
$array
);
$hydrated
=
$this
->
hydrate
(
$array
,
$this
->
getModel
(),
$this
->
config
);
return
$hydrated
;
}
...
...
@@ -74,29 +74,6 @@ class Doctrine_Resource_Query extends Doctrine_Resource
return
$url
;
}
public
function
hydrate
(
array
$array
,
$passedKey
=
null
)
{
$model
=
$passedKey
?
$passedKey
:
$this
->
getModel
();
$collection
=
new
Doctrine_Resource_Collection
(
$model
,
$this
->
config
);
foreach
(
$array
as
$record
)
{
$r
=
new
Doctrine_Resource_Record
(
$model
,
$this
->
config
);
foreach
(
$record
as
$key
=>
$value
)
{
if
(
is_array
(
$value
))
{
$r
->
data
[
$key
]
=
$this
->
hydrate
(
$value
,
$key
);
}
else
{
$r
->
data
[
$key
]
=
$value
;
}
}
$collection
->
data
[]
=
$r
;
}
return
$collection
;
}
public
function
getModel
()
{
$dql
=
$this
->
getDql
();
...
...
lib/Doctrine/Resource/Record.php
View file @
b98130db
...
...
@@ -4,7 +4,6 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
public
$data
=
array
();
public
$config
=
array
();
public
$model
=
null
;
public
$changes
=
array
();
public
function
__construct
(
$model
,
$config
)
{
...
...
@@ -24,8 +23,6 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
public
function
set
(
$set
,
$value
)
{
$this
->
data
[
$set
]
=
$value
;
$this
->
changes
[
$set
]
=
$value
;
}
public
function
count
()
...
...
@@ -40,22 +37,27 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
return
new
ArrayIterator
(
$data
);
}
public
function
save
(
)
public
function
newRequest
(
$type
)
{
$request
=
array
();
$request
[
'format'
]
=
$this
->
config
[
'format'
]
;
$request
[
'type'
]
=
'save'
;
$request
[
'format'
]
=
isset
(
$this
->
config
[
'format'
])
?
$this
->
config
[
'format'
]
:
'xml'
;
$request
[
'type'
]
=
$type
;
$request
[
'model'
]
=
$this
->
model
;
$request
[
'data'
]
=
$this
->
data
;
$request
[
'changes'
]
=
$this
->
changes
;
return
$request
;
}
public
function
save
()
{
$request
=
$this
->
newRequest
(
'save'
);
$request
[
'data'
]
=
$this
->
toArray
();
$response
=
Doctrine_Resource
::
request
(
$this
->
config
[
'url'
],
$request
);
$array
=
Doctrine_Parser
::
load
(
$response
,
$request
[
'format'
]);
$this
->
data
=
array_merge
(
$this
->
data
,
$array
);
return
$array
;
$resource
=
new
Doctrine_Resource
();
$this
->
data
=
$resource
->
hydrate
(
array
(
$array
),
$this
->
model
,
$this
->
config
)
->
getFirst
()
->
data
;
}
public
function
toArray
()
...
...
@@ -63,7 +65,7 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
$array
=
array
();
foreach
(
$this
->
data
as
$key
=>
$value
)
{
if
(
$value
instanceof
Doctrine_Resource_Collection
)
{
if
(
$value
instanceof
Doctrine_Resource_Collection
OR
$value
instanceof
Doctrine_Resource_Record
)
{
$array
[
$key
]
=
$value
->
toArray
();
}
else
{
$array
[
$key
]
=
$value
;
...
...
lib/Doctrine/Resource/Server.php
View file @
b98130db
...
...
@@ -2,12 +2,35 @@
class
Doctrine_Resource_Server
extends
Doctrine_Resource
{
public
$config
=
array
();
public
$format
=
'xml'
;
public
function
__construct
(
$config
)
{
$this
->
config
=
array_merge
(
$config
,
$this
->
config
);
}
public
function
executeSave
(
$request
)
{
$model
=
$request
[
'model'
];
$data
=
$request
[
'data'
];
$record
=
new
$model
();
$record
->
fromArray
(
$data
);
$record
->
save
();
return
$record
->
toArray
(
true
,
true
);
}
public
function
executeQuery
(
$request
)
{
$dql
=
$request
[
'dql'
];
$params
=
isset
(
$request
[
'params'
])
?
$request
[
'params'
]
:
array
();
$conn
=
Doctrine_Manager
::
connection
();
return
$conn
->
query
(
$dql
,
$params
)
->
toArray
(
true
,
true
);
}
public
function
execute
(
$request
)
{
if
(
!
isset
(
$request
[
'type'
]))
{
...
...
@@ -15,50 +38,10 @@ class Doctrine_Resource_Server extends Doctrine_Resource
}
$format
=
isset
(
$request
[
'format'
])
?
$request
[
'format'
]
:
'xml'
;
$type
=
$request
[
'type'
];
$funcName
=
'execute'
.
Doctrine
::
classify
(
$type
);
if
(
$request
[
'type'
]
==
'query'
)
{
if
(
isset
(
$request
[
'dql'
])
&&
$request
[
'dql'
])
{
$dql
=
$request
[
'dql'
];
$params
=
isset
(
$request
[
'params'
])
?
$request
[
'params'
]
:
array
();
$conn
=
Doctrine_Manager
::
connection
();
$result
=
$conn
->
query
(
$dql
,
$params
,
Doctrine
::
FETCH_ARRAY
);
}
else
{
throw
new
Doctrine_Resource_Exception
(
'You must specify a dql query'
);
}
}
else
if
(
$request
[
'type'
]
==
'save'
)
{
$model
=
$request
[
'model'
];
$table
=
Doctrine_Manager
::
getInstance
()
->
getTable
(
$model
);
$pks
=
(
array
)
$table
->
getIdentifier
();
$pks
=
array_flip
(
$pks
);
$hasPk
=
false
;
foreach
(
array_keys
(
$pks
)
as
$key
)
{
if
(
isset
(
$request
[
'data'
][
$key
])
&&
$request
[
'data'
][
$key
])
{
$pks
[
$key
]
=
$request
[
'data'
][
$key
];
$hasPk
=
true
;
}
}
if
(
$hasPk
)
{
$record
=
$table
->
find
(
$pks
);
}
else
{
$record
=
new
$model
();
}
if
(
isset
(
$request
[
'changes'
])
&&
!
empty
(
$request
[
'changes'
]))
{
$changes
=
$request
[
'changes'
];
foreach
(
$changes
as
$key
=>
$value
)
{
$record
->
$key
=
$value
;
}
}
$record
->
save
();
$result
=
$record
->
toArray
();
}
$result
=
$this
->
$funcName
(
$request
);
return
Doctrine_Parser
::
dump
(
$result
,
$format
);
}
...
...
playground/index.php
View file @
b98130db
...
...
@@ -2,4 +2,23 @@
require_once
(
'playground.php'
);
require_once
(
'connection.php'
);
require_once
(
'models.php'
);
require_once
(
'data.php'
);
\ No newline at end of file
require_once
(
'data.php'
);
$action
=
isset
(
$_REQUEST
[
'action'
])
?
$_REQUEST
[
'action'
]
:
'client'
;
if
(
$action
==
'server'
)
{
$config
=
array
();
$server
=
new
Doctrine_Resource_Server
(
$config
);
$server
->
run
(
$_REQUEST
);
}
else
{
$config
=
array
(
'url'
=>
'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server'
);
$client
=
new
Doctrine_Resource_Client
(
$config
);
$record
=
$client
->
newRecord
(
'User'
);
$record
->
name
=
'jon wage'
;
$record
->
loginname
=
'test'
;
$record
->
save
();
print_r
(
$record
->
toArray
());
}
\ 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