Created
August 23, 2012 05:49
-
-
Save albenik/3433157 to your computer and use it in GitHub Desktop.
Doctrine2 query filtering with association identities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Специально для слоупоков вроде меня | |
/** @Entity */ | |
class FooModel | |
{ | |
/** @Id @Column(type="integer") */ | |
private $id; | |
} | |
/** @Entity */ | |
class BarModel | |
{ | |
/** @Id @Column(type="integer") */ | |
private $id; | |
/** @OneToMany(targetEntity="FooModel", mappedBy="foo_id") | |
private $foo; | |
} | |
// Если мы хотим получить запрос select * from bar where bar.foo_id = 12345 | |
// делаем так, и никаких doctrine regular join, если мы фильтрует по foreign key: | |
$entityManager->select('b')->from('BarModel', 'b')->where('b.foo = 12345'); | |
// Если мы хотим сохранить BarModel с конкретным foo_id, без предварительно получения FooModel из базы | |
// делаем так: | |
$foo = $entityManager->getReference('FooModel', 12345); | |
$bar = new BarModel(); | |
$bar->setFoo($foo); | |
$entityManager->persist($bar); | |
$entityManager->flush(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment