Symfony:无法启用自定义存储库
Symfony: can't enable custom repository
我正在尝试为我的 symfony 2.8 网站使用自定义存储库:
自定义存储库:
// src/AppBundle/Entity/ExperimentationRepository
namespace AppBundle\Entity;
use Doctrine\ORM\EntityRepository;
class ExperimentationRepository extends EntityRepository
{
public function getExperimentationByUser($id){
// do stuff and return results
}
}
实体:
// src/AppBundle/Entity/Experimentation
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Experimentation
*
* @ORM\Table(name="experimentation")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ExperimentationRepository")
*/
class Experimentation{
// sutff
}
控制器(我尝试在另一个包中使用自定义存储库):
// src/ManageBundle/Controller/ManageController
$em = $this->getDoctrine()->getManager();
$experimentations = $em->getRepository('AppBundle:Experimentation')->getExperimentationByUser($id);
我收到以下错误:
Undefined method 'getExperimentationByUser'.
The method name must start with either findBy or findOneBy!
经过一些研究,我尝试检查我的自定义存储库是否被调用:
$repository=$this->getDoctrine()
->getRepository('AppBundle:Experimentation');
$repositoryClass=get_class($repository);
echo $repositoryClass;
exit;
哪个returns:
Doctrine\ORM\EntityRepository
所以我想我的自定义存储库根本没有被调用。但我找不到原因。我已经尝试了几个动作(经过一些挖掘):
- 清除缓存 (
app/console cache:clear
)
- 清除主义缓存(
app/console doctrine:cache:clear-metadata
)
- 检查config.yml(在doctrine:orm下):
auto_mapping: true
- 检查config.yml(在doctrine:dbal下):
type: annotation
- 我没有任何 config/doctrine 文件夹(这正常吗?)
没有任何效果。仍然有同样的错误。看起来框架完全忽略了实体 class 中的注释。任何 idea/suggestion?
感谢您的帮助!
你试过吗:
// src/ManageBundle/Controller/ManageController
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AppBundle:Experimentation');
$experimentations = $repository->getExperimentationByUser($id);
并生成实体
php app/console doctrine:generate:entities Experimentation
我终于成功了。执行几个命令后,我成功创建了 AppBundle/Resources/config/doctrine file
。它充满了 .orm.xml
个文件,所以我只是从文档中获取了 XML 语法:
<entity
name="AppBundle\Entity\Experimentation"
repository-class="AppBundle\Entity\ExperimentationRepository">
但是,我仍然不明白为什么注释系统不起作用,因为我已经很努力地启用它了。是否至少有一种方法可以检查启用了哪个系统(XML、YAML 或注释)?如果您有任何线索,请随时发表评论...
您刚刚删除了缓存文件夹。使用 CLI 命令清除缓存对我不起作用。 php app/console cache:clear
我正在尝试为我的 symfony 2.8 网站使用自定义存储库:
自定义存储库:
// src/AppBundle/Entity/ExperimentationRepository
namespace AppBundle\Entity;
use Doctrine\ORM\EntityRepository;
class ExperimentationRepository extends EntityRepository
{
public function getExperimentationByUser($id){
// do stuff and return results
}
}
实体:
// src/AppBundle/Entity/Experimentation
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Experimentation
*
* @ORM\Table(name="experimentation")
* @ORM\Entity(repositoryClass="AppBundle\Entity\ExperimentationRepository")
*/
class Experimentation{
// sutff
}
控制器(我尝试在另一个包中使用自定义存储库):
// src/ManageBundle/Controller/ManageController
$em = $this->getDoctrine()->getManager();
$experimentations = $em->getRepository('AppBundle:Experimentation')->getExperimentationByUser($id);
我收到以下错误:
Undefined method 'getExperimentationByUser'. The method name must start with either findBy or findOneBy!
经过一些研究,我尝试检查我的自定义存储库是否被调用:
$repository=$this->getDoctrine()
->getRepository('AppBundle:Experimentation');
$repositoryClass=get_class($repository);
echo $repositoryClass;
exit;
哪个returns:
Doctrine\ORM\EntityRepository
所以我想我的自定义存储库根本没有被调用。但我找不到原因。我已经尝试了几个动作(经过一些挖掘):
- 清除缓存 (
app/console cache:clear
) - 清除主义缓存(
app/console doctrine:cache:clear-metadata
) - 检查config.yml(在doctrine:orm下):
auto_mapping: true
- 检查config.yml(在doctrine:dbal下):
type: annotation
- 我没有任何 config/doctrine 文件夹(这正常吗?)
没有任何效果。仍然有同样的错误。看起来框架完全忽略了实体 class 中的注释。任何 idea/suggestion?
感谢您的帮助!
你试过吗:
// src/ManageBundle/Controller/ManageController
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AppBundle:Experimentation');
$experimentations = $repository->getExperimentationByUser($id);
并生成实体
php app/console doctrine:generate:entities Experimentation
我终于成功了。执行几个命令后,我成功创建了 AppBundle/Resources/config/doctrine file
。它充满了 .orm.xml
个文件,所以我只是从文档中获取了 XML 语法:
<entity
name="AppBundle\Entity\Experimentation"
repository-class="AppBundle\Entity\ExperimentationRepository">
但是,我仍然不明白为什么注释系统不起作用,因为我已经很努力地启用它了。是否至少有一种方法可以检查启用了哪个系统(XML、YAML 或注释)?如果您有任何线索,请随时发表评论...
您刚刚删除了缓存文件夹。使用 CLI 命令清除缓存对我不起作用。 php app/console cache:clear