如何在symfony2中访问实体的存储库方法?

编程入门 行业动态 更新时间:2024-10-12 08:25:20
本文介绍了如何在symfony2中访问实体的存储库方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了问题,请帮助我。这里是场景:

I am stuck with a problem please help me with it. Here is the scenarario:

我有一个实体 User和相应的存储库 UserRepository,在我的实体内部只有getter和setter方法。我已写入UserRepository的所有自定义查询。现在,在我的UserController中,我试图访问无法执行的存储库方法。 例如用户实体:

I have an entity "User" and corresponding repository "UserRepository", inside my entity there are only getter and setter methods. All custom queries I have written to UserRepository. Now inside my UserController I am trying to access repository methods which I am not able to do so. e.g. User entity:

class User { ... public function getId() { return $this->id; } public function setId($id) { return $this->id=$id; } public function setProperty($property) { $this->property = $property; } public function getProperty() { return $this->property; } .... } ?>

UserRepository:

UserRepository:

class UserRepository extends EntityRepository { public function findUsersListingById($id) { $queryBuilder = $this->getEntityManager()->createQueryBuilder(); $query = $em->createQuery( "SELECT U FROM UserEntityPathGoesHere WHERE U.id IN (".implode(",", $id).")" ); $users = $query->getResult(); return $users; } public function sayHelloWorld(){ echo ' Hello World'; } } ?>

UserController

UserController

class UserController { ... $users=$this->getDoctrine() ->getRepository('MyUserEntityPath') ->findUsersListingById($ids); //now I have multiple users I want to iterate through each user for associating additional data with each user foreach($users as $user) { $temp = array(); //I am able to access getId method which is defined in User entity $temp['id'] = $user->getId(); //however I am not able to access method from UserRepository, I tried something like below which gives me error call to undefined function sayHelloWorld $temp['status'] = $user->sayHelloWorld(); .... } }

....

如何访问实体的存储库方法?可能吗 ?如果不是,那么该解决方案有哪些替代方案?

How can I access repository methods for an entity? Is it possible ? If not then what are the alternatives for the solution?

推荐答案

一切皆有可能但是由于关注点分离,因此从实体本身访问实体的存储库。

Everything is possible however you should not access the entity's repository from the entity itself because of the separation of concerns.

请参阅此有关更多详细信息的Stackoverflow答案。

基本上,整个想法是,您希望按照以下方式组织应用程序。

Basically, the whole idea is that you want to have your application organized the following way.

简而言之:

它不应该朝另一个方向移动,否则会造成混乱。

It should not go in the other direction otherwise it creates a mess.

如果您想走得更远进入关注点分离,您可以执行以下操作。

If you want to go a bit further into the separation of concerns you could do the following.

替代解决方案:

  • Cre吃了访问服务(访问存储库)的 Twig扩展或
  • 在存储库中创建一个方法,在控制器中调用该方法,将数据映射到ID(数组的键为ID),将数组传递给模板,然后使用实体ID从数组中提取数据
  • 在您的存储库中创建一个方法,在您的控制器中调用该方法,将数据注入到您的实体中并通过您的实体中的实体访问数据模板。

可能还有其他人,但您会更好地了解应用程序的组织方式。

There are probably others but you would know better how your application is organized.

更多推荐

如何在symfony2中访问实体的存储库方法?

本文发布于:2023-11-13 22:56:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   方法   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!