簡體   English   中英

Symfony2:找不到存儲庫類

[英]Symfony2 : Repository Class not found

今天,我陷入了存儲庫類函數,遇到了這個錯誤

未定義的方法“測試”。 方法名稱必須以findBy或findOneBy開頭!

我已經檢查了這些解決方案- 解決方案1 解決方案2 解決方案3

我需要添加到配置文件中嗎?

這是我的實體類

// src/Foo/NewsBundle/Entity/News.php
namespace Foo\NewsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * News
 * @ORM\Entity(repositoryClass="Foo\NewsBundle\Repository\NewsRepository")
 * @ORM\Table(name="news")
 * @ORM\HasLifecycleCallbacks()
 */
class News
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $title;

這是我的存儲庫類

// Foo/NewsBundle/Repository/NewsRepository.php

namespace Foo\NewsBundle\Repository;
use Doctrine\ORM\EntityRepository;

Class NewsRepository extends EntityRepository
{
    public function test()
    {
        return "Nisarg";
    }
}

我正在從控制器中將此test()函數稱為wat

public function indexAction()
    {
//        $news = $this->getDoctrine()
//                ->getRepository('FooNewsBundle:News')
//                ->findAll();
        $em = $this->getDoctrine()
                   ->getManager();

        $news = $em->getRepository('FooNewsBundle:News')->test();

        if (!$news) {
            throw $this->createNotFoundException('No news found');
        }
        $build['news'] = $news;
        return $this->render('FooNewsBundle:Default:news_show_all.html.twig', $build);
    }

檢查是否在News orm配置文件中指定了存儲庫類。

必須有類似“ repositoryClass:Foo \\ NewsBundle \\ Repository \\ NewsRepository”之類的內容

並且不要忘記清除緩存!

我認為存儲庫類的標准是將其放在實體文件夾的子目錄中,並且仍然使用相同的實體名稱空間。 您使用了不同的命名空間,這就是為什么我認為您有錯誤。

根據菜譜,這是定義實體和自定義倉庫的方式。 鏈接到菜譜中的自定義存儲庫類

實體

// src/Acme/StoreBundle/Entity/Product.php
namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Acme\StoreBundle\Entity\ProductRepository")
 */
class Product
{
    //...
}

庫:

// src/Acme/StoreBundle/Entity/ProductRepository.php
namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\EntityRepository;

class ProductRepository extends EntityRepository
{
    public function findAllOrderedByName()
    {
        return $this->getEntityManager()
            ->createQuery(
                'SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC'
            )
            ->getResult();
    }
}

Fedor Petrick是對的!

您應該尋找與實體相對應的orm文件。 在我的情況下,我在OfertaBundle \\ Entity文件夾中創建了一個自定義存儲庫,名為:OfertaRepository.php。

另一方面,我有一個文件Oferta.orm.xml

在第三行中,它說:

  <entity name="OfertaBundle\Entity\Oferta" table="oferta">

但是應該是:

  <entity name="OfertaBundle\Entity\Oferta" table="oferta" repository-class="OfertaBundle\Entity\OfertaRepository">

現在,OfertaRepository.php中的方法效果很好!

在您的實體中,您沒有使用注釋,請檢查Resources / config / doctrine中是否有news.yml文件

您的代碼在實體和存儲庫中看起來正確。 也許您可以嘗試直接調用getRepository而不使用->getManager

$this->getDoctrine->getRepository('FooNewsBundle:News')->test();

如果您需要特定的字段,則應該使用findOneByfindBy在大多數情況下,它比編寫自定義類要容易得多。

http://symfony.com/doc/current/book/doctrine.html

你在生產嗎? 也許清除緩存是解決方案:

php app/console cache:clear --env=prod --no-debug

如果清除緩存不起作用, $em->getRepository('FooNewsBundle:News') instanceOf Foo\\NewsBundle\\Repository\\NewsRepository真還是假? 從外觀上看,您是否以某種方式沒有獲得正確的存儲庫?

您是否生成了實體?

php app/console doctrine:generate:entities BUNDLENAME

啟動此命令,然后重試您的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM