如何在symfony2中访问实体的存储库方法? [英] How to access repository methods for an entity in symfony2?

查看:46
本文介绍了如何在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中访问实体的存储库方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆