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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化