0

I have a simple symfony 2 setup with Doctrine ORM and a db with some underscore seperated field names (for instance "error_page"). Querying this never gives a result (getTitle does give a result, getErrorPage is always empty) and symfony gives me an error:

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

I can't figure out why... My Document looks like this:

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

Document creation via this document works fine by the way. The field name is also set to error_page as expected... I'm at a loss here :S

1
  • 1
    Use camelCase in Twig: {{ document.errorPage }} Commented Aug 28, 2015 at 15:43

1 Answer 1

1

Actually, Doctrine+Symfony2 assume camel case variable naming. Twig using the getter method names should be obvious, how should it access protected/private variables? It needs a name for something public : the getter. You're probably wondering why "get" is ignored; it is a simplification for designers as they normally shouldnt know about what "getters" are and the difference between methods and variables.

so in your twig file ,change :

{{document.error_page}}

to

{{document.errorPage}}

this would helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm. The issue with data not showing in the controller was due to caching being too harsh... Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.