2015-08-28 65 views
0

我有一个简单的symfony 2设置与Doctrine ORM和一些下划线分隔字段名称(例如“error_page”)的数据库。查询该从来没有给一个结果(的getTitle确实给结果,getErrorPage总是空)和symfony的给我一个错误:Symfony 2 Doctrine ODM返回现有字段上的错误

Method "error_page" for object "My\CmsBundle\Document\Website" does not exist in MyCmsBundle:Default:dashboard.html.twig at line 5 

我想不通为什么......我的文件看起来是这样的:

<?php 

// src/My/CmsBundle/Document/Website.php 
namespace My\CmsBundle\Document; 

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

/** 
* @MongoDB\Document(
*  collection="websites" 
*) 
*/ 
class Website 
{ 
    /** 
    * @MongoDB\Id 
    */ 
    protected $id; 

    /** 
    * @MongoDB\String 
    */ 
    protected $slug; 

    /** 
    * @MongoDB\Field(type="string", name="error_page") 
    */ 
    protected $error_page = ""; 


    /** 
    * @MongoDB\String 
    */ 
    protected $title; 


    /** 
    * @MongoDB\String(name="seo_title") 
    */ 
    protected $seo_title; 

    /** 
    * @MongoDB\String 
    */ 
    protected $seo_description; 




    /** 
    * @MongoDB\Collection 
    */ 
    protected $url = array(); 

    /** 
    * Get id 
    * 
    * @return id $id 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set slug 
    * 
    * @param string $slug 
    * @return self 
    */ 
    public function setSlug($slug) 
    { 
     $this->slug = $slug; 
     return $this; 
    } 

    /** 
    * Get slug 
    * 
    * @return string $slug 
    */ 
    public function getSlug() 
    { 
     return $this->slug; 
    } 

    /** 
    * Set title 
    * 
    * @param string $title 
    * @return self 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 
     return $this; 
    } 

    /** 
    * Get title 
    * 
    * @return string $title 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * Set errorPage 
    * 
    * @param string $errorPage 
    * @return self 
    */ 
    public function setErrorPage($errorPage) 
    { 
     $this->error_page = $errorPage; 
     return $this; 
    } 

    /** 
    * Get errorPage 
    * 
    * @return string $errorPage 
    */ 
    public function getErrorPage() 
    { 
     return $this->error_page; 
    } 

    /** 
    * Set url 
    * 
    * @param collection $url 
    * @return self 
    */ 
    public function setUrl($url) 
    { 
     $this->url = $url; 
     return $this; 
    } 

    /** 
    * Get url 
    * 
    * @return collection $url 
    */ 
    public function getUrl() 
    { 
     return $this->url; 
    } 

    /** 
    * Set seoTitle 
    * 
    * @param string $seoTitle 
    * @return self 
    */ 
    public function setSeoTitle($seoTitle) 
    { 
     $this->seo_title = $seoTitle; 
     return $this; 
    } 

    /** 
    * Get seoTitle 
    * 
    * @return string $seoTitle 
    */ 
    public function getSeoTitle() 
    { 
     return $this->seo_title; 
    } 

    /** 
    * Set seoDescription 
    * 
    * @param string $seoDescription 
    * @return self 
    */ 
    public function setSeoDescription($seoDescription) 
    { 
     $this->seo_description = $seoDescription; 
     return $this; 
    } 

    /** 
    * Get seoDescription 
    * 
    * @return string $seoDescription 
    */ 
    public function getSeoDescription() 
    { 
     return $this->seo_description; 
    } 
} 

通过本文档创建文档的方式很好。如预期的那样,字段名称也设置为error_page ...我在这里处于亏损状态:S

+1

在Twig中使用camelCase:'{{document.errorPage}}' – malcolm

回答

1

实际上,Doctrine + Symfony2假定骆驼大小写变量命名。 Twig使用getter方法名称应该很明显,它应该如何访问protected/private变量?它需要一个公开的名称:getter。你可能想知道为什么“get”被忽略;这对设计师来说是一种简化,因为他们通常不知道“吸气剂”是什么以及方法和变量之间的区别。

在树枝文件,更改

这样:

{{document.error_page}} 

{{document.errorPage}} 

this会很有帮助。

+0

就像一个魅力。数据没有在控制器中显示的问题是由于缓存过于苛刻...谢谢! –

+0

欢迎您! –

相关问题