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
c9658cb2
Commit
c9658cb2
authored
Sep 13, 2007
by
Jonathan.Wage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Getting exporting/importing working.
parent
13d781cb
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
238 additions
and
172 deletions
+238
-172
Schema.php
lib/Doctrine/Export/Schema.php
+93
-4
Xml.php
lib/Doctrine/Export/Schema/Xml.php
+1
-17
Yml.php
lib/Doctrine/Export/Schema/Yml.php
+1
-17
Xml.php
lib/Doctrine/Import/Schema/Xml.php
+13
-14
Yml.php
lib/Doctrine/Import/Schema/Yml.php
+3
-3
Xml.php
lib/Doctrine/Parser/Xml.php
+19
-18
index.php
playground/index.php
+2
-0
playground.php
playground/playground.php
+36
-32
schema.xml
tests/schema.xml
+40
-38
schema.yml
tests/schema.yml
+30
-29
No files found.
lib/Doctrine/Export/Schema.php
View file @
c9658cb2
...
...
@@ -57,7 +57,49 @@ abstract class Doctrine_Export_Schema
* @param string $schema
* @return void
*/
abstract
function
dump
(
$array
,
$schema
);
public
function
dump
(
$array
,
$schema
)
{
$data
=
$this
->
build
(
$array
);
file_put_contents
(
$schema
,
$data
);
}
public
function
getDirectoryTables
(
$directory
)
{
$parent
=
new
ReflectionClass
(
'Doctrine_Record'
);
$declared
=
get_declared_classes
();
if
(
$directory
!==
null
)
{
foreach
((
array
)
$directory
as
$dir
)
{
$it
=
new
RecursiveIteratorIterator
(
new
RecursiveDirectoryIterator
(
$dir
),
RecursiveIteratorIterator
::
LEAVES_ONLY
);
foreach
(
$it
as
$file
)
{
$e
=
explode
(
'.'
,
$file
->
getFileName
());
if
(
end
(
$e
)
===
'php'
&&
strpos
(
$file
->
getFileName
(),
'.inc'
)
===
false
)
{
require_once
$file
->
getPathName
();
}
}
}
$declared
=
get_declared_classes
();
$tables
=
array
();
foreach
(
$declared
as
$name
)
{
$class
=
new
ReflectionClass
(
$name
);
if
(
$class
->
isSubclassOf
(
$parent
)
AND
!
$class
->
isAbstract
())
{
$tables
[
$name
]
=
$name
;
}
}
return
$tables
;
}
}
/**
* buildSchema
...
...
@@ -69,8 +111,55 @@ abstract class Doctrine_Export_Schema
*/
public
function
buildSchema
(
$directory
)
{
// we need to figure out how we can build all the model information for the passed directory/directories
return
array
();
$array
=
array
(
'tables'
=>
array
());
$tables
=
$this
->
getDirectoryTables
(
$directory
);
$parent
=
new
ReflectionClass
(
'Doctrine_Record'
);
$sql
=
array
();
$fks
=
array
();
// we iterate trhough the diff of previously declared classes
// and currently declared classes
foreach
(
$tables
as
$name
)
{
$class
=
new
ReflectionClass
(
$name
);
$conn
=
Doctrine_Manager
::
getInstance
()
->
getConnectionForComponent
(
$name
);
// check if class is an instance of Doctrine_Record and not abstract
// class must have method setTableDefinition (to avoid non-Record subclasses like symfony's sfDoctrineRecord)
// we have to recursively iterate through the class parents just to be sure that the classes using for example
// column aggregation inheritance are properly exported to database
while
(
$class
->
isAbstract
()
||
!
$class
->
isSubclassOf
(
$parent
)
||
!
$class
->
hasMethod
(
'setTableDefinition'
)
||
(
$class
->
hasMethod
(
'setTableDefinition'
)
&&
$class
->
getMethod
(
'setTableDefinition'
)
->
getDeclaringClass
()
->
getName
()
!==
$class
->
getName
()))
{
$class
=
$class
->
getParentClass
();
if
(
$class
===
false
)
{
break
;
}
}
if
(
$class
===
false
)
{
continue
;
}
$record
=
new
$name
();
$table
=
$record
->
getTable
();
$data
=
$table
->
getExportableFormat
();
$table
=
array
();
$table
[
'name'
]
=
$data
[
'tableName'
];
$table
[
'class'
]
=
get_class
(
$record
);
$table
[
'columns'
]
=
$data
[
'columns'
];
$array
[
'tables'
][
$data
[
'tableName'
]]
=
$table
;
}
return
$array
;
}
/**
...
...
@@ -84,6 +173,6 @@ abstract class Doctrine_Export_Schema
{
$array
=
$this
->
buildSchema
(
$directory
);
$this
->
dump
(
$arr
,
$schema
);
return
$this
->
dump
(
$array
,
$schema
);
}
}
\ No newline at end of file
lib/Doctrine/Export/Schema/Xml.php
View file @
c9658cb2
...
...
@@ -41,22 +41,6 @@ class Doctrine_Export_Schema_Xml extends Doctrine_Export_Schema
*/
public
function
build
(
$array
)
{
return
Doctrime_Parser
::
dump
(
$array
,
null
,
'xml'
);
}
/**
* dump
*
* Dump the array to the schema file
*
* @param string $array
* @param string $schema
* @return void
*/
public
function
dump
(
$array
,
$schema
)
{
$xml
=
$this
->
build
(
$array
);
file_put_contents
(
$schema
,
$xml
);
return
Doctrine_Parser
::
dumpXml
(
$array
,
null
);
}
}
\ No newline at end of file
lib/Doctrine/Export/Schema/Yml.php
View file @
c9658cb2
...
...
@@ -41,22 +41,6 @@ class Doctrine_Export_Schema_Yml extends Doctrine_Export_Schema
*/
public
function
build
(
$array
)
{
return
Doctrime_Parser
::
dump
(
$array
,
null
,
'yml'
);
}
/**
* dump
*
* Dump the array to the schema file
*
* @param string $arr
* @param string $schema
* @return void
*/
public
function
dump
(
$arr
,
$schema
)
{
$yml
=
$this
->
build
(
$array
);
file_put_contents
(
$schema
,
$yml
);
return
Doctrine_Parser
::
dumpYml
(
$array
,
null
);
}
}
\ No newline at end of file
lib/Doctrine/Import/Schema/Xml.php
View file @
c9658cb2
...
...
@@ -52,26 +52,25 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
{
$xmlObj
=
$this
->
parse
(
$schema
);
// Go through all tables...
foreach
(
$xmlObj
->
table
as
$table
)
{
foreach
(
$xmlObj
->
tables
->
table
as
$table
)
{
$columns
=
array
();
// Go through all columns...
foreach
(
$table
->
declaration
->
field
as
$field
)
{
foreach
(
$table
->
columns
as
$column
)
{
$colDesc
=
array
(
'name'
=>
(
string
)
$
field
->
name
,
'type'
=>
(
string
)
$
field
->
type
,
'ptype'
=>
(
string
)
$
field
->
type
,
'length'
=>
(
int
)
$
field
->
length
,
'fixed'
=>
(
int
)
$
field
->
fixed
,
'unsigned'
=>
(
bool
)
$
field
->
unsigned
,
'primary'
=>
(
bool
)
(
isset
(
$
field
->
primary
)
&&
$field
->
primary
),
'default'
=>
(
string
)
$
field
->
default
,
'notnull'
=>
(
bool
)
(
isset
(
$
field
->
notnull
)
&&
$field
->
notnull
),
'autoinc'
=>
(
bool
)
(
isset
(
$
field
->
autoincrement
)
&&
$field
->
autoincrement
),
'name'
=>
(
string
)
$
column
->
name
,
'type'
=>
(
string
)
$
column
->
type
,
'ptype'
=>
(
string
)
$
column
->
type
,
'length'
=>
(
int
)
$
column
->
length
,
'fixed'
=>
(
int
)
$
column
->
fixed
,
'unsigned'
=>
(
bool
)
$
column
->
unsigned
,
'primary'
=>
(
bool
)
(
isset
(
$
column
->
primary
)
&&
$column
->
primary
),
'default'
=>
(
string
)
$
column
->
default
,
'notnull'
=>
(
bool
)
(
isset
(
$
column
->
notnull
)
&&
$column
->
notnull
),
'autoinc'
=>
(
bool
)
(
isset
(
$
column
->
autoincrement
)
&&
$column
->
autoincrement
),
);
$columns
[(
string
)
$
field
->
name
]
=
$colDesc
;
$columns
[(
string
)
$
column
->
name
]
=
$colDesc
;
}
$class
=
$table
->
class
?
(
string
)
$table
->
class
:
(
string
)
$table
->
name
;
...
...
lib/Doctrine/Import/Schema/Yml.php
View file @
c9658cb2
...
...
@@ -51,12 +51,12 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
public
function
parseSchema
(
$schema
)
{
$array
=
$this
->
parse
(
$schema
);
$tables
=
$array
[
'schema'
][
'tables'
];
// Go through all tables...
foreach
(
$array
[
'tables'
]
as
$table
)
{
foreach
(
$tables
as
$table
)
{
$columns
=
array
();
foreach
(
$table
[
'
declaration
'
]
as
$field
)
{
foreach
(
$table
[
'
columns
'
]
as
$field
)
{
$colDesc
=
array
(
'name'
=>
isset
(
$field
[
'name'
])
?
(
string
)
$field
[
'name'
]
:
null
,
'type'
=>
isset
(
$field
[
'type'
])
?
(
string
)
$field
[
'type'
]
:
null
,
...
...
lib/Doctrine/Parser/Xml.php
View file @
c9658cb2
...
...
@@ -31,26 +31,27 @@
*/
class
Doctrine_Parser_Xml
extends
Doctrine_Parser
{
public
function
arrayToXml
(
$
array
)
public
function
arrayToXml
(
$
data
,
$rootNodeName
=
'data'
,
$xml
=
null
)
{
$this
->
text
=
"<?xml version=
\"
1.0
\"
encoding=
\"
iso-8859-1
\"
?>"
;
$this
->
text
.=
$this
->
arrayTransform
(
$array
);
return
$this
->
text
;
}
if
(
$xml
===
null
)
{
$xml
=
new
SimpleXmlElement
(
"<?xml version=
\"
1.0
\"
encoding=
\"
utf-8
\"
?><
$rootNodeName
/>"
);
}
foreach
(
$data
as
$key
=>
$value
)
{
if
(
is_array
(
$value
))
{
$node
=
$xml
->
addChild
(
$key
);
$this
->
arrayToXml
(
$value
,
$rootNodeName
,
$node
);
}
else
{
$value
=
htmlentities
(
$value
);
$xml
->
addChild
(
$key
,
$value
);
}
public
function
arrayTransform
(
$array
)
{
foreach
(
$array
as
$key
=>
$value
)
{
if
(
!
is_array
(
$value
))
{
$this
->
text
.=
"<
$key
>
$value
</
$key
>"
;
}
else
{
$this
->
text
.=
"<
$key
>"
;
$this
->
arrayTransform
(
$value
);
$this
->
text
.=
"</
$key
>"
;
}
}
}
return
$xml
->
asXML
();
}
public
function
dumpData
(
$array
,
$path
=
null
)
...
...
playground/index.php
View file @
c9658cb2
<?php
define
(
'LOAD_MODELS'
,
false
);
require_once
(
'playground.php'
);
\ No newline at end of file
playground/playground.php
View file @
c9658cb2
...
...
@@ -6,15 +6,17 @@ require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'doctrine/Doctrine.php';
spl_autoload_register
(
array
(
'Doctrine'
,
'autoload'
));
$modelsPath
=
dirname
(
__FILE__
)
.
DIRECTORY_SEPARATOR
.
'models'
;
// include the models
$models
=
new
DirectoryIterator
(
$modelsPath
);
foreach
(
$models
as
$key
=>
$file
)
{
if
(
$file
->
isFile
()
&&
!
$file
->
isDot
())
{
$e
=
explode
(
'.'
,
$file
->
getFileName
());
if
(
end
(
$e
)
===
'php'
)
{
require_once
$file
->
getPathname
();
if
(
constant
(
'LOAD_MODELS'
))
{
$modelsPath
=
dirname
(
__FILE__
)
.
DIRECTORY_SEPARATOR
.
'models'
;
// include the models
$models
=
new
DirectoryIterator
(
$modelsPath
);
foreach
(
$models
as
$key
=>
$file
)
{
if
(
$file
->
isFile
()
&&
!
$file
->
isDot
())
{
$e
=
explode
(
'.'
,
$file
->
getFileName
());
if
(
end
(
$e
)
===
'php'
)
{
require_once
$file
->
getPathname
();
}
}
}
}
...
...
@@ -25,26 +27,28 @@ $dbh = new PDO('sqlite::memory:');
$conn
=
Doctrine_Manager
::
connection
(
$dbh
);
$manager
=
Doctrine_Manager
::
getInstance
();
$manager
->
setAttribute
(
Doctrine
::
ATTR_EXPORT
,
Doctrine
::
EXPORT_ALL
);
$tables
=
array
(
'entity'
,
'entityReference'
,
'email'
,
'phonenumber'
,
'groupuser'
,
'album'
,
'song'
,
'element'
,
'error'
,
'description'
,
'address'
,
'account'
,
'task'
,
'resource'
,
'assignment'
,
'resourceType'
,
'resourceReference'
);
$conn
->
export
->
exportClasses
(
$tables
);
require_once
(
'data.php'
);
\ No newline at end of file
if
(
constant
(
'LOAD_MODELS'
))
{
$manager
->
setAttribute
(
Doctrine
::
ATTR_EXPORT
,
Doctrine
::
EXPORT_ALL
);
$tables
=
array
(
'entity'
,
'entityReference'
,
'email'
,
'phonenumber'
,
'groupuser'
,
'album'
,
'song'
,
'element'
,
'error'
,
'description'
,
'address'
,
'account'
,
'task'
,
'resource'
,
'assignment'
,
'resourceType'
,
'resourceReference'
);
$conn
->
export
->
exportClasses
(
$tables
);
require_once
(
'data.php'
);
}
\ No newline at end of file
tests/schema.xml
View file @
c9658cb2
<?xml version="1.0" encoding="ISO-8859-1" ?>
<tables>
<table>
<name>
user
</name>
<class>
User
</class>
<declaration>
<field>
<name>
id
</name>
<type>
integer
</type>
<notnull>
true
</notnull>
<autoincrement>
true
</autoincrement>
</field>
<field>
<name>
username
</name>
<type>
string
</type>
<length>
20
</length>
<notnull>
true
</notnull>
</field>
</declaration>
</table>
<schema>
<tables>
<table>
<name>
user
</name>
<class>
User
</class>
<columns>
<column>
<name>
id
</name>
<type>
integer
</type>
<notnull>
true
</notnull>
<autoincrement>
true
</autoincrement>
</column>
<column>
<name>
username
</name>
<type>
string
</type>
<length>
20
</length>
<notnull>
true
</notnull>
</column>
</columns>
</table>
<table>
<name>
group
</name>
<class>
Group
</class>
<declaration>
<field>
<name>
id
</name>
<type>
integer
</type>
<notnull>
true
</notnull>
<autoincrement>
true
</autoincrement>
</field>
<field>
<name>
name
</name>
<type>
string
</type>
<length>
20
</length>
<notnull>
true
</notnull>
</field>
</declaration>
</table>
</tables>
\ No newline at end of file
<table>
<name>
group
</name>
<class>
Group
</class>
<columns>
<column>
<name>
id
</name>
<type>
integer
</type>
<notnull>
true
</notnull>
<autoincrement>
true
</autoincrement>
</column>
<column>
<name>
name
</name>
<type>
string
</type>
<length>
20
</length>
<notnull>
true
</notnull>
</column>
</columns>
</table>
</tables>
</schema>
\ No newline at end of file
tests/schema.yml
View file @
c9658cb2
---
tables
:
user
:
name
:
user
class
:
User
declaration
:
id
:
name
:
id
type
:
integer
notnull
:
true
autoincrement
:
true
username
:
name
:
username
type
:
string
length
:
20
notnull
:
true
group
:
name
:
group
class
:
Group
declaration
:
id
:
name
:
id
type
:
integer
notnull
:
true
autoincrement
:
true
name
:
name
:
name
type
:
string
length
:
20
notnull
:
true
\ No newline at end of file
schema
:
tables
:
user
:
name
:
user
class
:
User
columns
:
id
:
name
:
id
type
:
integer
notnull
:
true
autoincrement
:
true
username
:
name
:
username
type
:
string
length
:
20
notnull
:
true
group
:
name
:
group
class
:
Group
columns
:
id
:
name
:
id
type
:
integer
notnull
:
true
autoincrement
:
true
name
:
name
:
name
type
:
string
length
:
20
notnull
:
true
\ 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