Php 如何在我的服务中使用条令方法(Symfony 4)?
我在Symfony创建了自己的第一个服务:Php 如何在我的服务中使用条令方法(Symfony 4)?,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我在Symfony创建了自己的第一个服务: // src/Service/PagesGenerator.php 但我得到了错误信息: 试图调用类的名为getDoctrine的未定义方法 应用程序\服务\页面生成器 然后我尝试添加我的服务。yaml: PagesGenerator: class: %PagesGenerator.class% arguments: - "@doctrine.orm.entity_manager" 但是我得到了错误信息: 文件/Us
// src/Service/PagesGenerator.php
但我得到了错误信息:
试图调用类的名为getDoctrine的未定义方法
应用程序\服务\页面生成器
然后我尝试添加我的服务。yaml:
PagesGenerator:
class: %PagesGenerator.class%
arguments:
- "@doctrine.orm.entity_manager"
但是我得到了错误信息:
文件/Users/work/project/config/services.yaml不包含
/Users/work/project/config/services.YAML中的有效YAML
加载到resource/Users/work/project/config/services.yaml中
确保对YAML使用正确的缩进,并使用空格 YAML文件使用空格作为缩进,可以使用2或4个空格作为缩进 缩进,但没有标签 在symfony 3.3之前 例如,我们在AppBundle/FrontEndBundle/Services中有服务sms_管理器 然后,您的服务可以在构造函数中接收您的参数
<?php
namespace AppBundle\FrontEndBundle\Services;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class SmsManager {
private $container;
private $DM;
public function __construct( Container $container, \Doctrine\ORM\EntityManager $DM )
{
$this->container = $container;
$this->DM = $DM;
}
/**
* @return \Doctrine\ORM\EntityManager
*/
public function getDoctrine() {
return $this->DM;
}
}
所以,在我的评论中,我是说让Symfony做他的工作和自动布线EntityManager更好。这是你应该做的。另外,您能告诉我们您使用的是什么Symfony版本吗?如果启用了自动布线,请检查services.yaml以了解这一点
<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
class PagesGenerator
{
public function __construct(EntityManagerInterface $em) {
$this->em = $em;
}
public function getPages()
{
$page = $this->em->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
$messages = [
'You did it! You updated the system! Amazing!',
'That was one of the coolest updates I\'ve seen all day!',
'Great work! Keep going!',
];
$index = array_rand($messages);
return $messages[$index];
}
}
有了Symfony 4和新的autowiring,您可以轻松地注入一定数量的类 要了解可用于自动布线的类/接口,请使用以下命令:
bin/console debug:autowiring
我们将使用这个:
条令\ORM\entitymanager接口
条令.orm.default\u实体\u经理
让我们在getPages函数之前添加这个
/**
* @var EntityManagerInterface
*/
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
然后你可以这样使用它:
$page = $this->em->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);
希望有帮助 Symfony正在使用一种叫做依赖注入的神奇模式。不要在services.yaml中为您的服务提供任何参数,而是在您的服务中调用u ConstructentityManager接口$em,让Symfony注入您的实体管理器。我认为这更清楚了。@GasKa非常感谢你。这听起来真不错。你能举个例子吗。我试图将这一行放到我的服务中,但我得到了错误语法错误,意外的“$em”,我认为您使用了$em而不是$this->em。此外,您必须在构建之前为您的服务声明此属性。正如我在上一个问题中所说的,我的答案是:阅读文档。使用自动布线是Symfony最佳实践的一部分。但不阅读文档?再次强调:使用自动连接。我知道不再建议您注入容器。但建议从symfony4.x开始。无论如何,避免this@GasKa他没有提到这个版本。没错,我假设他有Symfony 3.x或Symfony 4.x,一切都如我所说。我看到你编辑了你的文章。好的让我们先看看他的回答我用的是Symfony 4谢谢!工作正常听起来不错,但如果我需要打电话给另一位经理以获取我的存储库,会发生什么?由于EntityManagerInterface没有getManager属性,我只能从“默认”调用存储库。如果我试图从服务中执行$this->getDoctrine,我会得到一个错误::对成员函数的调用为空
bin/console debug:autowiring
/**
* @var EntityManagerInterface
*/
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
$page = $this->em->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);