草庐IT

php - Symfony2 DoctrineMongoDBBundle : Getting empty result after creating Repository classes

coder 2023-11-04 原文

很抱歉问了这么长的问题,但我认为这对于有经验的 symfony 和 mongodb 开发人员来说很简单。 问:如果我生成存储库类,所有存储库 findAll()、findBy()、findOneBy()、findBy*() 都会失败。为什么会失败? 低于我的 Composer 状态。

"require":
 {
        "php": ">=5.3.9",
        "symfony/symfony": "2.8.*",
        "doctrine/orm": "^2.4.8",
        "doctrine/doctrine-bundle": "~1.4",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.11.3",
        "sensio/distribution-bundle": "~5.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "incenteev/composer-parameter-handler": "~2.0",
        "doctrine/mongodb-odm": "^1.1",
        "doctrine/mongodb-odm-bundle": "^3.2"
    }

Controller 方法如下

     /**
     * @Route("survey/{token}", name="survey_index")
     */
    public function indexAction($token){
        // if i remove repository, below query gives perfect result.
        $survey = $this->get('doctrine_mongodb')
        ->getRepository('CoreBundle:Survey')->findAll();

        return $this->render('CoreBundle:Survey:index.html.twig',array('survey'=>$survey));
    }

文档类如下

namespace CoreBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document(collection="survey_master")
 * @ODM\Document(repositoryClass="CoreBundle\Repository\SurveyRepository")
 */
class Survey {

    /**
     * @ODM\Id
     */
    protected $id;

    /**
     * @ODM\Field(type="string")
     */
    protected $name;

// setters and getters below
}

如果我删除

* @ODM\Document(repositoryClass="CoreBundle\Repository\SurveyRepository")

并执行缓存清除,一切正常。

SurveyRepository 类

namespace CoreBundle\Repository;

use Doctrine\ODM\MongoDB\DocumentRepository;

/**
 * SurveyRepository
 *
 * This class was generated by the Doctrine ODM. Add your own custom
 * repository methods below.
 */
class SurveyRepository extends DocumentRepository
{


}

最佳答案

Survey 有两个带有 @ODM\Document 的注释,它们会覆盖自己。

将两个参数放在一个注释中。

关于php - Symfony2 DoctrineMongoDBBundle : Getting empty result after creating Repository classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41425731/

有关php - Symfony2 DoctrineMongoDBBundle : Getting empty result after creating Repository classes的更多相关文章

随机推荐