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
3438aef5
Commit
3438aef5
authored
Sep 24, 2007
by
Jonathan.Wage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring of Resource module.
parent
c7ea8f37
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
274 additions
and
157 deletions
+274
-157
Schema.php
lib/Doctrine/Export/Schema.php
+7
-3
Schema.php
lib/Doctrine/Import/Schema.php
+13
-6
Resource.php
lib/Doctrine/Resource.php
+26
-22
Client.php
lib/Doctrine/Resource/Client.php
+41
-8
Collection.php
lib/Doctrine/Resource/Collection.php
+24
-16
Query.php
lib/Doctrine/Resource/Query.php
+30
-49
Record.php
lib/Doctrine/Resource/Record.php
+79
-31
Server.php
lib/Doctrine/Resource/Server.php
+44
-14
index.php
playground/index.php
+10
-8
No files found.
lib/Doctrine/Export/Schema.php
View file @
3438aef5
...
...
@@ -46,11 +46,15 @@ class Doctrine_Export_Schema
* @param string $directory
* @return void
*/
public
function
buildSchema
(
$directory
,
$models
=
array
())
public
function
buildSchema
(
$directory
=
null
,
$models
=
array
())
{
$array
=
array
();
$loadedModels
=
Doctrine
::
loadModels
(
$directory
);
if
(
$directory
)
{
$loadedModels
=
Doctrine
::
loadModels
(
$directory
);
}
else
{
$loadedModels
=
Doctrine
::
getLoadedModels
();
}
$parent
=
new
ReflectionClass
(
'Doctrine_Record'
);
...
...
@@ -140,7 +144,7 @@ class Doctrine_Export_Schema
* @param string $directory
* @return void
*/
public
function
exportSchema
(
$schema
,
$format
,
$directory
,
$models
=
array
())
public
function
exportSchema
(
$schema
,
$format
,
$directory
=
null
,
$models
=
array
())
{
$array
=
$this
->
buildSchema
(
$directory
,
$models
);
...
...
lib/Doctrine/Import/Schema.php
View file @
3438aef5
...
...
@@ -41,7 +41,17 @@ class Doctrine_Import_Schema
{
public
$relations
=
array
();
public
function
buildSchema
(
$schema
,
$format
)
{
$array
=
array
();
foreach
((
array
)
$schema
AS
$s
)
{
$array
=
array_merge
(
$array
,
$this
->
parseSchema
(
$s
,
$format
));
}
$this
->
buildRelationships
(
$array
);
return
array
(
'schema'
=>
$array
,
'relations'
=>
$this
->
relations
);
}
/**
* importSchema
*
...
...
@@ -58,12 +68,9 @@ class Doctrine_Import_Schema
$builder
=
new
Doctrine_Import_Builder
();
$builder
->
setTargetPath
(
$directory
);
$array
=
array
();
foreach
((
array
)
$schema
AS
$s
)
{
$array
=
array_merge
(
$array
,
$this
->
parseSchema
(
$s
,
$format
));
}
$schema
=
$this
->
buildSchema
(
$schema
,
$format
);
$
this
->
buildRelationships
(
$array
)
;
$
array
=
$schema
[
'schema'
]
;
foreach
(
$array
as
$name
=>
$properties
)
{
if
(
!
empty
(
$models
)
&&
!
in_array
(
$properties
[
'className'
],
$models
))
{
...
...
lib/Doctrine/Resource.php
View file @
3438aef5
...
...
@@ -23,6 +23,7 @@
* Doctrine_Resource
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
...
...
@@ -32,34 +33,37 @@
*/
class
Doctrine_Resource
{
public
static
function
request
(
$url
,
$request
)
{
$url
.=
strstr
(
$url
,
'?'
)
?
'&'
:
'?'
;
$url
.=
http_build_query
(
$request
);
$response
=
file_get_contents
(
$url
);
return
$response
;
}
protected
$_config
=
null
;
protected
$_defaultFormat
=
'xml'
;
public
function
hydrate
(
array
$array
,
$model
,
$config
,
$passedKey
=
null
)
public
function
__construct
(
$config
)
{
$collection
=
new
Doctrine_Resource_Collection
(
$model
,
$config
);
foreach
(
$config
as
$key
=>
$value
)
{
$this
->
getConfig
()
->
set
(
$key
,
$value
);
}
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
;
}
$loadDoctrine
=
false
;
foreach
(
$this
->
getConfig
()
->
getAll
()
as
$key
=>
$value
)
{
if
(
$key
==
'url'
)
{
$this
->
loadDoctrine
=
true
;
}
}
$collection
->
data
[]
=
$r
;
if
(
!
$this
->
getConfig
()
->
has
(
'format'
)
OR
!
$this
->
getConfig
()
->
get
(
'format'
))
{
$this
->
getConfig
()
->
set
(
'format'
,
$this
->
_defaultFormat
);
}
}
public
function
getConfig
(
$key
=
null
)
{
if
(
$this
->
_config
===
null
)
{
$this
->
_config
=
new
Doctrine_Resource_Config
();
}
return
$collection
;
if
(
$key
===
null
)
{
return
$this
->
_config
;
}
else
{
return
$this
->
_config
->
get
(
$key
);
}
}
}
lib/Doctrine/Resource/Client.php
View file @
3438aef5
...
...
@@ -23,6 +23,7 @@
* Doctrine_Resource_Client
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
...
...
@@ -32,25 +33,57 @@
*/
class
Doctrine_Resource_Client
extends
Doctrine_Resource
{
public
$
config
=
array
()
;
public
$
loadDoctrine
=
false
;
public
function
__construct
(
$config
)
static
public
function
getInstance
(
$config
=
null
)
{
$this
->
config
=
$config
;
static
$instance
;
if
(
!
$instance
)
{
$instance
=
new
Doctrine_Resource_Client
(
$config
);
if
(
$instance
->
loadDoctrine
===
true
)
{
$instance
->
loadDoctrine
();
}
}
return
$instance
;
}
public
function
loadDoctrine
()
{
$path
=
'/tmp/'
.
md5
(
serialize
(
$this
->
getConfig
()));
if
(
file_exists
(
$path
))
{
$schema
=
file_get_contents
(
$path
);
}
else
{
$request
=
new
Doctrine_Resource_Request
();
$request
->
set
(
'type'
,
'load'
);
$request
->
set
(
'format'
,
'xml'
);
$schema
=
$request
->
execute
();
file_put_contents
(
$path
,
$schema
);
}
$import
=
new
Doctrine_Import_Schema
();
$schema
=
$import
->
buildSchema
(
$path
,
'xml'
);
$this
->
getConfig
()
->
set
(
'schema'
,
$schema
);
}
public
function
newQuery
()
{
return
new
Doctrine_Resource_Query
(
$this
->
config
);
return
new
Doctrine_Resource_Query
();
}
public
function
newRecord
(
$model
)
public
function
newRecord
(
$model
,
$loadRelations
=
true
)
{
return
new
Doctrine_Resource_Record
(
$model
,
$
this
->
config
);
return
new
Doctrine_Resource_Record
(
$model
,
$
loadRelations
);
}
public
function
newCollection
(
$model
)
{
return
new
Doctrine_Resource_Collection
(
$model
,
$this
->
config
);
return
new
Doctrine_Resource_Collection
(
$model
);
}
}
}
\ No newline at end of file
lib/Doctrine/Resource/Collection.php
View file @
3438aef5
...
...
@@ -23,6 +23,7 @@
* Doctrine_Resource_Collection
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
...
...
@@ -32,50 +33,57 @@
*/
class
Doctrine_Resource_Collection
extends
Doctrine_Access
implements
Countable
,
IteratorAggregate
{
p
ublic
$
data
=
array
();
p
ublic
$
config
=
array
();
p
ublic
$
model
=
null
;
p
rotected
$_
data
=
array
();
p
rotected
$_
config
=
array
();
p
rotected
$_
model
=
null
;
public
function
__construct
(
$model
,
$config
)
public
function
__construct
(
$model
)
{
$this
->
model
=
$model
;
$this
->
config
=
$config
;
$this
->
_model
=
$model
;
}
public
function
getConfig
(
$key
=
null
)
{
return
Doctrine_Resource_Client
::
getInstance
()
->
getConfig
(
$key
);
}
public
function
count
()
{
return
count
(
$data
);
return
count
(
$
this
->
_
data
);
}
public
function
get
(
$get
)
{
if
(
isset
(
$this
->
data
[
$get
]))
{
return
$this
->
data
[
$get
];
if
(
isset
(
$this
->
_
data
[
$get
]))
{
return
$this
->
_
data
[
$get
];
}
}
public
function
set
(
$set
,
$value
)
{
$this
->
data
[
$set
]
=
$value
;
$this
->
_data
[
$set
]
=
$value
;
}
public
function
add
(
$value
)
{
$this
->
_data
[]
=
$value
;
}
public
function
getIterator
()
{
$data
=
$this
->
data
;
return
new
ArrayIterator
(
$data
);
return
new
ArrayIterator
(
$this
->
_data
);
}
public
function
getFirst
()
{
return
isset
(
$this
->
data
[
0
])
?
$this
->
data
[
0
]
:
null
;
return
isset
(
$this
->
_data
[
0
])
?
$this
->
_
data
[
0
]
:
null
;
}
public
function
toArray
()
{
$array
=
array
();
foreach
(
$this
->
data
as
$key
=>
$record
)
{
foreach
(
$this
->
_
data
as
$key
=>
$record
)
{
$array
[
$key
]
=
$record
->
toArray
();
}
...
...
@@ -84,7 +92,7 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
public
function
save
()
{
foreach
(
$this
->
data
as
$record
)
{
foreach
(
$this
as
$record
)
{
$record
->
save
();
}
}
...
...
lib/Doctrine/Resource/Query.php
View file @
3438aef5
...
...
@@ -23,6 +23,7 @@
* Doctrine_Resource_Query
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
...
...
@@ -30,66 +31,58 @@
* @link www.phpdoctrine.com
* @since 1.0
*/
class
Doctrine_Resource_Query
extends
Doctrine_Resource
class
Doctrine_Resource_Query
{
public
$config
=
array
();
public
$parts
=
array
();
public
$dql
=
null
;
public
$defaultFormat
=
'xml'
;
protected
$_parts
=
array
();
protected
$_dql
=
null
;
protected
$_params
=
array
();
public
function
__construct
(
$config
)
public
function
getConfig
(
$key
=
null
)
{
$this
->
config
=
$config
;
return
Doctrine_Resource_Client
::
getInstance
()
->
getConfig
(
$key
)
;
}
public
function
query
(
$dql
,
$params
=
array
())
{
$this
->
dql
=
$dql
;
$this
->
_
dql
=
$dql
;
return
$this
->
execute
(
$params
);
}
public
function
execute
(
$params
=
array
())
{
$request
=
array
();
$request
[
'dql'
]
=
$this
->
getDql
(
);
$request
[
'params'
]
=
$params
;
$request
[
'format'
]
=
$this
->
getFormat
(
);
$request
[
'type'
]
=
'query'
;
$request
=
new
Doctrine_Resource_Request
();
$request
->
set
(
'dql'
,
$this
->
getDql
()
);
$request
->
set
(
'params'
,
$params
)
;
$request
->
set
(
'format'
,
$this
->
getConfig
()
->
get
(
'format'
)
);
$request
->
set
(
'type'
,
'query'
)
;
$response
=
self
::
request
(
$this
->
config
[
'url'
],
$request
);
$response
=
$request
->
execute
(
);
return
$this
->
parseResponse
(
$response
);
$array
=
Doctrine_Parser
::
load
(
$response
,
$this
->
getConfig
()
->
get
(
'format'
));
return
$request
->
hydrate
(
$array
,
$this
->
getModel
());
}
public
function
getDql
()
{
if
(
!
$this
->
dql
&&
!
empty
(
$this
->
parts
))
{
if
(
!
$this
->
_dql
&&
!
empty
(
$this
->
_
parts
))
{
$q
=
''
;
$q
.=
(
!
empty
(
$this
->
parts
[
'select'
]))
?
'SELECT '
.
implode
(
', '
,
$this
->
parts
[
'select'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
parts
[
'from'
]))
?
' FROM '
.
implode
(
' '
,
$this
->
parts
[
'from'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
parts
[
'where'
]))
?
' WHERE '
.
implode
(
' AND '
,
$this
->
parts
[
'where'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
parts
[
'groupby'
]))
?
' GROUP BY '
.
implode
(
', '
,
$this
->
parts
[
'groupby'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
parts
[
'having'
]))
?
' HAVING '
.
implode
(
' AND '
,
$this
->
parts
[
'having'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
parts
[
'orderby'
]))
?
' ORDER BY '
.
implode
(
', '
,
$this
->
parts
[
'orderby'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
parts
[
'limit'
]))
?
' LIMIT '
.
implode
(
' '
,
$this
->
parts
[
'limit'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
parts
[
'offset'
]))
?
' OFFSET '
.
implode
(
' '
,
$this
->
parts
[
'offset'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
_parts
[
'select'
]))
?
'SELECT '
.
implode
(
', '
,
$this
->
_
parts
[
'select'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
_parts
[
'from'
]))
?
' FROM '
.
implode
(
' '
,
$this
->
_
parts
[
'from'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
_parts
[
'where'
]))
?
' WHERE '
.
implode
(
' AND '
,
$this
->
_
parts
[
'where'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
_parts
[
'groupby'
]))
?
' GROUP BY '
.
implode
(
', '
,
$this
->
_
parts
[
'groupby'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
_parts
[
'having'
]))
?
' HAVING '
.
implode
(
' AND '
,
$this
->
_
parts
[
'having'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
_parts
[
'orderby'
]))
?
' ORDER BY '
.
implode
(
', '
,
$this
->
_
parts
[
'orderby'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
_parts
[
'limit'
]))
?
' LIMIT '
.
implode
(
' '
,
$this
->
_
parts
[
'limit'
])
:
''
;
$q
.=
(
!
empty
(
$this
->
_parts
[
'offset'
]))
?
' OFFSET '
.
implode
(
' '
,
$this
->
_
parts
[
'offset'
])
:
''
;
return
$q
;
}
else
{
return
$this
->
dql
;
return
$this
->
_
dql
;
}
}
public
function
parseResponse
(
$response
)
{
$array
=
Doctrine_Parser
::
load
(
$response
,
$this
->
getFormat
());
$hydrated
=
$this
->
hydrate
(
$array
,
$this
->
getModel
(),
$this
->
config
);
return
$hydrated
;
}
public
function
buildUrl
(
$array
)
{
$url
=
''
;
...
...
@@ -115,18 +108,6 @@ class Doctrine_Resource_Query extends Doctrine_Resource
return
$e
[
0
];
}
public
function
setFormat
(
$format
)
{
$this
->
config
[
'format'
]
=
$format
;
return
$this
;
}
public
function
getFormat
()
{
return
isset
(
$this
->
config
[
'format'
])
?
$this
->
config
[
'format'
]
:
$this
->
defaultFormat
;
}
/**
* addSelect
* adds fields to the SELECT part of the query
...
...
@@ -253,7 +234,7 @@ class Doctrine_Resource_Query extends Doctrine_Resource
*/
public
function
distinct
(
$flag
=
true
)
{
$this
->
parts
[
'distinct'
]
=
(
bool
)
$flag
;
$this
->
_
parts
[
'distinct'
]
=
(
bool
)
$flag
;
return
$this
;
}
...
...
@@ -267,7 +248,7 @@ class Doctrine_Resource_Query extends Doctrine_Resource
*/
public
function
forUpdate
(
$flag
=
true
)
{
$this
->
parts
[
self
::
FOR_UPDATE
]
=
(
bool
)
$flag
;
$this
->
_
parts
[
self
::
FOR_UPDATE
]
=
(
bool
)
$flag
;
return
$this
;
}
...
...
@@ -446,7 +427,7 @@ class Doctrine_Resource_Query extends Doctrine_Resource
*/
public
function
parseQueryPart
(
$queryPartName
,
$queryPart
)
{
$this
->
parts
[
$queryPartName
][]
=
$queryPart
;
$this
->
_
parts
[
$queryPartName
][]
=
$queryPart
;
return
$this
;
}
...
...
lib/Doctrine/Resource/Record.php
View file @
3438aef5
...
...
@@ -23,6 +23,7 @@
* Doctrine_Resource_Record
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
...
...
@@ -32,70 +33,117 @@
*/
class
Doctrine_Resource_Record
extends
Doctrine_Record_Abstract
implements
Countable
,
IteratorAggregate
{
p
ublic
$
data
=
array
();
p
ublic
$config
=
array
()
;
p
ublic
$model
=
null
;
p
rotected
$_
data
=
array
();
p
rotected
$_model
=
null
;
p
rotected
$_schema
=
null
;
public
function
__construct
(
$model
,
$
config
)
public
function
__construct
(
$model
,
$
loadRelations
=
true
)
{
$this
->
model
=
$model
;
$this
->
config
=
$config
;
$this
->
_model
=
$model
;
$schema
=
$this
->
getConfig
(
'schema'
);
if
(
isset
(
$schema
[
'schema'
][
$model
])
&&
$schema
[
'schema'
][
$model
])
{
$this
->
_schema
=
$schema
[
'schema'
][
$model
];
}
if
(
isset
(
$schema
[
'relations'
][
$model
])
&&
$schema
[
'relations'
][
$model
])
{
$this
->
_schema
[
'relations'
]
=
$schema
[
'relations'
][
$model
];
}
$this
->
initialize
(
$loadRelations
);
}
public
function
initialize
(
$loadRelations
=
true
)
{
if
(
!
$this
->
_schema
)
{
return
false
;
}
$schema
=
$this
->
_schema
;
$relations
=
$this
->
_schema
[
'relations'
];
if
(
isset
(
$schema
[
'columns'
]))
{
$columns
=
$schema
[
'columns'
];
foreach
(
$columns
as
$column
)
{
if
(
!
isset
(
$this
->
_data
[
$column
[
'name'
]])
||
$this
->
_data
[
$column
[
'name'
]])
{
$this
->
_data
[
$column
[
'name'
]]
=
null
;
}
}
}
if
(
isset
(
$schema
[
'relations'
])
&&
$loadRelations
)
{
$relations
=
$schema
[
'relations'
];
foreach
(
$relations
as
$relation
)
{
if
(
$relation
[
'type'
]
===
Doctrine_Relation
::
ONE
)
{
$this
->
_data
[
$relation
[
'alias'
]]
=
Doctrine_Resource_Client
::
getInstance
()
->
newRecord
(
$relation
[
'class'
],
false
);
}
else
{
$this
->
_data
[
$relation
[
'alias'
]]
=
Doctrine_Resource_Client
::
getInstance
()
->
newCollection
(
$relation
[
'class'
]);
}
}
}
}
public
function
getConfig
(
$key
=
null
)
{
return
Doctrine_Resource_Client
::
getInstance
()
->
getConfig
(
$key
);
}
public
function
get
(
$get
)
{
if
(
!
isset
(
$this
->
data
[
$get
]))
{
$this
->
data
[
$get
]
=
null
;
if
(
!
isset
(
$this
->
_data
[
$get
]))
{
$this
->
_data
[
$get
]
=
null
;
}
else
{
$this
->
_data
[
$get
]
=
Doctrine_Resource_Client
::
getInstance
()
->
newRecord
(
$get
,
false
);
}
return
$this
->
data
[
$get
];
return
$this
->
_
data
[
$get
];
}
public
function
set
(
$set
,
$value
)
{
$this
->
data
[
$set
]
=
$value
;
$this
->
_
data
[
$set
]
=
$value
;
}
public
function
count
()
{
return
count
(
$this
->
data
);
return
count
(
$this
->
_
data
);
}
public
function
getIterator
()
{
$data
=
$this
->
data
;
return
new
ArrayIterator
(
$data
);
}
public
function
newRequest
(
$type
)
{
$request
=
array
();
$request
[
'format'
]
=
isset
(
$this
->
config
[
'format'
])
?
$this
->
config
[
'format'
]
:
'xml'
;
$request
[
'type'
]
=
$type
;
$request
[
'model'
]
=
$this
->
model
;
return
$request
;
return
new
ArrayIterator
(
$this
->
_data
);
}
public
function
save
()
{
$request
=
$this
->
newRequest
(
'save'
);
$request
[
'data'
]
=
$this
->
toArray
();
$format
=
$this
->
getConfig
(
'format'
)
?
$this
->
getConfig
(
'format'
)
:
'xml'
;
$request
=
new
Doctrine_Resource_Request
();
$request
->
set
(
'format'
,
$format
);
$request
->
set
(
'type'
,
'save'
);
$request
->
set
(
'model'
,
$this
->
getModel
());
$request
->
set
(
'data'
,
$this
->
toArray
());
$response
=
Doctrine_Resource
::
request
(
$this
->
config
[
'url'
],
$request
);
$response
=
$request
->
execute
(
);
$array
=
Doctrine_Parser
::
load
(
$response
,
$
request
[
'format'
]
);
$array
=
Doctrine_Parser
::
load
(
$response
,
$
format
);
$resource
=
new
Doctrine_Resource
();
$this
->
data
=
$resource
->
hydrate
(
array
(
$array
),
$this
->
model
,
$this
->
config
)
->
getFirst
()
->
data
;
$this
->
_data
=
$request
->
hydrate
(
array
(
$array
),
$this
->
_model
)
->
getFirst
()
->
_data
;
}
public
function
getModel
()
{
return
$this
->
_model
;
}
public
function
toArray
()
{
$array
=
array
();
foreach
(
$this
->
data
as
$key
=>
$value
)
{
foreach
(
$this
->
_
data
as
$key
=>
$value
)
{
if
(
$value
instanceof
Doctrine_Resource_Collection
OR
$value
instanceof
Doctrine_Resource_Record
)
{
$array
[
$key
]
=
$value
->
toArray
();
}
else
{
...
...
lib/Doctrine/Resource/Server.php
View file @
3438aef5
...
...
@@ -23,6 +23,7 @@
* Doctrine_Resource_Server
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
...
...
@@ -30,20 +31,23 @@
* @link www.phpdoctrine.com
* @since 1.0
*/
class
Doctrine_Resource_Server
extends
Doctrine_
R
esource
class
Doctrine_Resource_Server
extends
Doctrine_
r
esource
{
public
$config
=
array
();
public
$format
=
'xml'
;
public
function
__construct
(
$config
)
static
public
function
getInstance
(
$config
=
null
)
{
$this
->
config
=
array_merge
(
$config
,
$this
->
config
);
static
$instance
;
if
(
!
$instance
)
{
$instance
=
new
Doctrine_Resource_Server
(
$config
);
}
return
$instance
;
}
public
function
executeSave
(
$request
)
{
$model
=
$request
[
'model'
]
;
$data
=
$request
[
'data'
]
;
$model
=
$request
->
get
(
'model'
)
;
$data
=
$request
->
get
(
'data'
)
;
$record
=
new
$model
();
$record
->
fromArray
(
$data
);
...
...
@@ -54,31 +58,57 @@ class Doctrine_Resource_Server extends Doctrine_Resource
public
function
executeQuery
(
$request
)
{
$dql
=
$request
[
'dql'
]
;
$params
=
isset
(
$request
[
'params'
])
?
$request
[
'params'
]
:
array
();
$dql
=
$request
->
get
(
'dql'
)
;
$params
=
$request
->
get
(
'params'
)
?
$request
->
get
(
'params'
)
:
array
();
$conn
=
Doctrine_Manager
::
connection
();
return
$conn
->
query
(
$dql
,
$params
)
->
toArray
(
true
,
true
);
}
public
function
executeLoad
(
$request
)
{
$path
=
'/tmp/'
.
rand
()
.
'.'
.
$request
->
get
(
'format'
);
$models
=
$this
->
getConfig
(
'models'
)
?
$this
->
getConfig
(
'models'
)
:
array
();
$export
=
new
Doctrine_Export_Schema
();
$export
->
exportSchema
(
$path
,
$request
->
get
(
'format'
),
null
,
$models
);
$schema
=
Doctrine_Parser
::
load
(
$path
,
$request
->
get
(
'format'
));
unlink
(
$path
);
return
$schema
;
}
public
function
execute
(
$request
)
{
if
(
!
isset
(
$request
[
'type'
]))
{
throw
new
Doctrine_Resource_Exception
(
'You must specify a request type
: query or save
'
);
throw
new
Doctrine_Resource_Exception
(
'You must specify a request type'
);
}
$format
=
isset
(
$request
[
'format'
])
?
$request
[
'format'
]
:
'xml'
;
$type
=
$request
[
'type'
];
$funcName
=
'execute'
.
Doctrine
::
classify
(
$type
);
$re
sult
=
$this
->
$funcName
(
$request
);
$re
quest
=
new
Doctrine_Resource_Request
(
$request
);
return
Doctrine_Parser
::
dump
(
$result
,
$format
);
if
(
method_exists
(
$this
,
$funcName
))
{
$result
=
$this
->
$funcName
(
$request
);
}
else
{
throw
new
Doctrine_Resource_Exception
(
'Unknown Doctrine Resource Server function'
);
}
if
(
$result
)
{
return
Doctrine_Parser
::
dump
(
$result
,
$format
);
}
else
{
return
false
;
}
}
public
function
run
(
$request
)
{
echo
$this
->
execute
(
$request
);
}
}
}
\ No newline at end of file
playground/index.php
View file @
3438aef5
...
...
@@ -7,18 +7,20 @@ require_once('data.php');
$action
=
isset
(
$_REQUEST
[
'action'
])
?
$_REQUEST
[
'action'
]
:
'client'
;
if
(
$action
==
'server'
)
{
$config
=
array
();
$config
=
array
(
'name'
=>
'Doctrine_Resource_Test_Server'
,
'models'
=>
$tables
);
$server
=
new
Doctrine_Resource_Server
(
$config
);
$server
=
Doctrine_Resource_Server
::
getInstance
(
$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
();
$client
=
Doctrine_Resource_Client
::
getInstance
(
$config
);
$
user
=
$client
->
newRecord
(
'User'
)
;
$
user
->
name
=
'jonathan h. wage
'
;
$
user
->
save
();
print_r
(
$
record
->
toArray
());
print_r
(
$
user
->
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