【问题标题】:Using a repository function in service Symofony在服务 Symfony 中使用存储库功能
【发布时间】:2020-01-23 15:46:17
【问题描述】:

我在这样的树枝中使用服务

{{ count_service.getCount(term.getId) }}

我希望服务使用存储库功能,存储库功能

<?php

namespace AppBundle\Repository;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping;

class SynonymRepository extends EntityRepository
{

    public function getCount($termId)
    {
        $qbSynonymType = $this->getEntityManager()->createQueryBuilder();
        $synonymTypes = $qbSynonymType->select('synonymType.id, synonymType.type')
            ->from('AppBundle:SynonymType', 'synonymType')
            ->getQuery()->getResult();

        $qb = $this->getEntityManager()->createQueryBuilder();
        $count = [];

        $qb->select('count(synonym.synonymId)')
            ->from('AppBundle:Synonym','synonym');

        foreach($synonymTypes as $type) {
            $count[$type['type']] = $qb
                ->where('synonym.term = :termId')
                ->andWhere('synonym.synonymType = :type')
                ->setParameter('termId', $termId)
                ->setParameter('type', $type['id'])
                ->getQuery()->getSingleScalarResult();
        }

        $qbTerm = $this->getEntityManager()->createQueryBuilder()->from('AppBundle:Term', 'term');


        $count['parent'] = "NaN";
        $count['children'] = "NaN";

        return $count;
    }
 }

我的 service.yml 看起来像这样

synonymrepository:
    class: Doctrine\ORM\EntityRepository
    factory: ["@doctrine.orm.entity_manager", getRepository]
    arguments:
        - AppBundle\Entity\SynonymType

term_count:
    class: AppBundle\Services\TermCount
    arguments:
        - "@synonymrepository"

最后我的服务看起来像这样

<?php

namespace AppBundle\Services;

use AppBundle\Repository\SynonymRepository;

class TermCount
{
    private $repository;

    public function __construct()
    {
        $this->repository = new SynonymRepository();
    }

    public function getCount($termId)
    {
        return $this->repository->getCount($termId);
    }
}

运行时出现以下错误

Type error: Too few arguments to function Doctrine\ORM\EntityRepository::__construct(), 0 passed in /var/www/html/src/AppBundle/Services/TermCount.php on line 15 and exactly 2 expected

我认为这是因为使用 EntityRepository 扩展 SynonymRepository 需要 EntityManagerInterface $em 和 Mapping\ClassMetadata $class。但我不确定如何将它们传递给 EntityRepository。

我正在使用这个answer 把我带到这里,迷失在如何实际实现 finall 位。 感谢您的帮助。

更新

实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Table(name="synonym")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\SynonymRepository")
 */
class Synonym
{
    /**
     * @var int
     * @ORM\Id()
     * @ORM\Column(name="synonym_id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $synonymId;

    /**
     * @var Term
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Term", inversedBy="synonyms")
     */
    protected $term;

    /**
     * @var SynonymType[]
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\SynonymType", inversedBy="synonyms")
     */
    protected $synonymType;

    /**
     * @var int
     * @ORM\Column(name="language_id", type="integer")
     */
    protected $languageId;

    /**
     * @var string
     * @ORM\Column(name="synonym", type="string", length=255)
     */
    protected $synonym;

    public function __construct()
    {
       // $this->synonymType = new ArrayCollection();
    }

    /**
     * @return int
     */
    public function getSynonymId(): int
    {
        return $this->synonymId;
    }

    /**
     * @return Term
     */
    public function getTerm(): Term
    {
        return $this->term;
    }

    /**
     * @param int $termId
     * @return Term
     */
    public function setTerm(int $termId): Term
    {
        $this->term = $termId;
        return $this->term;
    }

    /**
     * @return SynonymType[]
     */
    public function getSynonymType()
    {
        return $this->synonymType;
    }

    /**
     * @param SynonymType $synonymType
     * @return SynonymType
     */
    public function setSynonymType(SynonymType $synonymType): SynonymType
    {
        $this->synonymType = $synonymType;
        return $this->synonymType;
    }

    /**
     * @return int
     */
    public function getLanguageId(): int
    {
        return $this->languageId;
    }

    /**
     * @param int $languageId
     * @return Synonym
     */
    public function setLanguageId(int $languageId): Synonym
    {
        $this->languageId = $languageId;
        return $this;
    }

    /**
     * @return string
     */
    public function getSynonym(): string
    {
        return $this->synonym;
    }

    /**
     * @param string $synonym
     * @return Synonym
     */
    public function setSynonym(string $synonym): Synonym
    {
        $this->synonym = $synonym;
        return $this;
    }

}

【问题讨论】:

  • 下面的答案为您指明了正确的方向,但我很好奇您使用的是什么 Symfony 版本?如果是 Symfony 2 就可以了。但是,如果您稍后使用任何东西,那么您可以为自己节省大量工作。
  • Symfony 3.4。我在听
  • 首先阅读有关autowire 奇迹的文档。使用 autowire,您可以大大减小 services.yml 文件的大小。然后切换到 Doctrine 部分,看看如何从 ServiceEntityRepository 类而不是 EntityRepository 扩展。请务必在阅读文档时始终选择 3.4 版本。

标签: php symfony doctrine-orm doctrine


【解决方案1】:

你需要在你的构造中使用 DI(依赖注入)来使用新的原因,因为我看到你的 SynonymRepository 依赖于其他服务是错误的

<?php

namespace AppBundle\Services;

use AppBundle\Repository\SynonymRepository;

class TermCount
{
    private $repository;

    public function __construct(SynonymRepository $synonymRepository)
    {
        $this->repository = $synonymRepository;
    }

    public function getCount($termId)
    {
        return $this->repository->getCount($termId);
    }
}

【讨论】:

  • 这给出了以下错误:类型错误:传递给 AppBundle\Services\TermCount::__construct() 的参数 1 必须是 AppBundle\Repository\SynonymRepository 的实例,Doctrine\ORM\EntityRepository 的实例, 在第 2661 行调用 /var/www/html/var/cache/dev/ContainerDhvlegw/appDevDebugProjectContainer.php
  • 发现这个问题与我的一模一样,但该解决方案适用于我@Youssef Saoubou 有什么想法吗? stackoverflow.com/questions/38542747/…
  • 验证您的 Synonym 实体是否正确映射到您的 SynonymRepository
  • @VladimirSabo 你还在为此烦恼吗?确保在 AppBundler/Resources/doctrine 下没有任何旧的学说映射文件。它们会干扰您的映射。您还可以使用 $repo = $entityManager->getRepository(Synonym::class) 进行调试,以验证您正在获取自定义仓库。
  • 在 service.yml 中找到了我的问题,我为存储库使用了错误的参数
猜你喜欢
  • 1970-01-01
  • 2018-08-15
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2012-08-26
  • 1970-01-01
相关资源
最近更新 更多