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
180d435e
Commit
180d435e
authored
Feb 16, 2007
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Relation model rewrite, first draft
parent
84cf99fb
Changes
14
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
304 additions
and
215 deletions
+304
-215
Collection.php
lib/Doctrine/Collection.php
+7
-10
Connection.php
lib/Doctrine/Connection.php
+3
-3
Pgsql.php
lib/Doctrine/Connection/Pgsql.php
+14
-0
Export.php
lib/Doctrine/Export.php
+10
-6
Mysql.php
lib/Doctrine/Export/Mysql.php
+24
-11
Lib.php
lib/Doctrine/Lib.php
+8
-24
Manager.php
lib/Doctrine/Manager.php
+1
-1
Record.php
lib/Doctrine/Record.php
+13
-2
Relation.php
lib/Doctrine/Relation.php
+76
-49
Association.php
lib/Doctrine/Relation/Association.php
+14
-29
Self.php
lib/Doctrine/Relation/Association/Self.php
+21
-14
ForeignKey.php
lib/Doctrine/Relation/ForeignKey.php
+10
-9
LocalKey.php
lib/Doctrine/Relation/LocalKey.php
+5
-5
Table.php
lib/Doctrine/Table.php
+98
-52
No files found.
lib/Doctrine/Collection.php
View file @
180d435e
...
...
@@ -261,7 +261,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
*
* @return void
*/
public
function
setReference
(
Doctrine_Record
$record
,
Doctrine_Relation
$relation
)
public
function
setReference
(
Doctrine_Record
$record
,
Doctrine_Relation
$relation
)
{
$this
->
reference
=
$record
;
$this
->
relation
=
$relation
;
...
...
@@ -607,30 +607,27 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$list
[]
=
$value
;
}
};
$query
->
from
(
$this
->
table
->
getComponentName
()
.
"("
.
implode
(
", "
,
$this
->
table
->
getPrimaryKeys
())
.
")"
);
$query
->
where
(
$this
->
table
->
getComponentName
()
.
".id IN ("
.
substr
(
str_repeat
(
"?, "
,
count
(
$list
)),
0
,
-
2
)
.
")"
);
$query
->
from
(
$this
->
table
->
getComponentName
()
.
'('
.
implode
(
", "
,
$this
->
table
->
getPrimaryKeys
())
.
')'
);
$query
->
where
(
$this
->
table
->
getComponentName
()
.
'.id IN ('
.
substr
(
str_repeat
(
"?, "
,
count
(
$list
)),
0
,
-
2
)
.
')'
);
return
$query
;
}
$rel
=
$this
->
table
->
getRelation
(
$name
);
$table
=
$rel
->
getTable
();
$foreign
=
$rel
->
getForeign
();
$local
=
$rel
->
getLocal
();
if
(
$rel
instanceof
Doctrine_Relation_LocalKey
||
$rel
instanceof
Doctrine_Relation_ForeignKey
)
{
foreach
(
$this
->
data
as
$record
)
{
$list
[]
=
$record
[
$
local
];
}
;
$list
[]
=
$record
[
$
rel
->
getLocal
()
];
}
}
else
{
foreach
(
$this
->
data
as
$record
)
{
$value
=
$record
->
getIncremented
();
if
(
$value
!==
null
)
{
$list
[]
=
$value
;
}
}
;
}
}
$this
->
table
->
getRelation
(
$name
);
$dql
=
$rel
->
getRelationDql
(
count
(
$list
),
'collection'
);
$coll
=
$query
->
query
(
$dql
,
$list
);
...
...
lib/Doctrine/Connection.php
View file @
180d435e
...
...
@@ -18,6 +18,7 @@
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
Doctrine
::
autoload
(
'Doctrine_Configurable'
);
/**
* Doctrine_Connection
*
...
...
@@ -481,8 +482,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
.
'VALUES ('
.
substr
(
str_repeat
(
'?, '
,
count
(
$values
)),
0
,
-
2
)
.
')'
;
// prepare and execute the statement
$stmt
=
$this
->
dbh
->
prepare
(
$query
);
$stmt
->
execute
(
array_values
(
$values
));
$this
->
execute
(
$query
,
array_values
(
$values
));
return
true
;
}
...
...
@@ -684,7 +684,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
}
}
catch
(
Doctrine_Adapter_Exception
$e
)
{
}
catch
(
PDOException
$e
)
{
}
print
Doctrine_Lib
::
formatSql
(
$query
);
$this
->
rethrowException
(
$e
);
}
/**
...
...
lib/Doctrine/Connection/Pgsql.php
View file @
180d435e
...
...
@@ -89,6 +89,20 @@ class Doctrine_Connection_Pgsql extends Doctrine_Connection_Common
$query
=
'SET NAMES '
.
$this
->
dbh
->
quote
(
$charset
);
$this
->
exec
(
$query
);
}
/**
* convertBoolean
* some drivers need the boolean values to be converted into integers
* when using DQL API
*
* This method takes care of that conversion
*
* @param array $item
* @return void
*/
public
function
convertBooleans
(
array
$items
)
{
return
$items
;
}
/**
* Changes a query string for various DBMS specific reasons
*
...
...
lib/Doctrine/Export.php
View file @
180d435e
...
...
@@ -615,7 +615,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
* The onDelete and onUpdate keys accept the following values:
*
* CASCADE: Delete or update the row from the parent table and automatically delete or
* update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
* update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
* Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
* in the parent table or in the child table.
*
...
...
@@ -624,7 +624,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
* specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
*
* NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary
* key value is not allowed to proceed if there is a related foreign key value in the referenced table.
* key value is not allowed to proceed if there is a related foreign key value in the referenced table.
*
* RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
* omitting the ON DELETE or ON UPDATE clause.
...
...
@@ -634,9 +634,9 @@ class Doctrine_Export extends Doctrine_Connection_Module
* @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
* of a field declaration.
*/
public
function
getForeignKeyDeclaration
(
$definition
)
public
function
getForeignKeyDeclaration
(
array
$definition
)
{
$sql
=
$this
->
getForeignKeyBaseDeclaration
();
$sql
=
$this
->
getForeignKeyBaseDeclaration
(
$definition
);
if
(
isset
(
$definition
[
'deferred'
]))
{
$sql
.=
' '
.
$this
->
getForeignKeyDeferredDeclaration
();
...
...
@@ -673,15 +673,19 @@ class Doctrine_Export extends Doctrine_Connection_Module
}
/**
* getForeignKeyBaseDeclaration
* Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
* of a field declaration to be used in statements like CREATE TABLE.
*
* @param array $definition
* @return string
*/
public
function
getForeignKeyBaseDeclaration
()
public
function
getForeignKeyBaseDeclaration
(
array
$definition
)
{
$sql
=
''
;
if
(
isset
(
$definition
[
'name'
]))
{
$sql
.=
'CONSTRAINT '
.
$definition
[
'name'
];
$sql
.=
'CONSTRAINT '
.
$definition
[
'name'
]
.
' '
;
}
$sql
.=
'FOREIGN KEY '
;
if
(
!
isset
(
$definition
[
'local'
]))
{
throw
new
Doctrine_Export_Exception
(
'Local reference field missing from definition.'
);
}
...
...
lib/Doctrine/Export/Mysql.php
View file @
180d435e
...
...
@@ -86,7 +86,6 @@ class Doctrine_Export_Mysql extends Doctrine_Export
* 'comment' => 'Foo',
* 'charset' => 'utf8',
* 'collate' => 'utf8_unicode_ci',
* 'collate' => 'utf8_unicode_ci',
* 'type' => 'innodb',
* );
*
...
...
@@ -106,6 +105,13 @@ class Doctrine_Export_Mysql extends Doctrine_Export
$queryFields
.=
', '
.
$this
->
getIndexDeclaration
(
$index
,
$definition
);
}
}
if
(
isset
(
$options
[
'foreignKeys'
])
&&
!
empty
(
$options
[
'foreignKeys'
]))
{
foreach
(
$options
[
'foreignKeys'
]
as
$definition
)
{
$queryFields
.=
', '
.
$this
->
getForeignKeyDeclaration
(
$definition
);
}
}
if
(
isset
(
$options
[
'primary'
])
&&
!
empty
(
$options
[
'primary'
]))
{
$queryFields
.=
', PRIMARY KEY('
.
implode
(
', '
,
array_values
(
$options
[
'primary'
]))
.
')'
;
}
...
...
@@ -333,13 +339,17 @@ class Doctrine_Export_Mysql extends Doctrine_Export
$sequenceName
=
$this
->
conn
->
quoteIdentifier
(
$this
->
conn
->
getSequenceName
(
$sequenceName
),
true
);
$seqcolName
=
$this
->
conn
->
quoteIdentifier
(
$this
->
conn
->
getAttribute
(
Doctrine
::
ATTR_SEQCOL_NAME
),
true
);
$query
=
'CREATE TABLE '
.
$sequenceName
.
' ('
.
$seqcolName
.
' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
.
$seqcolName
.
'))'
.
strlen
(
$this
->
conn
->
default_table_type
)
?
' TYPE = '
.
$this
->
conn
->
default_table_type
:
''
;
$res
=
$this
->
conn
->
exec
(
$query
);
try
{
$query
=
'CREATE TABLE '
.
$sequenceName
.
' ('
.
$seqcolName
.
' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
.
$seqcolName
.
'))'
.
strlen
(
$this
->
conn
->
default_table_type
)
?
' TYPE = '
.
$this
->
conn
->
default_table_type
:
''
;
$res
=
$this
->
conn
->
exec
(
$query
);
}
catch
(
Doctrine_Connection_Exception
$e
)
{
throw
new
Doctrine_Export_Exception
(
'could not create sequence table'
);
}
if
(
$start
==
1
)
return
true
;
...
...
@@ -352,11 +362,11 @@ class Doctrine_Export_Mysql extends Doctrine_Export
// Handle error
try
{
$result
=
$this
->
conn
->
exec
(
'DROP TABLE '
.
$sequenceName
);
}
catch
(
Exception
$e
)
{
}
catch
(
Doctrine_Connection_
Exception
$e
)
{
throw
new
Doctrine_Export_Exception
(
'could not drop inconsistent sequence table'
);
}
throw
new
Doctrine_Export_Exception
(
'could not create sequence table'
);
}
/**
* Get the stucture of a field into an array
...
...
@@ -462,9 +472,12 @@ class Doctrine_Export_Mysql extends Doctrine_Export
}
}
if
(
!
isset
(
$definition
[
'fields'
])
||
!
is_array
(
$definition
[
'fields'
])
)
{
if
(
!
isset
(
$definition
[
'fields'
]))
{
throw
new
Doctrine_Export_Exception
(
'No index columns given.'
);
}
if
(
!
is_array
(
$definition
[
'fields'
]))
{
$definition
[
'fields'
]
=
array
(
$definition
[
'fields'
]);
}
$query
=
$type
.
'INDEX '
.
$name
;
...
...
lib/Doctrine/Lib.php
View file @
180d435e
...
...
@@ -54,7 +54,7 @@ class Doctrine_Lib
case
Doctrine_Record
::
STATE_TCLEAN
:
return
"transient clean"
;
break
;
}
;
}
}
/**
* returns a string representation of Doctrine_Record object
...
...
@@ -89,7 +89,7 @@ class Doctrine_Lib
case
Doctrine_Transaction
::
STATE_ACTIVE
:
return
"active"
;
break
;
}
;
}
}
/**
* returns a string representation of Doctrine_Connection object
...
...
@@ -98,28 +98,12 @@ class Doctrine_Lib
*/
public
static
function
getConnectionAsString
(
Doctrine_Connection
$connection
)
{
$r
[]
=
"<pre>"
;
$r
[]
=
"Doctrine_Connection object"
;
$r
[]
=
"State : "
.
Doctrine_Lib
::
getConnectionStateAsString
(
$connection
->
getTransaction
()
->
getState
());
$r
[]
=
"Open Transactions : "
.
$connection
->
getTransaction
()
->
getTransactionLevel
();
$r
[]
=
"Table in memory : "
.
$connection
->
count
();
$queries
=
false
;
if
(
$connection
->
getDBH
()
instanceof
Doctrine_Db
)
{
$handler
=
"Doctrine Database Handler"
;
$queries
=
count
(
$connection
->
getDBH
()
->
getQueries
());
$sum
=
array_sum
(
$connection
->
getDBH
()
->
getExecTimes
());
}
elseif
(
$connection
->
getDBH
()
instanceof
PDO
)
{
$handler
=
"PHP Native PDO Driver"
;
}
else
{
$handler
=
"Unknown Database Handler"
;
}
$r
[]
=
"DB Handler : "
.
$handler
;
if
(
$queries
)
{
$r
[]
=
"Executed Queries : "
.
$queries
;
$r
[]
=
"Sum of Exec Times : "
.
$sum
;
}
$r
[]
=
'<pre>'
;
$r
[]
=
'Doctrine_Connection object'
;
$r
[]
=
'State : '
.
Doctrine_Lib
::
getConnectionStateAsString
(
$connection
->
transaction
->
getState
());
$r
[]
=
'Open Transactions : '
.
$connection
->
transaction
->
getTransactionLevel
();
$r
[]
=
'Table in memory : '
.
$connection
->
count
();
$r
[]
=
'Driver name : '
.
$connection
->
getDbh
()
->
getAttribute
(
Doctrine
::
ATTR_DRIVER_NAME
);
$r
[]
=
"</pre>"
;
return
implode
(
"
\n
"
,
$r
)
.
"<br>"
;
...
...
lib/Doctrine/Manager.php
View file @
180d435e
...
...
@@ -231,7 +231,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
$this
->
connections
[
$name
]
=
new
Doctrine_Connection_Mock
(
$this
,
$adapter
);
break
;
default
:
throw
new
Doctrine_Manager_Exception
(
'Unknown connection driver '
.
$adapter
->
getAttribute
(
PDO
::
ATTR_DRIVER_NAME
));
throw
new
Doctrine_Manager_Exception
(
'Unknown connection driver '
.
$adapter
->
getAttribute
(
Doctrine
::
ATTR_DRIVER_NAME
));
};
if
(
$setCurrent
)
{
...
...
lib/Doctrine/Record.php
View file @
180d435e
...
...
@@ -775,7 +775,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if
(
!
isset
(
$this
->
references
[
$name
]))
{
$this
->
loadReference
(
$name
);
}
}
catch
(
Doctrine_Table_Exception
$e
)
{
}
catch
(
Doctrine_Table_Exception
$e
)
{
print
$e
;
throw
new
Doctrine_Record_Exception
(
"Unknown property / related component '
$name
'."
);
}
...
...
@@ -1525,6 +1526,17 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this
->
_table
->
setOption
(
$name
,
$value
);
}
}
/**
* index
* defines a foreignKey
*
* @param array $definition the definition array
* @return void
*/
public
function
foreignKey
(
array
$definition
=
array
())
{
return
$this
->
_table
->
addForeignKey
(
$definition
);
}
/**
* index
* defines or retrieves an index
...
...
@@ -1533,7 +1545,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
*
* @param string $name the name of the index
* @param array $definition the definition array
* @param array $options an array of options
* @return mixed
*/
public
function
index
(
$name
,
array
$definition
=
array
())
...
...
lib/Doctrine/Relation.php
View file @
180d435e
...
...
@@ -53,41 +53,66 @@ abstract class Doctrine_Relation
*/
const
MANY_COMPOSITE
=
3
;
const
ONE
=
0
;
const
MANY
=
1
;
protected
$definition
=
array
(
'alias'
=>
true
,
'foreign'
=>
true
,
'local'
=>
true
,
'table'
=>
true
,
);
/**
* @var Doctrine_Table $table foreign factory
*/
protected
$table
;
/**
* @var string $local local field
*/
protected
$local
;
/**
* @var string $foreign foreign field
*/
protected
$foreign
;
/**
* @var integer $type bind type
*/
protected
$type
;
/**
* @var string $alias relation alias
*/
protected
$alias
;
/**
* @param Doctrine_Table $table
* @param string $local
* @param string $foreign
* @param integer $type
* @param string $alias
* constructor
*
* @param array $definition an associative array with the following structure:
* name related class name
*
* local the local field(s)
*
* foreign the foreign reference field(s)
*
* table the foreign table object
*
* assocTable the association table object (if any)
*
* onDelete referential delete action
*
* onUpdate referential update action
*
* deferred deferred constraint checking
*
* alias relation alias
*
* type the relation type, either Doctrine_Relation::ONE or Doctrine_Relation::MANY
*
* The onDelete and onUpdate keys accept the following values:
*
* CASCADE: Delete or update the row from the parent table and automatically delete or
* update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
* Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
* in the parent table or in the child table.
*
* SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the
* child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier
* specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
*
* NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary
* key value is not allowed to proceed if there is a related foreign key value in the referenced table.
*
* RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
* omitting the ON DELETE or ON UPDATE clause.
*
* SET DEFAULT
*/
public
function
__construct
(
Doctrine_Table
$table
,
$local
,
$foreign
,
$type
,
$alias
)
public
function
__construct
(
array
$definition
)
{
$this
->
table
=
$table
;
$this
->
local
=
$local
;
$this
->
foreign
=
$foreign
;
$this
->
type
=
$type
;
$this
->
alias
=
$alias
;
foreach
(
array_keys
(
$this
->
definition
)
as
$key
)
{
if
(
!
isset
(
$definition
[
$key
]))
{
throw
new
Doctrine_Exception
(
$key
.
' is required!'
);
}
}
$this
->
definition
=
$definition
;
}
/**
* getAlias
...
...
@@ -97,7 +122,7 @@ abstract class Doctrine_Relation
*/
final
public
function
getAlias
()
{
return
$this
->
alias
;
return
$this
->
definition
[
'alias'
]
;
}
/**
* getType
...
...
@@ -108,7 +133,7 @@ abstract class Doctrine_Relation
*/
final
public
function
getType
()
{
return
$this
->
type
;
return
$this
->
definition
[
'type'
]
;
}
/**
* getTable
...
...
@@ -118,7 +143,7 @@ abstract class Doctrine_Relation
*/
final
public
function
getTable
()
{
return
$this
->
table
;
return
$this
->
definition
[
'table'
]
;
}
/**
* getLocal
...
...
@@ -128,7 +153,7 @@ abstract class Doctrine_Relation
*/
final
public
function
getLocal
()
{
return
$this
->
local
;
return
$this
->
definition
[
'local'
]
;
}
/**
* getForeign
...
...
@@ -139,7 +164,7 @@ abstract class Doctrine_Relation
*/
final
public
function
getForeign
()
{
return
$this
->
foreign
;
return
$this
->
definition
[
'foreign'
]
;
}
/**
* isComposite
...
...
@@ -149,8 +174,8 @@ abstract class Doctrine_Relation
*/
final
public
function
isComposite
()
{
return
(
$this
->
type
==
Doctrine_Relation
::
ONE_COMPOSITE
||
$this
->
type
==
Doctrine_Relation
::
MANY_COMPOSITE
);
return
(
$this
->
definition
[
'type'
]
==
Doctrine_Relation
::
ONE_COMPOSITE
||
$this
->
definition
[
'type'
]
==
Doctrine_Relation
::
MANY_COMPOSITE
);
}
/**
* isOneToOne
...
...
@@ -160,8 +185,8 @@ abstract class Doctrine_Relation
*/
final
public
function
isOneToOne
()
{
return
(
$this
->
type
==
Doctrine_Relation
::
ONE_AGGREGATE
||
$this
->
type
==
Doctrine_Relation
::
ONE_COMPOSITE
);
return
(
$this
->
definition
[
'type'
]
==
Doctrine_Relation
::
ONE_AGGREGATE
||
$this
->
definition
[
'type'
]
==
Doctrine_Relation
::
ONE_COMPOSITE
);
}
/**
* getRelationDql
...
...
@@ -171,8 +196,10 @@ abstract class Doctrine_Relation
*/
public
function
getRelationDql
(
$count
)
{
$dql
=
'FROM '
.
$this
->
table
->
getComponentName
()
.
' WHERE '
.
$this
->
table
->
getComponentName
()
.
'.'
.
$this
->
foreign
$component
=
$this
->
definition
[
'table'
]
->
getComponentName
();
$dql
=
'FROM '
.
$component
.
' WHERE '
.
$component
.
'.'
.
$this
->
definition
[
'foreign'
]
.
' IN ('
.
substr
(
str_repeat
(
'?, '
,
$count
),
0
,
-
2
)
.
')'
;
return
$dql
;
...
...
@@ -278,12 +305,12 @@ abstract class Doctrine_Relation
public
function
__toString
()
{
$r
[]
=
"<pre>"
;
$r
[]
=
"Class : "
.
get_class
(
$this
);
$r
[]
=
"Component : "
.
$this
->
table
->
getComponentName
();
$r
[]
=
"Table : "
.
$this
->
table
->
getTableName
()
;
$r
[]
=
"Local key : "
.
$this
->
local
;
$r
[]
=
"Foreign key : "
.
$this
->
foreign
;
$r
[]
=
"Type : "
.
$this
->
type
;
foreach
(
$this
->
definition
as
$k
=>
$v
)
{
if
(
is_object
(
$v
))
{
$v
=
'Object('
.
get_class
(
$v
)
.
')'
;
}
$r
[]
=
$k
.
' : '
.
$v
;
}
$r
[]
=
"</pre>"
;
return
implode
(
"
\n
"
,
$r
);
}
...
...
lib/Doctrine/Relation/Association.php
View file @
180d435e
...
...
@@ -35,30 +35,12 @@ Doctrine::autoload('Doctrine_Relation');
*/
class
Doctrine_Relation_Association
extends
Doctrine_Relation
{
/**
* @var Doctrine_Table $associationTable
*/
protected
$associationTable
;
/**
* the constructor
* @param Doctrine_Table $table foreign factory object
* @param Doctrine_Table $associationTable factory which handles the association
* @param string $local local field name
* @param string $foreign foreign field name
* @param integer $type type of relation
* @see Doctrine_Table constants
*/
public
function
__construct
(
Doctrine_Table
$table
,
Doctrine_Table
$associationTable
,
$local
,
$foreign
,
$type
,
$alias
)
{
parent
::
__construct
(
$table
,
$local
,
$foreign
,
$type
,
$alias
);
$this
->
associationTable
=
$associationTable
;
}
/**
* @return Doctrine_Table
*/
public
function
getAssociationFactory
()
{
return
$this
->
associationTable
;
return
$this
->
definition
[
'assocTable'
]
;
}
/**
* processDiff
...
...
@@ -106,23 +88,26 @@ class Doctrine_Relation_Association extends Doctrine_Relation
*/
public
function
getRelationDql
(
$count
,
$context
=
'record'
)
{
$component
=
$this
->
definition
[
'assocTable'
]
->
getComponentName
();
switch
(
$context
)
{
case
"record"
:
$sub
=
'SQL:SELECT '
.
$this
->
foreign
.
' FROM '
.
$this
->
associationTable
->
getTableName
()
.
' WHERE '
.
$this
->
local
.
$sub
=
'SQL:SELECT '
.
$this
->
definition
[
'foreign'
]
.
' FROM '
.
$this
->
definition
[
'assocTable'
]
->
getTableName
()
.
' WHERE '
.
$this
->
definition
[
'local'
]
.
' IN ('
.
substr
(
str_repeat
(
"?, "
,
$count
),
0
,
-
2
)
.
')'
;
$dql
=
"FROM "
.
$this
->
table
->
getComponentName
();
$dql
.=
"."
.
$this
->
associationTable
->
getComponentName
();
$dql
.=
" WHERE "
.
$this
->
table
->
getComponentName
()
.
"."
.
$this
->
table
->
getIdentifier
()
.
" IN (
$sub
)"
;
$dql
=
'FROM '
.
$this
->
definition
[
'table'
]
->
getComponentName
();
$dql
.=
'.'
.
$component
;
$dql
.=
' WHERE '
.
$this
->
definition
[
'table'
]
->
getComponentName
()
.
'.'
.
$this
->
definition
[
'table'
]
->
getIdentifier
()
.
' IN ('
.
$sub
.
')'
;
break
;
case
"collection"
:
$sub
=
substr
(
str_repeat
(
"?, "
,
$count
),
0
,
-
2
);
$dql
=
"FROM "
.
$this
->
associationTable
->
getComponentName
()
.
"."
.
$this
->
table
->
getComponentName
();
$dql
.=
" WHERE "
.
$this
->
associationTable
->
getComponentName
()
.
"."
.
$this
->
local
.
" IN (
$sub
)"
;
};
$dql
=
'FROM '
.
$component
.
'.'
.
$this
->
definition
[
'table'
]
->
getComponentName
();
$dql
.=
' WHERE '
.
$component
.
'.'
.
$this
->
definition
[
'local'
]
.
' IN ('
.
$sub
.
')'
;
break
;
}
return
$dql
;
}
...
...
@@ -138,7 +123,7 @@ class Doctrine_Relation_Association extends Doctrine_Relation
{
$id
=
$record
->
getIncremented
();
if
(
empty
(
$id
))
{
$coll
=
new
Doctrine_Collection
(
$this
->
table
);
$coll
=
new
Doctrine_Collection
(
$this
->
definition
[
'table'
]
);
}
else
{
$coll
=
Doctrine_Query
::
create
()
->
parseQuery
(
$this
->
getRelationDql
(
1
))
->
execute
(
array
(
$id
));
}
...
...
lib/Doctrine/Relation/Association/Self.php
View file @
180d435e
...
...
@@ -42,24 +42,31 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association
{
switch
(
$context
)
{
case
'record'
:
$sub
=
'SELECT '
.
$this
->
foreign
.
' FROM '
.
$this
->
associationTable
->
getTableName
()
.
' WHERE '
.
$this
->
local
.
' = ?'
;
$sub2
=
'SELECT '
.
$this
->
local
.
' FROM '
.
$this
->
associationTable
->
getTableName
()
.
' WHERE '
.
$this
->
foreign
.
' = ?'
;
$sub
=
'SELECT '
.
$this
->
definition
[
'foreign'
]
.
' FROM '
.
$this
->
definition
[
'assocTable'
]
->
getTableName
()
.
' WHERE '
.
$this
->
definition
[
'local'
]
.
' = ?'
;
$dql
=
'FROM '
.
$this
->
table
->
getComponentName
();
$dql
.=
'.'
.
$this
->
associationTable
->
getComponentName
();
$dql
.=
' WHERE '
.
$this
->
table
->
getComponentName
()
.
'.'
.
$this
->
table
->
getIdentifier
()
.
' IN ('
.
$sub
.
')'
;
$dql
.=
' || '
.
$this
->
table
->
getComponentName
()
.
'.'
.
$this
->
table
->
getIdentifier
()
.
' IN ('
.
$sub2
.
')'
;
$sub2
=
'SELECT '
.
$this
->
definition
[
'local'
]
.
' FROM '
.
$this
->
definition
[
'assocTable'
]
->
getTableName
()
.
' WHERE '
.
$this
->
definition
[
'foreign'
]
.
' = ?'
;
$dql
=
'FROM '
.
$this
->
definition
[
'table'
]
->
getComponentName
()
.
'.'
.
$this
->
definition
[
'assocTable'
]
->
getComponentName
()
.
' WHERE '
.
$this
->
definition
[
'table'
]
->
getComponentName
()
.
'.'
.
$this
->
definition
[
'table'
]
->
getIdentifier
()
.
' IN ('
.
$sub
.
')'
.
' || '
.
$this
->
definition
[
'table'
]
->
getComponentName
()
.
'.'
.
$this
->
definition
[
'table'
]
->
getIdentifier
()
.
' IN ('
.
$sub2
.
')'
;
break
;
case
'collection'
:
$sub
=
substr
(
str_repeat
(
'?, '
,
$count
),
0
,
-
2
);
$dql
=
'FROM '
.
$this
->
associationTable
->
getComponentName
()
.
'.'
.
$this
->
table
->
getComponentName
();
$dql
.=
' WHERE '
.
$this
->
associationTable
->
getComponentName
()
.
'.'
.
$this
->
local
.
' IN ('
.
$sub
.
')'
;
$dql
=
'FROM '
.
$this
->
definition
[
'assocTable'
]
->
getComponentName
()
.
'.'
.
$this
->
definition
[
'table'
]
->
getComponentName
()
.
' WHERE '
.
$this
->
definition
[
'assocTable'
]
->
getComponentName
()
.
'.'
.
$this
->
definition
[
'local'
]
.
' IN ('
.
$sub
.
')'
;
};
return
$dql
;
...
...
lib/Doctrine/Relation/ForeignKey.php
View file @
180d435e
...
...
@@ -37,6 +37,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
* processDiff
*
* @param Doctrine_Record $record
* @return void
*/
public
function
processDiff
(
Doctrine_Record
$record
)
{
...
...
@@ -75,29 +76,29 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
*/
public
function
fetchRelatedFor
(
Doctrine_Record
$record
)
{
$id
=
$record
->
get
(
$this
->
local
);
$id
=
$record
->
get
(
$this
->
definition
[
'local'
]
);
if
(
$this
->
isOneToOne
())
{
if
(
empty
(
$id
))
{
$related
=
$this
->
table
->
create
();
$related
=
$this
->
definition
[
'table'
]
->
create
();
}
else
{
$dql
=
'FROM '
.
$this
->
table
->
getComponentName
()
.
' WHERE '
.
$this
->
table
->
getComponentName
()
.
'.'
.
$this
->
foreign
.
' = ?'
;
$dql
=
'FROM '
.
$this
->
definition
[
'table'
]
->
getComponentName
()
.
' WHERE '
.
$this
->
definition
[
'table'
]
->
getComponentName
()
.
'.'
.
$this
->
definition
[
'foreign'
]
.
' = ?'
;
$coll
=
$this
->
table
->
getConnection
()
->
query
(
$dql
,
array
(
$id
));
$coll
=
$this
->
definition
[
'table'
]
->
getConnection
()
->
query
(
$dql
,
array
(
$id
));
$related
=
$coll
[
0
];
}
$related
->
set
(
$this
->
foreign
,
$record
,
false
);
$related
->
set
(
$this
->
definition
[
'foreign'
]
,
$record
,
false
);
}
else
{
if
(
empty
(
$id
))
{
$related
=
new
Doctrine_Collection
(
$this
->
table
);
$related
=
new
Doctrine_Collection
(
$this
->
definition
[
'table'
]
);
}
else
{
$query
=
$this
->
getRelationDql
(
1
);
$related
=
$this
->
table
->
getConnection
()
->
query
(
$query
,
array
(
$id
));
$related
=
$this
->
definition
[
'table'
]
->
getConnection
()
->
query
(
$query
,
array
(
$id
));
}
$related
->
setReference
(
$record
,
$this
);
}
...
...
lib/Doctrine/Relation/LocalKey.php
View file @
180d435e
...
...
@@ -58,17 +58,17 @@ class Doctrine_Relation_LocalKey extends Doctrine_Relation
*/
public
function
fetchRelatedFor
(
Doctrine_Record
$record
)
{
$id
=
$record
->
get
(
$this
->
local
);
$id
=
$record
->
get
(
$this
->
definition
[
'local'
]
);
if
(
empty
(
$id
))
{
$related
=
$this
->
table
->
create
();
$related
=
$this
->
definition
[
'table'
]
->
create
();
}
else
{
if
(
!
(
$related
=
$this
->
table
->
find
(
$id
)))
{
$related
=
$this
->
table
->
create
();
if
(
!
(
$related
=
$this
->
definition
[
'table'
]
->
find
(
$id
)))
{
$related
=
$this
->
definition
[
'table'
]
->
create
();
}
}
$record
->
set
(
$this
->
local
,
$related
,
false
);
$record
->
set
(
$this
->
definition
[
'local'
]
,
$related
,
false
);
return
$related
;
}
...
...
lib/Doctrine/Table.php
View file @
180d435e
This diff is collapsed.
Click to expand it.
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