duanfu2562 2013-06-17 08:51
浏览 25
已采纳

PHP模式 - 如何检索对象的集合

Situation:

There is simple User class which is Doctrine entity (I skipped comments to keep the code short):

class User {
  protected $iUserId;
  protected $sName;
}

Question:
How to retrieve collection of objects of class User from the controller?

Follow up: Until now, we were creating methods like getUsers() in User class which gets data from DB, creates User class objects and returns it.

Now, I'm wondering if it's not a better solution to create class like UserCollection which would handle data retrieving and creating User objects? Maybe I should make use of \Doctrine\Common\Collections\Collection class somehow?

What I'd like to accomplish is easy way to handles i.e. where clauses. UserCollection class could have QueryBuilder object on which I could operate from controller like this:

$oUserCollection = new UserCollection();
$oUserCollection->setWhere( 'u.iUserId = 1' );
$aUsers = oUserCollection->getUsers();
...

Please share your thoughts on that topic.

Update: Doctrine provides a concept of repositories of entities which I think maybe the solution I'm looking for.

  • 写回答

1条回答 默认 最新

  • dss67853 2013-06-19 09:53
    关注

    You have two options:

    If your criterias are simple, eg. you just want to filter on a single property, or you don't need filtering at all, you can use the findBy() and findAll() methods, on the EntityRepository:

    // find all users
    $repository = $em->getRepository('My\\User');
    $users = $repository->findAll();
    
    // find users who are marked as active
    $users = $repository->findBy(array('active' => true));
    
    // sort users by age
    $users = $repository->findBy(array(), array('age' => 'DESC'));
    

    If you have complex(er) requirements, or your finders will be used from multiple places, you will have to create a custom EntityRepository, and group your finder logic there. You have to specify in the mapping that you want to create your own EntityRepository for this entity, the method for this varies depending on what mapping driver you use (annotation, yaml, xml). For example, in yaml you would have to place this line in your mapping:

    RepositoryClass: My\Repository\UserRepository
    

    And then create the file:

    namespace My\Repository;
    use Doctrine\ORM\EntityRepository;
    
    class UserRepository extends EntityRepository {
    
        public function findVIPUsers() {
            $query = $this->_em->createQuery("Your query here");
            return $query->getResult();
    
            // you can also use $this->findBy(), or $this->findAll() here
        }
    }
    

    And then in your controller, when you call $em->getRepository('My\User') it will return the custom repository you just created.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用