php - MySQL 保存到数据库 Symfony2 时出错

标签 php mysql symfony doctrine

问题:

在使用 Doctrine 将实体持久保存到数据库时,我收到以下 MySQL 错误,我的实体名称都不是保留字。我尝试了很多事情,包括重建数据库和表以及运行doctrine:schema:update,但没有成功。任何帮助将不胜感激。

错误:

string(601) "An exception occurred while executing 'INSERT INTO release (serial, title, artist, length, genre, description, tracks, download, thumbnail, artwork) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["FREELOV", "afds", "asdf", "2:23", "asdf", "asdf", "fdsaf=>2:23", "FREELOV.zip", "FREELOV.jpg", "FREELOVThumb.jpg"]: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'release (serial, title, artist, length, genre, description, tracks, download, th' at line 1"

实体:

    namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class Release
 * @ORM\Entity
 * @ORM\Table(name="release")
 */

class Release
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
/**
 * @ORM\Column(type="string", length=255)
 */
protected $serial;
/**
 * @ORM\Column(type="string", length=255)
 */
protected $title;
/**
 * @ORM\Column(type="string", length=255)
 */
protected $artist;
/**
 * @ORM\Column(type="string", length=255)
 */
protected $length;
/**
 * @ORM\Column(type="string", length=255)  
 */
protected $genre; # Array
/**
 * @ORM\Column(type="string", length=255)
 */
protected $description;
/**
 * @ORM\Column(type="string", length=255) 
 */
protected $tracks; 
/**
 * @ORM\Column(type="string", length=255)
 */
protected $download;
/**
 * @ORM\Column(type="string", length=255) 
 */
protected $thumbnail;
/**
 * @ORM\Column(type="string", length=255) 
 */
protected $artwork;


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

// Doctrine generated getters & setters

发布 Controller :

//namespaces and use statments

class ReleaseController extends Controller{

    public function indexAction(Request $request)
    {
        $release = new Release();

        $form = $this->createFormBuilder($release)
                    ->add('Artist', 'text')
                    ->add('Title', 'text')
                    ->add('Genre', 'text')
                    ->add('Description', 'textarea')
                    ->add('Tracks', 'text', array('attr' => array('style' => 'display:none')))
                    ->add('Length', 'text', array('label' => ' ', 'attr' => array('style' => 'display:none')))
                    ->add('addTrack', 'button', array('label'=>'+'))
                    ->add('Download', 'file') 
                    ->add('Thumbnail', 'file') 
                    ->add('Artwork', 'file') 
                    ->add('save', 'submit', array('label' => 'Add Release'))
                    ->getForm();

        $form->handleRequest($request);

        if($form->isValid()){
            $rel = $this->createAction($form);

            dump($rel);

            $this->addToDB($rel);
        }

        return $this->render('cms/addRel.html.twig', array(
            'form' => $form->createView(),
        ));

    }



    public function createAction(Form $form)
    {
        $release = new Release();
        $release->setSerial('FREELOV'.$release->getId());


        $artist = $form->get('Artist')->getData();
        $title = $form->get('Title')->getData();
        $genre = $form->get('Genre')->getData();
        $description = $form->get('Description')->getData();
        $tracks = $form->get('Tracks')->getData();
        $length = $form->get('Length')->getData();
        $download = $this->uploadAction($form, 'Download', $release);
        $thumb = $this->uploadAction($form, 'Artwork', $release);
        $art = $this->uploadAction($form, 'Thumbnail', $release);


        $release->setArtist($artist);
        $release->setTitle($title);
        $release->setLength($length);
        $release->setGenre($genre);
        $release->setDescription($description);
        $release->setTracks($tracks);
        $release->setDownload($download);
        $release->setThumbnail($thumb);
        $release->setArtwork($art);

        return $release;
    }

    public function uploadAction($form, $file, Release $rel)
    {
        $fileString = $rel->getSerial();

        #TODO: Needs to include error checking, use preg_match and $form[]->getData to make sure files are the right type
        switch($file)
        {
            case 'Download':
                $fileString = $fileString.'.zip';
            break;
            case 'Thumbnail':
                $fileString = $fileString.'Thumb.jpg';
                break;
            case 'Artwork':
                $fileString = $fileString.'.jpg';
            break;
        }

        $dir = 'bin/'; #FIXME: Needs to be directory for file uploads (maybe definable in the parameters.yml?)

        $form[''.$file]->getData()->move($dir, $fileString);
        return $fileString;
    }

    public function addToDB(Release $release)
    {
        try{
        $em = $this->getDoctrine()->getManager();
        $em->persist($release);
        $em->flush();
        return new Response('New release added: '.$release->getArtist().' - '.$release->getTitle());
            }catch(\Exception $e){
        var_dump($e->getMessage());
        }

    }




}

最佳答案

RELEASEreserved word在 MySQL 中。

如果您确实希望将表命名为 release,则必须在实体中设置表名称并引用它,如下所示:

<?php

/**
  * @ORM\Enity
  * @ORM\Table(name="`release`")
  */
class Release;

…

但是,如果您仍然可以选择不同的名称,我建议您这样做。这可以让您免去以后的很多麻烦。

关于php - MySQL 保存到数据库 Symfony2 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30279027/

相关文章:

php - Symfony 2.8 - 如何为任何 URL 配置防火墙?

doctrine - 在 Doctrine2/Symfony2 中插入忽略重复条目

php - 在 PHP 中创建链接的点击计数

php - 固定字符串的正则表达式

php exec()通过cmd行成功运行Rscript但无法通过浏览器运行

mysql - 未设置将 MySQL 数据库连接到 Glassfish 类路径或类名错误

javascript - 如何将数据从 eventSources 提供给 EventListener

php - 获取字符串中另一个词之后的下一个词

mysql - 以不同方式显示查询结果

mysql - Left Join 2个表,只返回出现在第2个表上的记录