Skip to content

Instantly share code, notes, and snippets.

@albenik
Created August 23, 2012 05:49
Show Gist options
  • Save albenik/3433157 to your computer and use it in GitHub Desktop.
Save albenik/3433157 to your computer and use it in GitHub Desktop.
Doctrine2 query filtering with association identities
//Специально для слоупоков вроде меня
/** @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