Dependency injection 传递给[some service]::\u construct()的symfony 3参数1必须是给定的条令\ORM\EntityRepository的[something]实例的实例
我正在尝试创建一个服务:Dependency injection 传递给[some service]::\u construct()的symfony 3参数1必须是给定的条令\ORM\EntityRepository的[something]实例的实例,dependency-injection,symfony,Dependency Injection,Symfony,我正在尝试创建一个服务: <?php namespace AppBundle\Service; use AppBundle\Repository\GroupEntityRepositoryInterface; use AppBundle\Repository\GroupUserRepository; use AppBundle\Repository\GroupUserRepositoryInterface; use AppBundle\Entity\Group; use AppBund
<?php
namespace AppBundle\Service;
use AppBundle\Repository\GroupEntityRepositoryInterface;
use AppBundle\Repository\GroupUserRepository;
use AppBundle\Repository\GroupUserRepositoryInterface;
use AppBundle\Entity\Group;
use AppBundle\Repository\GroupRepository;
class GroupUserService
{
private $groupRepository;
private $groupUserRepository;
/**
* @param GroupRepositoryInterface $groupRepository
* @param GroupUserRepositoryInterface $groupUserRepository
*/
public function __construct(GroupRepository $groupRepository /*, GroupUserRepositoryInterface $groupUserRepository*/)
{
$this->groupRepository = $groupRepository;
// $this->groupUserRepository = $groupUserRepository;
}
public function deleteGroup(Group $group)
{
$this->groupRepository->delete($group);
}
public function createGroup(Group $group)
{
$this->groupRepository->add($group);
}
}
我得到一个错误:
可捕获的致命错误:传递给AppBundle\Service\GroupUserService::\uu construct()的参数1必须是AppBundle\Repository\GroupRepository的实例,给定的Doctrine\ORM\EntityRepository的实例,在第1654行的E:\other\dropbox\dropbox\programavimas\kodo pavyzdziai\htdocs\users\u admin\u demo\var\cache\dev\appDevDebugProjectContainer.php中调用并定义
你能解释一下,如果我通过了GroupRepository,为什么它会成为一个完整的存储库吗?如何修复它?我试着搜索类似的问题,但几个小时后我没有看到结果
更新:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\GroupRepository")
* Group
*/
class Group
{
/**
* @var string
*/
private $name;
/**
* @var integer
*/
private $id;
/**
* Set name
*
* @param string $name
*
* @return Group
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
使用定义AppBundle\Repository\GroupRepository
作为AppBundle\Entity\Group
的响应
@ORM\Entity(repositoryClass="AppBundle\Repository\GroupRepository")
XML
我应该将此代码放在哪里?我在这里发现它位于实体类名之上。但一切都没有改变。已清除缓存。已更新问题。也许这应该用xml编写,而不是用注释,因为我使用xml进行配置?当然,如果您使用xml映射,您应该在xml文件中定义它。请看更新后的答案只要看一下您链接的文档,您可以在那里选择xml映射类型:)让我们看看。
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AppBundle\Entity\Group" table="`group`">
<unique-constraints>
<unique-constraint name="UNIQ_6DC044C55E237E06" columns="name"/>
</unique-constraints>
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="name" type="string" column="name" length="255" nullable="false">
<options>
<option name="fixed"/>
</options>
</field>
</entity>
</doctrine-mapping>
@ORM\Entity(repositoryClass="AppBundle\Repository\GroupRepository")
<entity
name="AppBundle\Entity\Group"
repository-class="AppBundle\Repository\GroupRepository">
</entity>