如何在Symfony中使用自定义存储库?

时间:2016-05-31 23:51:30

标签: php symfony repository

我的Symfony项目中有一个自定义存储库,我希望像搜索工具一样使用。我的项目结构如下:

Pd积。更新的问题和代码

-Manager:
 * BaseManager.php
 * MyEntityManager.php
-Repository:
 * BaseRepository.php
 * MyEntityRepository.php

好吧,我想访问我的自定义存储库并使用以下方法findByTitle,该方法应返回一个数组,其中包含描述字段类似的对象。我在我的函数中放了一个简单的打印(输入术语的var_dump)来查看我的浏览器是否显示它,但它还没有显示...

我的BaseManager:

<?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();
    }

}

MyEntityManager:

<?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);
    }
}

BaseRepository:

<?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>';
        ....
    }

2 个答案:

答案 0 :(得分:0)

只是一个猜测,因为你的问题远非明确(尤其是最后一段):你是否只注册了服务,或者你是否也告诉Symfony使用该实体的存储库(大概是MyEntity )?例如,使用注释,你需要这样的东西:

@ORM\Entity(repositoryClass="The\RepositoryClass")

答案 1 :(得分:0)

问题在于我将我的功能声明为私有而非公开

private function findByTitle($term){

而不是

public function findByTitle($term){