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.

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

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码