如何在服务内部使用主义方法(Symfony 4)?

时间:2019-03-05 09:45:32

标签: php symfony doctrine-orm

我在Symfony中创建了自己的第一项服务:

// src/Service/PagesGenerator.php 

namespace App\Service;

class PagesGenerator
{
    public function getPages()
    {

      $page = $this->getDoctrine()->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];
    }
}

但是我收到错误消息:

  

试图调用名为class的未定义方法“ getDoctrine”   “ App \ Service \ PagesGenerator”。

然后我尝试添加我的services.yaml:

PagesGenerator:
    class: %PagesGenerator.class%
    arguments:
      - "@doctrine.orm.entity_manager"

但是随后我收到错误消息:

  

文件“ /Users/work/project/config/services.yaml”不包含   /Users/work/project/config/services.yaml中的有效YAML(   加载到资源“ /Users/work/project/config/services.yaml”)。

3 个答案:

答案 0 :(得分:3)

因此,在评论中我说的是让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];
    }
}

答案 1 :(得分:1)

确保对YAML使用“空格”适当的缩进。

  

YAML文件使用空格作为缩进,您可以将2或4个空格用于   缩进,但没有制表符   read more about this

symfony 3.3之前

例如,我们在sms_manager中提供服务AppBundle/FrontEndBundle/Services

services:
    AppBundle.sms_manager:
        class: AppBundle\FrontEndBundle\Services\SmsManager
        arguments: [ '@service_container' ,'@doctrine.orm.entity_manager' ]

然后您的服务可以在构造函数中接收参数

<?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 3.3或更高版本,

Is there a way to inject EntityManager into a service

use Doctrine\ORM\EntityManagerInterface

class PagesGenerator
{
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    // ...
}

答案 2 :(得分:1)

借助Symfony 4和新的自动布线功能,您可以轻松注入一定数量的课程

要找出可用于自动装配的类/接口,请使用以下命令:

bin/console   debug:autowiring 

我们将使用这个:

  

Doctrine \ ORM \ EntityManagerInterface   (doctrine.orm.default_entity_manager)

因此,让它添加到getPages函数之前

/**
 * @var EntityManagerInterface
 */
private $em;

public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}

然后您可以像这样使用它:

  $page = $this->em->getRepository(Pages::class)->findOneBy(['slug'=>$slug]);

希望有帮助!

相关问题