我正在构建一个非常依赖第三方API的站点,所以我认为将API包装器打包为服务是有意义的,但是我开始找到访问它的实例.在控制器之外,例如在实体存储库中.与此相关的是能够访问控制器之外的配置值(例如在实体存储库中)也是有用的.
任何人都可以告诉我,如果这是可能的,如果没有,是否有建议的方法来做这种事情?
谢谢你的帮助
我是symfony2的新手.当我通过命令行创建实体类时,我创建了一个存储库类.但是我无法访问该存储库类中的自定义函数.如何在symfony2中创建自定义存储库类?有人可以从头开始用一些示例代码给我一步一步的解释吗?
下面是我的存储库类
namespace Mypro\symBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* RegisterRepository
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class RegisterRepository extends EntityRepository
{
public function findAllOrderedByName()
{
return $this->getEntityManager()
->createQuery('SELECT p FROM symBundle:Register p ORDER BY p.name ASC')
->getResult();
}
}
Run Code Online (Sandbox Code Playgroud)
我这样打电话给我的控制器
$em = $this->getDoctrine()->getEntityManager();
$pro = $em->getRepository('symBundle:Register')
->findAllOrderedByName();
Run Code Online (Sandbox Code Playgroud)
我得到了以下错误
Undefined method 'findAllOrderedByName'. The method name must start with either findBy or findOneBy!
Run Code Online (Sandbox Code Playgroud)
我的代码中有任何错误吗?创建存储库类有什么错误吗?我需要使用任何课程吗?
我想在symfony 2 webapplication的每个页面上显示新的通知.我被建议使用Twig Extension.我在该扩展中创建了一个函数getFriendRequests,但我不知道在twig扩展中通过我的自定义存储库获取数据是否是一个好习惯:现在它给了我错误,它找不到getDoctrine方法.
<?php
namespace Tennisconnect\DashboardBundle\Extension;
class NotificationTwigExtension extends \Twig_Extension
{
public function getFriendRequests($user)
{
$users = $this->getDoctrine()
->getRepository('TennisconnectUserBundle:User')
->getFriendRequests();
return count($users);
}
public function getName()
{
return 'notification';
}
public function getFunctions()
{
return array(
'getFriendRequests' => new \Twig_Function_Method($this, 'getFriendRequests'));
}
}
Run Code Online (Sandbox Code Playgroud)