我可以在 Doctrine 2 实体方法中包含便捷查询吗?

发布于 2024-11-27 23:58:43 字数 880 浏览 1 评论 0原文

我想在我的 Doctrine 2 实体中包含一些附加函数,以包含我必须经常运行的代码。例如:

用户 - 有很多帖子
帖子 - 有一个用户

我已经有一个函数 $user->getPosts(),但这会返回我的所有帖子。我想写一个 $user->getActivePosts() ,如下所示:

$user->getPosts()->where('active = true') //如果可能的话
或:
$em->getRepository('Posts')->findBy(array('user'=>$user,'active'=>true)) //如果这样更方便

据我所知,没有办法通过实体本身返回实体管理器,所以我唯一的选择是

class User {
   function getActivePosts() {
     $all_posts = $this->getPosts();
     $active_posts = new ArrayCollection();
     foreach ($all_posts as $post) {
        if ($post->getActive()) {
           $active_posts->add($post);
        }
     }
     return $active_posts;
}

但是,这要求我将所有帖子加载到我的实体管理器中,而我实际上只想要一小部分其中,以及它要求我在 PHP 中进行过滤,而在 SQL 层中这样做会更合适。有什么方法可以完成我想要在实体内部执行的操作,或者我是否必须在实体外部创建代码?

I'd like to include some additional functions in my Doctrine 2 entities to contain code that I'm going to have to run quite frequently. For example:

User - has many Posts
Post - has a single user

I already have a function $user->getPosts(), but this returns all of my posts. I'm looking to write a $user->getActivePosts(), which would be like:

$user->getPosts()->where('active = true') //if this were possible
or:
$em->getRepository('Posts')->findBy(array('user'=>$user,'active'=>true)) //if this were more convenient

As far as I can tell, there's no way to get back to the entity manager though the Entity itself, so my only option would be

class User {
   function getActivePosts() {
     $all_posts = $this->getPosts();
     $active_posts = new ArrayCollection();
     foreach ($all_posts as $post) {
        if ($post->getActive()) {
           $active_posts->add($post);
        }
     }
     return $active_posts;
}

However, this requires me to load ALL posts into my entity manager, when I really only want a small subset of them, and it requires me to do filtering in PHP, when it would be much more appropriate to do so in the SQL layer. Is there any way to accomplish what I'm looking to do inside the Entity, or do I have to create code outside of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2024-12-04 23:58:43

我认为您应该在 PostRepository 而不是实体模型上实现该方法。

我尝试将所有与模型相关的逻辑保留在“特定于域”方法后面的存储库中。这样,如果您更改表示帖子是否处于活动状态的方式,您只需更改单个方法的实现,而不必查找分散在您的帖子中的所有 active = true 语句。应用程序或在“不相关”实体模型中进行更改。

像这样的东西

PostRepository extends EntityRepository {
  public function findActiveByUser($user){
     // whatever it takes to get the active posts
  }
}

I think you should implement the method on the PostRepository rather than on the entity model.

I try to keep all model related logic in the repositories behind "domain specific" methods. That way if you change the way you represent whether a post is active or not, you only have to change the implementation of a single method instead of having to find all the active = true statements scattered around in your application or making changes in an "unrelated" entity model.

Something like this

PostRepository extends EntityRepository {
  public function findActiveByUser($user){
     // whatever it takes to get the active posts
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文