持久化到数据库Symfony2的MySQL错误

时间:2015-05-16 18:02:27

标签: php mysql symfony doctrine

问题:

在将实体持久化到具有学说的数据库时,我得到以下MySQL错误,我的实体名称都不是保留字。我已经尝试了很多东西,包括重建数据库和表并运行doctrine:schema:update但无济于事。任何帮助将不胜感激。

错误:

  

string(601)“执行'INSERT INTO时发生异常   发行(连续剧,标题,艺术家,长度,流派,描述,曲目,   下载,缩略图,艺术品)VALUES(?,?,?,?,?,?,?,?,?,?)'   用params [“FREELOV”,“afds”,“asdf”,“2:23”,“asdf”,“asdf”,   “fdsaf => 2:23”,“FREELOV.zip”,“FREELOV.jpg”,“FREELOVThumb.jpg”:   SQLSTATE [42000]:语法错误或访问冲突:1064您有   SQL语法错误;查看与您的手册相对应的手册   MySQL服务器版本,用于在'release'附近使用正确的语法   (连续剧,标题,艺术家,长度,流派,描述,曲目,下载,   在第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

ReleaseController:

//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());
        }

    }




}

1 个答案:

答案 0 :(得分:0)

RELEASE是MySQL中的reserved word

如果您确实希望将您的表命名为release,则必须在实体中设置表名并对其进行搜索,如下所示:

<?php

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

…

但是,如果您仍然可以选择其他名称,我建议您这样做。这可以让你以后免除很多麻烦。