【发布时间】:2016-05-31 23:51:29
【问题描述】:
我的 Symfony 项目中有一个自定义存储库,我想像搜索工具一样使用它。我的项目结构如下:
P.D.更新的问题和代码
-Manager:
* BaseManager.php
* MyEntityManager.php
-Repository:
* BaseRepository.php
* MyEntityRepository.php
好吧,我想访问我的自定义存储库并使用以下方法findByTitle,该方法应该返回一个包含描述字段相似的对象的数组。我在我的函数中放了一个简单的打印(输入的术语的 var_dump)来查看我的浏览器是否显示它,但它还没有显示...
我的基地经理:
<?php
namespace AppBundle\Manager;
use AppBundle\Repository\BaseRepository;
class BaseManager
{
/**
* @var BaseRepository
*/
protected $repo;
/**
* @param BaseRepository $repo
*/
public function __construct(BaseRepository $repo)
{
$this->repo = $repo;
}
/**
* @param $model
* @return bool
*/
public function create($model)
{
return $this->repo->create($model);
}
/**
* @param CrudModel $model
* @return bool
*/
public function update($model)
{
return $this->repo->save($model);
}
/**
* @param CrudModel $model
* @return bool
*/
public function delete($model)
{
return $this->repo->delete($model);
}
/**
* @param $id
* @return null|object
*/
public function getOneById($id)
{
return $this->repo->findOneById($id);
}
/**
* @return array
*/
public function all()
{
return $this->repo->all();
}
}
我的实体管理器:
<?php
namespace AppBundle\Manager;
use AppBundle\Repository\MyEntityRepository;
use AppBundle\Entity\MyEntity;
/**
* Class MyEntityManager
* @package AppBundle\Manager
*/
class MyEntityManager extends BaseManager{
public function findByTitle($title){
echo '<h1>flux of code here</h1>';
return $this->repo->findByTitle($title);
}
public function findSimilars($term){
echo '<h1>flux of code here</h1>';
return $this->repo->findSimilars($term);
}
}
基础存储库:
<?php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
abstract class BaseRepository extends EntityRepository
{
public function create($model, $autoFlush = true)
{
return $this->insert($model,$autoFlush);
}
public function save($model, $autoFlush = true)
{
return $this->insert($model,$autoFlush);
}
public function delete($model)
{
try{
$this->getEntityManager()->remove($model);
$this->getEntityManager()->flush();
return true;
}catch (\Exception $e){
echo $e->getMessage();
}
}
public function findOneById($id)
{
return $this->findOneBy(array('id' => $id));
}
public function all()
{
return $this->findAll();
}
private function insert($model, $autoFlush = true)
{
$this->getEntityManager()->persist($model);
if ($autoFlush) {
$this->getEntityManager()->flush();
return true;
}
}
}
MyEntityRepository:
<?php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* Class MyEntityRepository
* @package AppBundle\Repository
*/
class MyEntityRepository extends BaseRepository{
private function findById($id){
$query = $this->createQueryBuilder('myentity')
->where('myentity.id = :id')
->setParameter('id', $id)
->getQuery();
$myentity = $query->getResult();
return $myentity;
}
private function findByTitle($term){
echo '<h1>';
var_dump($term);
echo '</h1>',
$query = $this->createQueryBuilder('myentity')
->andwhere('myentity.description = :description')
->setParameter('description', $term)
->getQuery();
$myentity = $query->getResult();
return $myentity;
}
}
MyEntity 的开头:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="myentity")
* @ORM\Entity
* @ORM\Entity(repositoryClass="AppBundle\Repository\MyEntityRepository")
*/
class MyEntity {
......
我的 services.yml:
parameters:
app.myentity.repository.class: AppBundle\Repository\MyEntityRepository
app.myentity.manager.class: AppBundle\Manager\MyEntityManager
services:
app.myentity.repository:
class: %app.myentity.repository.class%
public: true
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments: [ AppBundle\Entity\MyEntity ]
app.myentity.manager:
class: %app.myentity.manager.class%
arguments: [@app.myentity.repository]
我通过以下方式调用我的服务:
public function searchAction(Request $request, $term){
$manager = $this->get('app.myentity.manager');
$result = $manager->findByTitle($term);
echo '<h5>';
var_dump($result);
echo '</h5>';
....
}
【问题讨论】:
-
无法理解您的句子,但您可以检查以确保您的实体管理器正确映射到您的实体类。听起来您正在获取基本存储库,而不是您的自定义存储库。
-
您应该提供您尝试使用的代码。
-
是的,看来我得到的只是我的 BaseRepository ......但我看不出我的错 @Cerad
-
是的,你是对的;如果我说错了,我很抱歉。我更新了我的问题并包含了我的代码。我认为现在我的问题更清楚了@dragoste
-
验证您在 Resources/config/doctrine 下没有任何其他映射文件,因为它们会干扰您的注释。如果您正在尝试这样做,您将无法访问私有方法。
标签: php symfony repository