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
0b9ea73a
Commit
0b9ea73a
authored
May 20, 2007
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
--no commit message
--no commit message
parent
d79e4d0b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
55 deletions
+73
-55
Parser.php
lib/Doctrine/Relation/Parser.php
+49
-55
ParserTestCase.php
tests/Relation/ParserTestCase.php
+24
-0
No files found.
lib/Doctrine/Relation/Parser.php
View file @
0b9ea73a
...
@@ -127,8 +127,52 @@ class Doctrine_Relation_Parser
...
@@ -127,8 +127,52 @@ class Doctrine_Relation_Parser
if
(
isset
(
$this
->
_pending
[
$name
]))
{
if
(
isset
(
$this
->
_pending
[
$name
]))
{
$def
=
$this
->
_pending
[
$name
];
$def
=
$this
->
_pending
[
$name
];
if
(
isset
(
$def
[
'refClass'
]))
{
$def
=
$this
->
completeAssocDefinition
(
$def
);
}
else
{
$def
=
$this
->
completeDefinition
(
$def
);
}
}
}
public
function
completeAssocDefinition
(
$def
)
{
$conn
=
$this
->
_table
->
getConnection
();
$def
[
'table'
]
=
$conn
->
getTable
(
$def
[
'class'
]);
$def
[
'definer'
]
=
$conn
->
getTable
(
$def
[
'definer'
]);
$def
[
'refTable'
]
=
$conn
->
getTable
(
$def
[
'refClass'
]);
if
(
!
isset
(
$def
[
'foreign'
]))
{
// foreign key not set
// try to guess the foreign key
$columns
=
$this
->
getIdentifiers
(
$def
[
'table'
]);
$def
[
'foreign'
]
=
$columns
;
}
if
(
!
isset
(
$def
[
'local'
]))
{
// local key not set
// try to guess the local key
$columns
=
$this
->
getIdentifiers
(
$this
->
_table
);
$def
[
'local'
]
=
$columns
;
}
return
$def
;
}
public
function
getIdentifiers
(
Doctrine_Table
$table
)
{
if
(
is_array
(
$table
->
getIdentifier
()))
{
$columns
=
array
();
foreach
((
array
)
$table
->
getIdentifier
()
as
$identifier
)
{
$columns
[]
=
strtolower
(
$table
->
getComponentName
())
.
'_'
.
$table
->
getIdentifier
();
}
}
else
{
$columns
=
strtolower
(
$table
->
getComponentName
())
.
'_'
.
$table
->
getIdentifier
();
}
}
return
$columns
;
}
}
public
function
completeDefinition
(
$def
)
public
function
completeDefinition
(
$def
)
{
{
...
@@ -142,14 +186,9 @@ class Doctrine_Relation_Parser
...
@@ -142,14 +186,9 @@ class Doctrine_Relation_Parser
// try to guess the foreign key
// try to guess the foreign key
if
(
$def
[
'local'
]
===
$def
[
'definer'
]
->
getIdentifier
())
{
if
(
$def
[
'local'
]
===
$def
[
'definer'
]
->
getIdentifier
())
{
$column
=
strtolower
(
$def
[
'definer'
]
->
getComponentName
())
$columns
=
$this
->
getIdentifiers
(
$def
[
'definer'
]);
.
'_'
.
$def
[
'definer'
]
->
getIdentifier
();
if
(
!
$def
[
'table'
]
->
hasColumn
(
$column
))
{
$def
[
'foreign'
]
=
$columns
;
// auto-add column
}
$def
[
'foreign'
]
=
$column
;
}
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
...
@@ -161,10 +200,9 @@ class Doctrine_Relation_Parser
...
@@ -161,10 +200,9 @@ class Doctrine_Relation_Parser
// 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
[
'definer'
]
->
getIdentifier
())
{
$column
=
strtolower
(
$def
[
'table'
]
->
getComponentName
())
$columns
=
$this
->
getIdentifiers
(
$def
[
'table'
]);
.
'_'
.
$def
[
'table'
]
->
getIdentifier
();
$def
[
'local'
]
=
$column
;
$def
[
'local'
]
=
$column
s
;
}
else
{
}
else
{
$def
[
'local'
]
=
$def
[
'definer'
]
->
getIdentifier
();
$def
[
'local'
]
=
$def
[
'definer'
]
->
getIdentifier
();
}
}
...
@@ -225,48 +263,6 @@ class Doctrine_Relation_Parser
...
@@ -225,48 +263,6 @@ class Doctrine_Relation_Parser
$definition
[
'table'
]
=
$this
->
conn
->
getTable
(
$definition
[
'class'
],
$allowExport
);
$definition
[
'table'
]
=
$this
->
conn
->
getTable
(
$definition
[
'class'
],
$allowExport
);
$definition
[
'constraint'
]
=
false
;
$definition
[
'constraint'
]
=
false
;
if
(
$component
==
$this
->
options
[
'name'
]
||
in_array
(
$component
,
$this
->
options
[
'parents'
]))
{
// ONE-TO-ONE
if
(
$definition
[
'type'
]
==
Doctrine_Relation
::
ONE_COMPOSITE
||
$definition
[
'type'
]
==
Doctrine_Relation
::
ONE_AGGREGATE
)
{
// tree structure parent relation found
if
(
!
isset
(
$definition
[
'local'
]))
{
$definition
[
'local'
]
=
$definition
[
'foreign'
];
$definition
[
'foreign'
]
=
$definition
[
'table'
]
->
getIdentifier
();
}
$relation
=
new
Doctrine_Relation_LocalKey
(
$definition
);
}
else
{
// tree structure children relation found
if
(
!
isset
(
$definition
[
'local'
]))
{
$tmp
=
$definition
[
'table'
]
->
getIdentifier
();
$definition
[
'local'
]
=
$tmp
;
}
//$definition['foreign'] = $tmp;
$definition
[
'constraint'
]
=
true
;
$relation
=
new
Doctrine_Relation_ForeignKey
(
$definition
);
}
}
elseif
(
$component
==
$definition
[
'class'
]
||
(
$component
==
$definition
[
'alias'
]))
{
// && ($name == $this->options['name'] || in_array($name,$this->parents))
if
(
!
isset
(
$defintion
[
'local'
]))
{
$definition
[
'local'
]
=
$this
->
identifier
;
}
$definition
[
'constraint'
]
=
true
;
// ONE-TO-MANY or ONE-TO-ONE
$relation
=
new
Doctrine_Relation_ForeignKey
(
$definition
);
}
else
{
// MANY-TO-MANY
// MANY-TO-MANY
// only aggregate relations allowed
// only aggregate relations allowed
...
@@ -343,8 +339,6 @@ class Doctrine_Relation_Parser
...
@@ -343,8 +339,6 @@ class Doctrine_Relation_Parser
$relation
=
new
Doctrine_Relation_Association
(
$definition
);
$relation
=
new
Doctrine_Relation_Association
(
$definition
);
}
}
}
}
}
$this
->
relations
[
$name
]
=
$relation
;
$this
->
relations
[
$name
]
=
$relation
;
return
$this
->
relations
[
$name
];
return
$this
->
relations
[
$name
];
...
...
tests/Relation/ParserTestCase.php
View file @
0b9ea73a
...
@@ -130,4 +130,28 @@ class Doctrine_Relation_Parser_TestCase extends Doctrine_UnitTestCase
...
@@ -130,4 +130,28 @@ class Doctrine_Relation_Parser_TestCase extends Doctrine_UnitTestCase
$this
->
assertEqual
(
$d
[
'foreign'
],
'id'
);
$this
->
assertEqual
(
$d
[
'foreign'
],
'id'
);
$this
->
assertEqual
(
$d
[
'local'
],
'email_id'
);
$this
->
assertEqual
(
$d
[
'local'
],
'email_id'
);
}
}
public
function
testRelationParserSupportsForeignColumnGuessingForAssociations
()
{
$r
=
new
Doctrine_Relation_Parser
(
$this
->
conn
->
getTable
(
'User'
));
$d
=
$r
->
completeAssocDefinition
(
array
(
'class'
=>
'Group'
,
'type'
=>
Doctrine_Relation
::
MANY
,
'local'
=>
'user_id'
,
'refClass'
=>
'GroupUser'
,
'definer'
=>
'User'
));
$this
->
assertEqual
(
$d
[
'foreign'
],
'group_id'
);
}
public
function
testRelationParserSupportsLocalColumnGuessingForAssociations
()
{
$r
=
new
Doctrine_Relation_Parser
(
$this
->
conn
->
getTable
(
'User'
));
$d
=
$r
->
completeAssocDefinition
(
array
(
'class'
=>
'Group'
,
'type'
=>
Doctrine_Relation
::
MANY
,
'foreign'
=>
'group_id'
,
'refClass'
=>
'GroupUser'
,
'definer'
=>
'User'
));
$this
->
assertEqual
(
$d
[
'local'
],
'user_id'
);
}
}
}
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