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
1c782a08
Commit
1c782a08
authored
Sep 15, 2006
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support for application level default values
parent
7914695e
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
159 additions
and
81 deletions
+159
-81
DataDict.php
Doctrine/DataDict.php
+3
-8
Record.php
Doctrine/Record.php
+28
-1
Table.php
Doctrine/Table.php
+44
-3
datadict-sqlite.inc.php
Doctrine/adodb-hack/drivers/datadict-sqlite.inc.php
+1
-1
Getting started - Setting table definition - Default values.php
...g started - Setting table definition - Default values.php
+0
-0
Getting started - Setting table definition - Default values.php
...g started - Setting table definition - Default values.php
+0
-0
documentation.php
manual/documentation.php
+1
-0
RecordTestCase.php
tests/RecordTestCase.php
+74
-60
classes.php
tests/classes.php
+5
-5
run.php
tests/run.php
+3
-3
No files found.
Doctrine/DataDict.php
View file @
1c782a08
...
@@ -35,14 +35,9 @@ class Doctrine_DataDict {
...
@@ -35,14 +35,9 @@ class Doctrine_DataDict {
if
(
!
is_array
(
$args
[
2
]))
if
(
!
is_array
(
$args
[
2
]))
$args
[
2
]
=
array
();
$args
[
2
]
=
array
();
$constraints
=
array
(
);
unset
(
$args
[
2
][
'default'
]
);
foreach
(
$args
[
2
]
as
$k
=>
$v
)
{
$constraints
=
array_keys
(
$args
[
2
]);
if
(
is_string
(
$k
))
$constraints
[]
=
$k
;
else
$constraints
[]
=
$v
;
}
$r
[]
=
$name
.
" "
.
$this
->
getADOType
(
$args
[
0
],
$args
[
1
])
.
" "
.
implode
(
' '
,
$constraints
);
$r
[]
=
$name
.
" "
.
$this
->
getADOType
(
$args
[
0
],
$args
[
1
])
.
" "
.
implode
(
' '
,
$constraints
);
}
}
...
...
Doctrine/Record.php
View file @
1c782a08
...
@@ -188,6 +188,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
...
@@ -188,6 +188,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
else
else
$this
->
state
=
Doctrine_Record
::
STATE_TCLEAN
;
$this
->
state
=
Doctrine_Record
::
STATE_TCLEAN
;
// set the default values for this record
$this
->
setDefaultValues
();
// listen the onCreate event
// listen the onCreate event
$this
->
table
->
getAttribute
(
Doctrine
::
ATTR_LISTENER
)
->
onCreate
(
$this
);
$this
->
table
->
getAttribute
(
Doctrine
::
ATTR_LISTENER
)
->
onCreate
(
$this
);
...
@@ -233,6 +236,27 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
...
@@ -233,6 +236,27 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
public
function
getOID
()
{
public
function
getOID
()
{
return
$this
->
oid
;
return
$this
->
oid
;
}
}
/**
* setDefaultValues
* sets the default values
*
* @param boolean $overwrite whether or not to overwrite the already set values
* @return boolean
*/
public
function
setDefaultValues
(
$overwrite
=
false
)
{
if
(
!
$this
->
table
->
hasDefaultValues
())
return
false
;
foreach
(
$this
->
data
as
$column
=>
$value
)
{
$default
=
$this
->
table
->
getDefaultValueOf
(
$column
);
if
(
$default
===
null
)
$default
=
self
::
$null
;
if
(
$value
===
self
::
$null
||
$overwrite
)
$this
->
data
[
$column
]
=
$default
;
}
}
/**
/**
* cleanData
* cleanData
* modifies data array
* modifies data array
...
@@ -466,7 +490,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
...
@@ -466,7 +490,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$id
=
array_values
(
$id
);
$id
=
array_values
(
$id
);
$query
=
$this
->
table
->
getQuery
()
.
" WHERE "
.
implode
(
" = ? AND "
,
$this
->
table
->
getPrimaryKeys
())
.
" = ?"
;
$query
=
$this
->
table
->
getQuery
()
.
" WHERE "
.
implode
(
" = ? AND "
,
$this
->
table
->
getPrimaryKeys
())
.
" = ?"
;
$this
->
data
=
$this
->
table
->
getConnection
()
->
execute
(
$query
,
$id
)
->
fetch
(
PDO
::
FETCH_ASSOC
);
$stmt
=
$this
->
table
->
getConnection
()
->
execute
(
$query
,
$id
);
$this
->
data
=
$stmt
->
fetch
(
PDO
::
FETCH_ASSOC
);
if
(
!
$this
->
data
)
if
(
!
$this
->
data
)
throw
new
Doctrine_Record_Exception
(
'Failed to refresh. Record does not exist anymore'
);
throw
new
Doctrine_Record_Exception
(
'Failed to refresh. Record does not exist anymore'
);
...
...
Doctrine/Table.php
View file @
1c782a08
...
@@ -123,6 +123,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
...
@@ -123,6 +123,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* @var array $enum enum value arrays
* @var array $enum enum value arrays
*/
*/
private
$enum
=
array
();
private
$enum
=
array
();
/**
* @var boolean $hasDefaultValues whether or not this table has default values
*/
private
$hasDefaultValues
;
...
@@ -176,7 +180,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
...
@@ -176,7 +180,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
switch
(
count
(
$this
->
primaryKeys
))
:
switch
(
count
(
$this
->
primaryKeys
))
:
case
0
:
case
0
:
$this
->
columns
=
array_merge
(
array
(
"id"
=>
array
(
"integer"
,
11
,
array
(
"autoincrement"
,
"primary"
))),
$this
->
columns
);
$this
->
columns
=
array_merge
(
array
(
"id"
=>
array
(
"integer"
,
11
,
array
(
"autoincrement"
=>
true
,
"primary"
=>
true
))),
$this
->
columns
);
$this
->
primaryKeys
[]
=
"id"
;
$this
->
primaryKeys
[]
=
"id"
;
$this
->
identifier
=
"id"
;
$this
->
identifier
=
"id"
;
$this
->
identifierType
=
Doctrine_Identifier
::
AUTO_INCREMENT
;
$this
->
identifierType
=
Doctrine_Identifier
::
AUTO_INCREMENT
;
...
@@ -193,7 +197,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
...
@@ -193,7 +197,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$found
=
false
;
$found
=
false
;
foreach
(
$e
as
$option
)
{
foreach
(
$e
as
$option
=>
$value
)
{
if
(
$found
)
if
(
$found
)
break
;
break
;
...
@@ -279,13 +283,50 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
...
@@ -279,13 +283,50 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
if
(
is_string
(
$options
))
if
(
is_string
(
$options
))
$options
=
explode
(
'|'
,
$options
);
$options
=
explode
(
'|'
,
$options
);
foreach
(
$options
as
$k
=>
$option
)
{
if
(
is_numeric
(
$k
))
{
if
(
!
empty
(
$option
))
$options
[
$option
]
=
true
;
unset
(
$options
[
$k
]);
}
}
$this
->
columns
[
$name
]
=
array
(
$type
,
$length
,
$options
);
$this
->
columns
[
$name
]
=
array
(
$type
,
$length
,
$options
);
if
(
i
n_array
(
"primary"
,
$options
))
{
if
(
i
sset
(
$options
[
'primary'
]
))
{
$this
->
primaryKeys
[]
=
$name
;
$this
->
primaryKeys
[]
=
$name
;
}
}
if
(
isset
(
$options
[
'default'
]))
{
$this
->
hasDefaultValues
=
true
;
}
}
}
/**
* hasDefaultValues
* returns true if this table has default values, otherwise false
*
* @return boolean
*/
public
function
hasDefaultValues
()
{
return
$this
->
hasDefaultValues
;
}
/**
* getDefaultValueOf
* returns the default value(if any) for given column
*
* @param string $column
* @return mixed
*/
public
function
getDefaultValueOf
(
$column
)
{
if
(
!
isset
(
$this
->
columns
[
$column
]))
throw
new
Doctrine_Table_Exception
(
"Couldn't get default value. Column "
.
$column
.
" doesn't exist."
);
if
(
isset
(
$this
->
columns
[
$column
][
2
][
'default'
]))
{
return
$this
->
columns
[
$column
][
2
][
'default'
];
}
else
return
null
;
}
/**
/**
* @return mixed
* @return mixed
*/
*/
...
...
Doctrine/adodb-hack/drivers/datadict-sqlite.inc.php
View file @
1c782a08
...
@@ -63,7 +63,7 @@ class ADODB2_sqlite extends ADODB_DataDict {
...
@@ -63,7 +63,7 @@ class ADODB2_sqlite extends ADODB_DataDict {
return
$suffix
;
return
$suffix
;
}
}
function
_TableSQL
(
$tabname
,
$lines
,
$pkey
,
$tableoptions
)
function
_TableSQL
(
$tabname
,
$lines
,
$pkey
,
$tableoptions
)
{
{
$sql
=
array
();
$sql
=
array
();
...
...
manual/codes/Getting started - Setting table definition - Default values.php
0 → 100644
View file @
1c782a08
manual/docs/Getting started - Setting table definition - Default values.php
0 → 100644
View file @
1c782a08
manual/documentation.php
View file @
1c782a08
...
@@ -96,6 +96,7 @@ $menu = array("Getting started" =>
...
@@ -96,6 +96,7 @@ $menu = array("Getting started" =>
"Introduction"
,
"Introduction"
,
"Data types and lengths"
,
"Data types and lengths"
,
"Constraints and validators"
,
"Constraints and validators"
,
"Default values"
,
"Enum emulation"
"Enum emulation"
),
),
"Record identifiers"
=>
array
(
"Record identifiers"
=>
array
(
...
...
tests/RecordTestCase.php
View file @
1c782a08
...
@@ -5,9 +5,82 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
...
@@ -5,9 +5,82 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public
function
prepareTables
()
{
public
function
prepareTables
()
{
$this
->
tables
[]
=
"enumTest"
;
$this
->
tables
[]
=
"enumTest"
;
$this
->
tables
[]
=
"fieldNameTest"
;
parent
::
prepareTables
();
parent
::
prepareTables
();
}
}
public
function
testEnumType
()
{
$enum
=
new
EnumTest
();
$enum
->
status
=
"open"
;
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
save
();
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
refresh
();
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
status
=
"closed"
;
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$enum
->
save
();
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$this
->
assertTrue
(
is_numeric
(
$enum
->
id
));
$enum
->
refresh
();
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
}
public
function
testEnumTypeWithCaseConversion
()
{
$this
->
dbh
->
setAttribute
(
PDO
::
ATTR_CASE
,
PDO
::
CASE_UPPER
);
$enum
=
new
EnumTest
();
$enum
->
status
=
"open"
;
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
save
();
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
refresh
();
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
status
=
"closed"
;
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$enum
->
save
();
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$enum
->
refresh
();
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$this
->
dbh
->
setAttribute
(
PDO
::
ATTR_CASE
,
PDO
::
CASE_NATURAL
);
}
public
function
testFailingRefresh
()
{
$enum
=
$this
->
connection
->
getTable
(
'EnumTest'
)
->
find
(
1
);
$this
->
dbh
->
query
(
'DELETE FROM enum_test WHERE id = 1'
);
$f
=
false
;
try
{
$enum
->
refresh
();
}
catch
(
Doctrine_Record_Exception
$e
)
{
$f
=
true
;
}
$this
->
assertTrue
(
$f
);
}
public
function
testDefaultValues
()
{
$test
=
new
FieldNameTest
;
$this
->
assertEqual
(
$test
->
someColumn
,
'some string'
);
$this
->
assertEqual
(
$test
->
someEnum
,
'php'
);
$this
->
assertEqual
(
$test
->
someArray
,
array
());
$this
->
assertTrue
(
is_object
(
$test
->
someObject
));
$this
->
assertEqual
(
$test
->
someInt
,
11
);
}
public
function
testJoinTableSelfReferencingInsertingData
()
{
public
function
testJoinTableSelfReferencingInsertingData
()
{
$e
=
new
Entity
();
$e
=
new
Entity
();
$e
->
name
=
"Entity test"
;
$e
->
name
=
"Entity test"
;
...
@@ -210,66 +283,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
...
@@ -210,66 +283,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
}
}
public
function
testEnumType
()
{
$enum
=
new
EnumTest
();
$enum
->
status
=
"open"
;
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
save
();
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
refresh
();
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
status
=
"closed"
;
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$enum
->
save
();
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$enum
->
refresh
();
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
}
public
function
testEnumTypeWithCaseConversion
()
{
$this
->
dbh
->
setAttribute
(
PDO
::
ATTR_CASE
,
PDO
::
CASE_UPPER
);
$enum
=
new
EnumTest
();
$enum
->
status
=
"open"
;
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
save
();
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
refresh
();
$this
->
assertEqual
(
$enum
->
status
,
"open"
);
$enum
->
status
=
"closed"
;
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$enum
->
save
();
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$enum
->
refresh
();
$this
->
assertEqual
(
$enum
->
status
,
"closed"
);
$this
->
dbh
->
setAttribute
(
PDO
::
ATTR_CASE
,
PDO
::
CASE_NATURAL
);
}
public
function
testFailingRefresh
()
{
$enum
=
$this
->
connection
->
getTable
(
'EnumTest'
)
->
find
(
1
);
$this
->
dbh
->
query
(
'DELETE FROM enum_test WHERE id = 1'
);
$f
=
false
;
try
{
$enum
->
refresh
();
}
catch
(
Doctrine_Record_Exception
$e
)
{
$f
=
true
;
}
$this
->
assertTrue
(
$f
);
}
public
function
testSerialize
()
{
public
function
testSerialize
()
{
$user
=
$this
->
connection
->
getTable
(
"User"
)
->
find
(
4
);
$user
=
$this
->
connection
->
getTable
(
"User"
)
->
find
(
4
);
...
@@ -918,5 +931,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
...
@@ -918,5 +931,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$user
=
$this
->
connection
->
getTable
(
"User"
)
->
find
(
4
);
$user
=
$this
->
connection
->
getTable
(
"User"
)
->
find
(
4
);
$this
->
assertTrue
(
$user
->
getIterator
()
instanceof
ArrayIterator
);
$this
->
assertTrue
(
$user
->
getIterator
()
instanceof
ArrayIterator
);
}
}
}
}
?>
?>
tests/classes.php
View file @
1c782a08
...
@@ -19,11 +19,11 @@ class Entity extends Doctrine_Record {
...
@@ -19,11 +19,11 @@ class Entity extends Doctrine_Record {
}
}
class
FieldNameTest
extends
Doctrine_Record
{
class
FieldNameTest
extends
Doctrine_Record
{
public
function
setTableDefinition
()
{
public
function
setTableDefinition
()
{
$this
->
hasColumn
(
"someColumn"
,
"string"
,
200
);
$this
->
hasColumn
(
"someColumn"
,
"string"
,
200
,
array
(
'default'
=>
'some string'
)
);
$this
->
hasColumn
(
"someEnum"
,
"enum"
,
4
);
$this
->
hasColumn
(
"someEnum"
,
"enum"
,
4
,
array
(
'default'
=>
'php'
)
);
$this
->
hasColumn
(
"someArray"
,
"array"
,
100
);
$this
->
hasColumn
(
"someArray"
,
"array"
,
100
,
array
(
'default'
=>
array
())
);
$this
->
hasColumn
(
"someObject"
,
"
array"
,
200
);
$this
->
hasColumn
(
"someObject"
,
"
object"
,
200
,
array
(
'default'
=>
new
stdClass
)
);
$this
->
hasColumn
(
"someInt"
,
"integer"
);
$this
->
hasColumn
(
"someInt"
,
"integer"
,
20
,
array
(
'default'
=>
11
)
);
$this
->
setEnumValues
(
"someEnum"
,
array
(
'php'
,
'java'
,
'python'
));
$this
->
setEnumValues
(
"someEnum"
,
array
(
'php'
,
'java'
,
'python'
));
...
...
tests/run.php
View file @
1c782a08
...
@@ -32,12 +32,12 @@ error_reporting(E_ALL);
...
@@ -32,12 +32,12 @@ error_reporting(E_ALL);
$test
=
new
GroupTest
(
"Doctrine Framework Unit Tests"
);
$test
=
new
GroupTest
(
"Doctrine Framework Unit Tests"
);
$test
->
addTestCase
(
new
Doctrine_RecordTestCase
());
$test
->
addTestCase
(
new
Doctrine_AccessTestCase
());
$test
->
addTestCase
(
new
Doctrine_AccessTestCase
());
$test
->
addTestCase
(
new
Doctrine_EventListenerTestCase
());
$test
->
addTestCase
(
new
Doctrine_EventListenerTestCase
());
$test
->
addTestCase
(
new
Doctrine_RecordTestCase
());
$test
->
addTestCase
(
new
Doctrine_TableTestCase
());
$test
->
addTestCase
(
new
Doctrine_TableTestCase
());
$test
->
addTestCase
(
new
Doctrine_ConnectionTestCase
());
$test
->
addTestCase
(
new
Doctrine_ConnectionTestCase
());
...
...
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