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
Hide 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
...
...
@@ -34,15 +34,10 @@ class Doctrine_DataDict {
foreach
(
$columns
as
$name
=>
$args
)
{
if
(
!
is_array
(
$args
[
2
]))
$args
[
2
]
=
array
();
$constraints
=
array
();
foreach
(
$args
[
2
]
as
$k
=>
$v
)
{
if
(
is_string
(
$k
))
$constraints
[]
=
$k
;
else
$constraints
[]
=
$v
;
}
unset
(
$args
[
2
][
'default'
]);
$constraints
=
array_keys
(
$args
[
2
]);
$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
else
$this
->
state
=
Doctrine_Record
::
STATE_TCLEAN
;
// set the default values for this record
$this
->
setDefaultValues
();
// listen the onCreate event
$this
->
table
->
getAttribute
(
Doctrine
::
ATTR_LISTENER
)
->
onCreate
(
$this
);
...
...
@@ -233,6 +236,27 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
public
function
getOID
()
{
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
* modifies data array
...
...
@@ -466,7 +490,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$id
=
array_values
(
$id
);
$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
)
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 {
* @var array $enum enum value arrays
*/
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 {
switch
(
count
(
$this
->
primaryKeys
))
:
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
->
identifier
=
"id"
;
$this
->
identifierType
=
Doctrine_Identifier
::
AUTO_INCREMENT
;
...
...
@@ -193,7 +197,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$found
=
false
;
foreach
(
$e
as
$option
)
{
foreach
(
$e
as
$option
=>
$value
)
{
if
(
$found
)
break
;
...
...
@@ -279,13 +283,50 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
if
(
is_string
(
$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
);
if
(
i
n_array
(
"primary"
,
$options
))
{
if
(
i
sset
(
$options
[
'primary'
]
))
{
$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
*/
...
...
Doctrine/adodb-hack/drivers/datadict-sqlite.inc.php
View file @
1c782a08
...
...
@@ -63,7 +63,7 @@ class ADODB2_sqlite extends ADODB_DataDict {
return
$suffix
;
}
function
_TableSQL
(
$tabname
,
$lines
,
$pkey
,
$tableoptions
)
function
_TableSQL
(
$tabname
,
$lines
,
$pkey
,
$tableoptions
)
{
$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" =>
"Introduction"
,
"Data types and lengths"
,
"Constraints and validators"
,
"Default values"
,
"Enum emulation"
),
"Record identifiers"
=>
array
(
...
...
tests/RecordTestCase.php
View file @
1c782a08
...
...
@@ -5,9 +5,82 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public
function
prepareTables
()
{
$this
->
tables
[]
=
"enumTest"
;
$this
->
tables
[]
=
"fieldNameTest"
;
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
()
{
$e
=
new
Entity
();
$e
->
name
=
"Entity test"
;
...
...
@@ -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
()
{
$user
=
$this
->
connection
->
getTable
(
"User"
)
->
find
(
4
);
...
...
@@ -918,5 +931,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$user
=
$this
->
connection
->
getTable
(
"User"
)
->
find
(
4
);
$this
->
assertTrue
(
$user
->
getIterator
()
instanceof
ArrayIterator
);
}
}
?>
tests/classes.php
View file @
1c782a08
...
...
@@ -19,11 +19,11 @@ class Entity extends Doctrine_Record {
}
class
FieldNameTest
extends
Doctrine_Record
{
public
function
setTableDefinition
()
{
$this
->
hasColumn
(
"someColumn"
,
"string"
,
200
);
$this
->
hasColumn
(
"someEnum"
,
"enum"
,
4
);
$this
->
hasColumn
(
"someArray"
,
"array"
,
100
);
$this
->
hasColumn
(
"someObject"
,
"
array"
,
200
);
$this
->
hasColumn
(
"someInt"
,
"integer"
);
$this
->
hasColumn
(
"someColumn"
,
"string"
,
200
,
array
(
'default'
=>
'some string'
)
);
$this
->
hasColumn
(
"someEnum"
,
"enum"
,
4
,
array
(
'default'
=>
'php'
)
);
$this
->
hasColumn
(
"someArray"
,
"array"
,
100
,
array
(
'default'
=>
array
())
);
$this
->
hasColumn
(
"someObject"
,
"
object"
,
200
,
array
(
'default'
=>
new
stdClass
)
);
$this
->
hasColumn
(
"someInt"
,
"integer"
,
20
,
array
(
'default'
=>
11
)
);
$this
->
setEnumValues
(
"someEnum"
,
array
(
'php'
,
'java'
,
'python'
));
...
...
tests/run.php
View file @
1c782a08
...
...
@@ -32,11 +32,11 @@ error_reporting(E_ALL);
$test
=
new
GroupTest
(
"Doctrine Framework Unit Tests"
);
$test
->
addTestCase
(
new
Doctrine_
AccessTestCase
());
$test
->
addTestCase
(
new
Doctrine_
RecordTestCase
());
$test
->
addTestCase
(
new
Doctrine_
EventListener
TestCase
());
$test
->
addTestCase
(
new
Doctrine_
Access
TestCase
());
$test
->
addTestCase
(
new
Doctrine_
Record
TestCase
());
$test
->
addTestCase
(
new
Doctrine_
EventListener
TestCase
());
$test
->
addTestCase
(
new
Doctrine_TableTestCase
());
...
...
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