Rah*_*hul 12 entity repository symfony
我遇到了问题请帮帮我.这是scenarario:
我有一个实体"User"和相应的存储库"UserRepository",在我的实体里面只有getter和setter方法.我写给UserRepository的所有自定义查询.现在在我的UserController里面,我试图访问我无法访问的存储库方法.例如用户实体:
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;
}
....
}
?>
Run Code Online (Sandbox Code Playgroud)
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';
}
}
?>
Run Code Online (Sandbox Code Playgroud)
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();
....
}
}
Run Code Online (Sandbox Code Playgroud)
....
如何访问实体的存储库方法?可能吗 ?如果没有,那么该解决方案的替代方案是什么?
Tho*_*ire 22
一切皆有可能,但是由于关注点的分离,您不应该从实体本身访问实体的存储库.
有关详细信息,请参阅此Stackoverflow答案.
基本上,整个想法是您希望以下列方式组织您的应用程序.
简而言之:
它不应该朝另一个方向走,否则会造成混乱.
如果您想进一步分析关注点,可以执行以下操作.
替代方案:
可能有其他人,但你会更清楚你的应用程序是如何组织的.
| 归档时间: |
|
| 查看次数: |
37999 次 |
| 最近记录: |