2016-05-31 34 views
-1

我在我的Symfony項目中有一個自定義存儲庫,我想像搜索工具一樣使用。我的項目結構如下:如何在Symfony中使用自定義存儲庫?

P.D.已更新的問題和代碼

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

嗯,我想訪問我的定製的倉庫,使用下面的方法findByTitle,該方法應該與描述字段是類似的對象返回數組。我把一個簡單的print(任期的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>'; 
     .... 
    } 
+0

有理解你的句子有困難,但你可以檢查一下,確保你的實體管理器被正確地映射到你的實體類。聽起來像你正在獲得基礎知識庫,而不是你自定義的。 – Cerad

+0

你應該提供你正在嘗試使用的代碼。 –

+0

是的,似乎我只是獲得了我的BaseRepository ...但我沒有看到我的錯@Cerad –

回答

0

的問題是,我宣佈我的功能,而不是私人的公共

private function findByTitle($term){ 

,而不是

public function findByTitle($term){ 
0

剛一個猜測,因爲你的問題很不清楚(尤其是,最後一段):你只註冊了服務,還是你還告訴Symfony使用該實體的存儲庫(推測爲MyEntity)?例如,使用註釋,你需要這樣的事:

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

是的,你是對的;如果我錯過了,我很抱歉。我更新了我的問題,幷包含了我的代碼。我認爲現在它更清楚我的問題 –

+0

而且我在我的實體類中使用註釋到我的存儲庫 –

相關問題