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
4e70e5d8
Commit
4e70e5d8
authored
Jul 02, 2009
by
piccoloprincipe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] added one-many self referential association test (addresses #2276)
parent
31892fb4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
155 additions
and
3 deletions
+155
-3
ECommerceCategory.php
tests/Doctrine/Tests/Models/ECommerce/ECommerceCategory.php
+56
-3
OneToManySelfReferentialAssociationTest.php
...RM/Functional/OneToManySelfReferentialAssociationTest.php
+98
-0
OrmFunctionalTestCase.php
tests/Doctrine/Tests/OrmFunctionalTestCase.php
+1
-0
No files found.
tests/Doctrine/Tests/Models/ECommerce/ECommerceCategory.php
View file @
4e70e5d8
...
...
@@ -17,21 +17,33 @@ class ECommerceCategory
* @Id
* @GeneratedValue(strategy="AUTO")
*/
p
ublic
$id
;
p
rivate
$id
;
/**
* @Column(type="string", length=50)
*/
p
ublic
$name
;
p
rivate
$name
;
/**
* @ManyToMany(targetEntity="ECommerceProduct", mappedBy="categories")
*/
public
$products
;
private
$products
;
/**
* @OneToMany(targetEntity="ECommerceCategory", mappedBy="parent", cascade={"save"})
*/
private
$children
;
/**
* @ManyToOne(targetEntity="ECommerceCategory")
* @JoinColumn(name="parent_id", referencedColumnName="id")
*/
private
$parent
;
public
function
__construct
()
{
$this
->
products
=
new
\Doctrine\Common\Collections\Collection
();
$this
->
children
=
new
\Doctrine\Common\Collections\Collection
();
}
public
function
getId
()
...
...
@@ -69,4 +81,45 @@ class ECommerceCategory
{
return
$this
->
products
;
}
private
function
setParent
(
ECommerceCategory
$parent
)
{
$this
->
parent
=
$parent
;
}
public
function
getChildren
()
{
return
$this
->
children
;
}
public
function
getParent
()
{
return
$this
->
parent
;
}
public
function
addChild
(
ECommerceCategory
$child
)
{
$this
->
children
[]
=
$child
;
$child
->
setParent
(
$this
);
}
/** does not set the owning side. */
public
function
brokenAddChild
(
ECommerceCategory
$child
)
{
$this
->
children
[]
=
$child
;
}
public
function
removeChild
(
ECommerceCategory
$child
)
{
$removed
=
$this
->
children
->
removeElement
(
$child
);
if
(
$removed
!==
null
)
{
$removed
->
removeParent
();
}
}
private
function
removeParent
()
{
$this
->
parent
=
null
;
}
}
tests/Doctrine/Tests/ORM/Functional/OneToManySelfReferentialAssociationTest.php
0 → 100644
View file @
4e70e5d8
<?php
namespace
Doctrine\Tests\ORM\Functional
;
use
Doctrine\Tests\Models\ECommerce\ECommerceCategory
;
require_once
__DIR__
.
'/../../TestInit.php'
;
/**
* Tests a bidirectional one-to-one association mapping (without inheritance).
*/
class
OneToManySelfReferentialAssociationTest
extends
\Doctrine\Tests\OrmFunctionalTestCase
{
private
$parent
;
private
$firstChild
;
private
$secondChild
;
protected
function
setUp
()
{
$this
->
useModelSet
(
'ecommerce'
);
parent
::
setUp
();
$this
->
parent
=
new
ECommerceCategory
();
$this
->
parent
->
setName
(
'Programming languages books'
);
$this
->
firstChild
=
new
ECommerceCategory
();
$this
->
firstChild
->
setName
(
'Java books'
);
$this
->
secondChild
=
new
ECommerceCategory
();
$this
->
secondChild
->
setName
(
'Php books'
);
}
public
function
testSavesAOneToManyAssociationWithCascadeSaveSet
()
{
$this
->
parent
->
addChild
(
$this
->
firstChild
);
$this
->
parent
->
addChild
(
$this
->
secondChild
);
$this
->
_em
->
save
(
$this
->
parent
);
$this
->
assertForeignKeyIs
(
$this
->
parent
->
getId
(),
$this
->
firstChild
);
$this
->
assertForeignKeyIs
(
$this
->
parent
->
getId
(),
$this
->
secondChild
);
}
public
function
testSavesAnEmptyCollection
()
{
$this
->
_em
->
save
(
$this
->
parent
);
$this
->
assertEquals
(
0
,
count
(
$this
->
parent
->
getChildren
()));
}
public
function
testDoesNotSaveAnInverseSideSet
()
{
$this
->
parent
->
brokenAddChild
(
$this
->
firstChild
);
$this
->
_em
->
save
(
$this
->
parent
);
$this
->
assertForeignKeyIs
(
null
,
$this
->
firstChild
);
}
public
function
testRemovesOneToManyAssociation
()
{
$this
->
parent
->
addChild
(
$this
->
firstChild
);
$this
->
parent
->
addChild
(
$this
->
secondChild
);
$this
->
_em
->
save
(
$this
->
parent
);
$this
->
parent
->
removeChild
(
$this
->
firstChild
);
$this
->
_em
->
flush
();
$this
->
assertForeignKeyIs
(
null
,
$this
->
firstChild
);
$this
->
assertForeignKeyIs
(
$this
->
parent
->
getId
(),
$this
->
secondChild
);
}
public
function
testEagerLoadsOneToManyAssociation
()
{
$this
->
parent
->
addChild
(
$this
->
firstChild
);
$this
->
parent
->
addChild
(
$this
->
secondChild
);
$this
->
_em
->
save
(
$this
->
parent
);
$this
->
_em
->
flush
();
$this
->
_em
->
clear
();
$query
=
$this
->
_em
->
createQuery
(
'select c1, c2 from Doctrine\Tests\Models\ECommerce\ECommerceCategory c1 join c1.children c2'
);
$result
=
$query
->
getResultList
();
$this
->
assertEquals
(
1
,
count
(
$result
));
$parent
=
$result
[
0
];
$children
=
$parent
->
getChildren
();
$this
->
assertTrue
(
$children
[
0
]
instanceof
ECommerceCategory
);
$this
->
assertSame
(
$parent
,
$children
[
0
]
->
getParent
());
$this
->
assertTrue
(
strstr
(
$children
[
0
]
->
getName
(),
' books'
));
$this
->
assertTrue
(
$children
[
1
]
instanceof
ECommerceCategory
);
$this
->
assertSame
(
$parent
,
$children
[
1
]
->
getParent
());
$this
->
assertTrue
(
strstr
(
$children
[
1
]
->
getName
(),
' books'
));
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
public
function
assertForeignKeyIs
(
$value
,
ECommerceCategory
$child
)
{
$foreignKey
=
$this
->
_em
->
getConnection
()
->
execute
(
'SELECT parent_id FROM ecommerce_categories WHERE id=?'
,
array
(
$child
->
getId
()))
->
fetchColumn
();
$this
->
assertEquals
(
$value
,
$foreignKey
);
}
}
tests/Doctrine/Tests/OrmFunctionalTestCase.php
View file @
4e70e5d8
...
...
@@ -80,6 +80,7 @@ class OrmFunctionalTestCase extends OrmTestCase
$conn
->
exec
(
'DELETE FROM ecommerce_carts'
);
$conn
->
exec
(
'DELETE FROM ecommerce_customers'
);
$conn
->
exec
(
'DELETE FROM ecommerce_products'
);
$conn
->
exec
(
'DELETE FROM ecommerce_carts_products'
);
$conn
->
exec
(
'DELETE FROM ecommerce_shippings'
);
$conn
->
exec
(
'DELETE FROM ecommerce_features'
);
$conn
->
exec
(
'DELETE FROM ecommerce_categories'
);
...
...
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