【发布时间】: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