Symfony2 transtable添加查询

时间:2015-09-24 14:51:38

标签: php mysql symfony doctrine-orm doctrine

我对Doctrine和Transtable的问题。

我想将新数据添加到数据库中。我的默认语言是pl_PL。

我的控制器:

namespace Gogs\CMSBundle\Controller;    
use Gogs\CMSBundle\Entity\Page;

...

public function addAction()
    {
        $em = $this->getDoctrine()->getManager();

        $page = new Page();
        $page->setName('new');
        $em->persist($page);
        $em->flush();


        $page->setTranslatableLocale('it');
        $page->setName('new ita');
        $em->persist($page);
        $em->flush();

        $page->setTranslatableLocale('en');
        $page->setName('new eng');
        $em->persist($page);
        $em->flush();

        $em->persist($page);
        $em->flush();

        $abc = 'Success';
        return new Response($abc);
    }

当打开web / app_dev.php /添加页面时,它会出现错误:

  

执行'INSERT INTO Page(名称,   内容,菜单,顺序,图标)VALUES(?,?,?,?,?)'与params   [“new”,null,null,null,null]:

     

SQLSTATE [42000]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,用于在'order,icon)附近使用正确的语法   第1行的VALUES('new',NULL,NULL,NULL,NULL)

我的Page.php

<?php

namespace Gogs\CMSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;

/**
 * Page
 *
 * @ORM\Table()
 * @ORM\Entity
 *
 */
class Page
{
    /**
     * @Gedmo\Locale
     */
    private $locale = 'pl_PL';

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     * @Gedmo\Translatable
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text")
     * @Gedmo\Translatable
     */
    private $content;

    /**
     * @var string
     *
     * @ORM\Column(name="menu", type="string", length=255)
     */
    private $menu;

    /**
     * @var integer
     *
     * @ORM\Column(name="order", type="integer", length=32)
     */
    private $order;

    /**
     * @var string
     *
     * @ORM\Column(name="icon", type="string", length=255)
     */
    private $icon;


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

    /**
     * Set name
     *
     * @param string $name
     * @return Page
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set content
     *
     * @param string $content
     * @return Page
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

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

    /**
     * Set menu
     *
     * @param string $menu
     * @return Page
     */
    public function setMenu($menu)
    {
        $this->menu = $menu;

        return $this;
    }

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

    /**
     * Set order
     *
     * @param integer $order
     * @return Page
     */
    public function setOrder($order)
    {
        $this->order = $order;

        return $this;
    }

    /**
     * Get order
     *
     * @return integer 
     */
    public function getOrder()
    {
        return $this->order;
    }

    /**
     * Set icon
     *
     * @param string $icon
     * @return Page
     */
    public function setIcon($icon)
    {
        $this->icon = $icon;

        return $this;
    }

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

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }
}

我使用Symfony 2.1。你可以帮帮我吗?哪里出错?

修改:

我发现了错误。

我不会在MySQL中使用“order”字样。我把它换成'位置'。另外,我将'nullable = true'添加到Page.php。

中的每个列

现在,数据“NEW”进入数据库,但是休息(“new eng”,“new ita”)不是。表'ext_translation'为空。

新的page.php:

<?php

namespace Gogs\CMSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;

/**
 * Page
 *
 * @ORM\Table()
 * @ORM\Entity
 *
 */
class Page implements Translatable
{
    /**
     * @Gedmo\Locale
     */
    private $locale;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     * @Gedmo\Translatable
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text", nullable=true)
     * @Gedmo\Translatable
     */
    private $content;

    /**
     * @var string
     *
     * @ORM\Column(name="menu", type="string", length=255, nullable=true)
     */
    private $menu;

    /**
     * @var integer
     *
     * @ORM\Column(name="position", type="integer", length=32, nullable=true)
     */
    private $position;

    /**
     * @var string
     *
     * @ORM\Column(name="icon", type="string", length=255, nullable=true)
     */
    private $icon;


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

    /**
     * Set name
     *
     * @param string $name
     * @return Page
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set content
     *
     * @param string $content
     * @return Page
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

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

    /**
     * Set menu
     *
     * @param string $menu
     * @return Page
     */
    public function setMenu($menu)
    {
        $this->menu = $menu;

        return $this;
    }

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

    /**
     * Set position
     *
     * @param integer $position
     * @return Page
     */
    public function setPositionr($position)
    {
        $this->position = $position;

        return $this;
    }

    /**
     * Get position
     *
     * @return integer 
     */
    public function getPosition()
    {
        return $this->position;
    }

    /**
     * Set icon
     *
     * @param string $icon
     * @return Page
     */
    public function setIcon($icon)
    {
        $this->icon = $icon;

        return $this;
    }

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

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }
}

在Controller中我尝试为数据“NEW”添加翻译。

$ em = $ this-&gt; getDoctrine() - &gt; getManager();         // $ article = $ em-&gt; find('Gogs \ CMSBundle \ Entity \ Page',8 / 文章ID /);

    $repository = $this->getDoctrine()
        ->getRepository('GogsCMSBundle:Page');

    $article = $repository->find(8);

    $article->setName('my title in eng');
    $article->setContent('my content in eng');
    $article->setTranslatableLocale('en'); // change locale
    $em->persist($article);
    $em->flush();

不幸的是,在表“Page”中,记录8有新值,表ext_translation进一步为空

编辑2:

我解决了我的问题。

在config.yml中缺少#Distrtrine Extensions配置。现在看起来像:

    # Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        entity_managers:
                    default:
                        auto_mapping: true
                        mappings:
                            gedmo_translatable:
                                type: annotation
                                prefix: Gedmo\Translatable\Entity
                                dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
                                alias: GedmoTranslatable # (optional) it will default to the name set for the mapping
                                is_bundle: false

# Doctrine Extensions Configuration
stof_doctrine_extensions:
        default_locale: "%locale%"
        translation_fallback: true
        orm:
            default:
              translatable: true

0 个答案:

没有答案