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
5227d51e
Commit
5227d51e
authored
May 21, 2007
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
--no commit message
--no commit message
parent
17349244
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
24 deletions
+59
-24
ForeignKey.php
lib/Doctrine/Relation/ForeignKey.php
+21
-4
Parser.php
lib/Doctrine/Relation/Parser.php
+38
-20
No files found.
lib/Doctrine/Relation/ForeignKey.php
View file @
5227d51e
...
@@ -43,15 +43,16 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
...
@@ -43,15 +43,16 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
*/
*/
public
function
fetchRelatedFor
(
Doctrine_Record
$record
)
public
function
fetchRelatedFor
(
Doctrine_Record
$record
)
{
{
$id
=
$record
->
get
(
$this
->
definition
[
'local'
]);
$id
=
array
();
foreach
((
array
)
$this
->
definition
[
'local'
]
as
$local
)
{
$id
=
$record
->
get
(
$local
);
}
if
(
$this
->
isOneToOne
())
{
if
(
$this
->
isOneToOne
())
{
if
(
empty
(
$id
))
{
if
(
empty
(
$id
))
{
$related
=
$this
->
getTable
()
->
create
();
$related
=
$this
->
getTable
()
->
create
();
}
else
{
}
else
{
$dql
=
'FROM '
.
$this
->
getTable
()
->
getComponentName
()
$dql
=
'FROM '
.
$this
->
getTable
()
->
getComponentName
()
.
' WHERE '
.
$this
->
getTable
()
->
getComponentName
()
.
' WHERE '
.
$this
->
getCondition
();
.
'.'
.
$this
->
definition
[
'foreign'
]
.
' = ?'
;
$coll
=
$this
->
getTable
()
->
getConnection
()
->
query
(
$dql
,
array
(
$id
));
$coll
=
$this
->
getTable
()
->
getConnection
()
->
query
(
$dql
,
array
(
$id
));
$related
=
$coll
[
0
];
$related
=
$coll
[
0
];
...
@@ -71,4 +72,20 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
...
@@ -71,4 +72,20 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
}
}
return
$related
;
return
$related
;
}
}
/**
* getCondition
*
* @param string $alias
*/
public
function
getCondition
(
$alias
=
null
)
{
if
(
!
$alias
)
{
$alias
=
$this
->
getTable
()
->
getComponentName
();
}
$conditions
=
array
();
foreach
((
array
)
$this
->
definition
[
'foreign'
]
as
$foreign
)
{
$conditions
[]
=
$alias
.
'.'
.
$foreign
.
' = ?'
;
}
return
implode
(
' AND '
,
$conditions
);
}
}
}
lib/Doctrine/Relation/Parser.php
View file @
5227d51e
...
@@ -106,11 +106,6 @@ class Doctrine_Relation_Parser
...
@@ -106,11 +106,6 @@ class Doctrine_Relation_Parser
}
else
{
}
else
{
$alias
=
$name
;
$alias
=
$name
;
}
}
if
(
!
isset
(
$options
[
'definer'
]))
{
throw
new
Doctrine_Relation_Exception
(
'Relation definer not set.'
);
}
if
(
!
isset
(
$options
[
'type'
]))
{
if
(
!
isset
(
$options
[
'type'
]))
{
throw
new
Doctrine_Relation_Exception
(
'Relation type not set.'
);
throw
new
Doctrine_Relation_Exception
(
'Relation type not set.'
);
}
}
...
@@ -189,6 +184,32 @@ class Doctrine_Relation_Parser
...
@@ -189,6 +184,32 @@ class Doctrine_Relation_Parser
return
$columns
;
return
$columns
;
}
}
public
function
guessColumns
(
$classes
,
Doctrine_Table
$foreignTable
)
{
$conn
=
$this
->
_table
->
getConnection
();
foreach
(
$classes
as
$class
)
{
$table
=
$conn
->
getTable
(
$class
);
$columns
=
$this
->
getIdentifiers
(
$table
);
$found
=
true
;
foreach
((
array
)
$columns
as
$column
)
{
if
(
!
$foreignTable
->
hasColumn
(
$column
))
{
$found
=
false
;
break
;
}
}
if
(
$found
)
{
break
;
}
}
if
(
!
$found
)
{
throw
new
Doctrine_Relation_Exception
(
"Couldn't find columns."
);
}
return
$columns
;
}
/**
/**
* Completes the given definition
* Completes the given definition
*
*
...
@@ -199,17 +220,16 @@ class Doctrine_Relation_Parser
...
@@ -199,17 +220,16 @@ class Doctrine_Relation_Parser
{
{
$conn
=
$this
->
_table
->
getConnection
();
$conn
=
$this
->
_table
->
getConnection
();
$def
[
'table'
]
=
$conn
->
getTable
(
$def
[
'class'
]);
$def
[
'table'
]
=
$conn
->
getTable
(
$def
[
'class'
]);
$def
[
'definer'
]
=
$conn
->
getTable
(
$def
[
'definer'
]);
$foreignClasses
=
array_merge
(
$def
[
'table'
]
->
getOption
(
'parents'
),
array
(
$def
[
'class'
]));
$localClasses
=
array_merge
(
$this
->
_table
->
getOption
(
'parents'
),
array
(
$this
->
_table
->
getComponentName
()));
if
(
isset
(
$def
[
'local'
]))
{
if
(
isset
(
$def
[
'local'
]))
{
if
(
!
isset
(
$def
[
'foreign'
]))
{
if
(
!
isset
(
$def
[
'foreign'
]))
{
// local key is set, but foreign key is not
// local key is set, but foreign key is not
// try to guess the foreign key
// try to guess the foreign key
if
(
$def
[
'local'
]
===
$def
[
'definer'
]
->
getIdentifier
())
{
if
(
$def
[
'local'
]
===
$this
->
_table
->
getIdentifier
())
{
$columns
=
$this
->
getIdentifiers
(
$def
[
'definer'
]);
$def
[
'foreign'
]
=
$this
->
guessColumns
(
$localClasses
,
$def
[
'table'
]);
$def
[
'foreign'
]
=
$columns
;
}
else
{
}
else
{
// the foreign field is likely to be the
// the foreign field is likely to be the
// identifier of the foreign class
// identifier of the foreign class
...
@@ -220,29 +240,27 @@ class Doctrine_Relation_Parser
...
@@ -220,29 +240,27 @@ class Doctrine_Relation_Parser
if
(
isset
(
$def
[
'foreign'
]))
{
if
(
isset
(
$def
[
'foreign'
]))
{
// local key not set, but foreign key is set
// local key not set, but foreign key is set
// try to guess the local key
// try to guess the local key
if
(
$def
[
'foreign'
]
===
$def
[
'definer'
]
->
getIdentifier
())
{
if
(
$def
[
'foreign'
]
===
$def
[
'table'
]
->
getIdentifier
())
{
$columns
=
$this
->
getIdentifiers
(
$def
[
'table'
]);
$def
[
'local'
]
=
$this
->
guessColumns
(
$foreignClasses
,
$this
->
_table
);
$def
[
'local'
]
=
$columns
;
}
else
{
}
else
{
$def
[
'local'
]
=
$
def
[
'definer'
]
->
getIdentifier
();
$def
[
'local'
]
=
$
this
->
_table
->
getIdentifier
();
}
}
}
else
{
}
else
{
// neither local or foreign key is being set
// neither local or foreign key is being set
// try to guess both keys
// try to guess both keys
$column
=
strtolower
(
$
def
[
'definer'
]
->
getComponentName
())
$column
=
strtolower
(
$
this
->
_table
->
getComponentName
())
.
'_'
.
$
def
[
'definer'
]
->
getIdentifier
();
.
'_'
.
$
this
->
_table
->
getIdentifier
();
if
(
$def
[
'table'
]
->
hasColumn
(
$column
))
{
if
(
$def
[
'table'
]
->
hasColumn
(
$column
))
{
$def
[
'foreign'
]
=
$column
;
$def
[
'foreign'
]
=
$column
;
$def
[
'local'
]
=
$
def
[
'definer'
]
->
getIdentifier
();
$def
[
'local'
]
=
$
this
->
_table
->
getIdentifier
();
}
else
{
}
else
{
$column
=
strtolower
(
$def
[
'table'
]
->
getComponentName
())
$column
=
strtolower
(
$def
[
'table'
]
->
getComponentName
())
.
'_'
.
$def
[
'table'
]
->
getIdentifier
();
.
'_'
.
$def
[
'table'
]
->
getIdentifier
();
if
(
$
def
[
'definer'
]
->
hasColumn
(
$column
))
{
if
(
$
this
->
_table
->
hasColumn
(
$column
))
{
$def
[
'foreign'
]
=
$def
[
'table'
]
->
getIdentifier
();
$def
[
'foreign'
]
=
$def
[
'table'
]
->
getIdentifier
();
$def
[
'local'
]
=
$column
;
$def
[
'local'
]
=
$column
;
}
}
...
...
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