简体   繁体   中英

How to use a custom repository in Symfony?

I have a custom repository in my Symfony's project and I want use like a search tool. My project' structure is the following:

PD UPDATED Question and code

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

Well, I want access to my custom repository and use the following method findByTitle , which method should return an array with objects which description field be similar. I put a simple print (var_dump of the term entered) inside of my function to see if my browser shows it, but it isn't showed yet...

My 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;
  }
}

The beginning of 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 {

......

My 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]

And I'm calling my service in the following way:

 public function searchAction(Request $request, $term){
        $manager = $this->get('app.myentity.manager');
        $result = $manager->findByTitle($term);

        echo '<h5>';
        var_dump($result);
        echo '</h5>';
        ....
    }

Just a guess, as your question is far from being clear (esp. the last paragraph): did you only register the service, or did you also tell Symfony to use the repository for the entity (presumably MyEntity )? For instance, using annotations, you'd need something like this:

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

The problem was that I declared my function as private instead of public

private function findByTitle($term){

instead of

public function findByTitle($term){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM