回答
可以使用findBy
方法,它是繼承和存在於所有存儲庫。
例子:
$criteria = array('name' => 'someValue', 'status' => 'enabled');
$result = $em->getRepository('SomeEntity')->findBy($criteria);
您可以在倉庫的一個使用這樣的定義創建findByExample
方法:
class MyRepository extends Doctrine\ORM\EntityRepository {
public function findByExample(MyEntity $entity) {
return $this->findBy($entity->toArray());
}
}
爲了這個工作,你必須創建自己的實體的基類,實現了toArray
方法。
MyEntity
也可以是一個接口,您的具體實體將不得不再次實現toArray
方法。
要使您的所有存儲庫都可用,請確保您正在擴展基本存儲庫類 - 在本例中爲MyRepository
。
P.S我假設你正在談論主義2.X
是的。
比方說,你有一個名爲用戶的模型。您有以下兩類
abstract class Base_User extends Doctrine_Record
{
//define table, columns, etc
}
class User extends Base_User
{
}
在一些其他的對象,你可以做
$user = new User;
//This will return a Doctrine Collection of all users with first name = Travis
$user->getTable()->findByFirstName("Travis");
//The above code is actually an alias for this function call
$user->getTable()->findBy("first_name", "Travis");
//This will return a Doctrine Record for the user with id = 24
$user->getTable()->find(24);
//This will return a Doctrine Collection for all users with name=Raphael and
//type = developer
$user->getTable()
->findByDql("User.name= ? AND User.type = ?", array("Raphael", "developer"));
謝謝回答特拉維斯,但它不是什麼即時尋找。我想要做這樣的事情: $ user = new User; $ user-> name =「rafael」; $ user-> category =「developer」; $ q = Doctrine :: getTable('User') - > findByExample($ user); 和$ q應返回「rafael」作爲名稱和「developer」作爲類別的用戶集合。你知道如何做到這一點? – rizidoro 2010-01-13 18:56:29
檢查使用findByDql方法的最後一段代碼。這應該讓你找到你想要的。 – Travis 2010-01-14 02:52:46
$users = $userTable->findByIsAdminAndIsModeratorOrIsSuperAdmin(true, true, true);
見http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en
- 1. symfony2學說findBy id arrayCollection
- 2. Symfony的學說findBy然後映射
- 3. 學說findBy *方法和讀取數組
- 4. Symfony2的學說傳遞參數外鍵在findBy功能
- 5. 使用PHPDriver的學說映射示例
- 6. 使用派生物對象的學說findBy/getBy魔術方法?
- 7. 學說2.0 findBy * - 當列包含不工作強調
- 8. 學說2 findby功能,得到鍵和值
- 9. 等效findBy和fetchOne從學說中行走
- 10. Symfony2的學說例外
- 11. 學說:有沒有在findBy中使用關聯實體的方法
- 12. 學說 - 不在
- 13. Camunda示例說明
- 14. 學說
- 15. 學說加載模塊例外
- 16. Symfony2的學說onetoone完整的例子
- 17. 學說映射未找到案例
- 18. 在PHP或學說
- 19. 學說2.1 - 在DateTimeType
- 20. 學說:在更新
- 21. 當學說生成學說/基地/ *。class.php
- 22. Symfony 1.4,學說(學說:: HYDRATE_ARRAY非複數)
- 23. dev.android.com的JetBoy示例說明?
- 24. 學說2.1其中外鍵id = ?,編輯:固定在學說2.2
- 25. Symfony2的學說
- 26. 使學說
- 27. 學說意義
- 28. 與學說
- 29. 學說+ ZF + phpunit
- 30. 學說2 @joincolumns
我不是在談論Doctrine2(我在2010年末提出問題),但現在,它解決了這個問題。 – rizidoro 2012-05-07 18:23:17