我正在尝试为我的 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
}
Controller (我尝试在另一个包中使用自定义存储库的地方):
// 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;
哪个返回:
Doctrine\ORM\EntityRepository
所以我想我的自定义存储库根本没有被调用。但我找不到原因。我尝试了几种操作(经过一些挖掘):
- 清除缓存(
app/console cache:clear
) - 清除 Doctrine 缓存(
app/console Doctrine :cache:clear-metadata
) - 检查 config.yml(在 doctrine:orm 下):
auto_mapping: true
- 检查 config.yml(在 doctrine:dbal 下):
type: annotation
- 我没有任何 config/doctrine 文件夹(这正常吗?)
没有任何效果。仍然有同样的错误。看起来框架完全忽略了实体类中的注释。有什么想法/建议吗?
感谢您的帮助!
最佳答案
你试过吗:
// src/ManageBundle/Controller/ManageController
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('AppBundle:Experimentation');
$experimentations = $repository->getExperimentationByUser($id);
并生成实体
php app/console doctrine:generate:entities Experimentation
关于php - Symfony:无法启用自定义存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34676617/