执行sql查询Symfony2时出错

时间:2015-09-12 14:37:43

标签: php symfony

我在查询symfony2时遇到问题,请看这张图片:http://snapplr.com/snap/yt84

当我尝试创建一个新的图库实体(在我的服务器上上传图片文件)时,我收到此错误,

但是当我尝试更新我在数据库中添加手册的实体时,它就可以了。

这是我的实体

<?php

namespace Core\PageBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Knp\DoctrineBehaviors\Model as ORMBehaviors;

use Symfony\Component\Validator\Constraints as Assert;

/**
 * media
 *
 * @ORM\Table(name="page_gallery")
 * @ORM\Entity(repositoryClass="Core\PageBundle\Repository\GalleryRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Gallery
{
    use ORMBehaviors\Translatable\Translatable;
    public function __call($method, $arguments)
    {
        return \Symfony\Component\PropertyAccess\PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $method);
    }
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @Assert\Image(maxSize="3096k", maxSizeMessage="L'image est trop lourde ({{ size }} ko). La taille maximum autorisée est de {{ limit }} ko.")
     */
    private $file;

    /**
     * 
     * @ORM\ManyToOne(targetEntity="Core\PageBundle\Entity\Page", cascade={"persist"})
     * @ORM\JoinColumn(nullable=true)
     */
    private $page;

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


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

    public function getUploadRootDir(){
        return __DIR__.'/../../../../web/uploads/page/gallery';
    }

    public function getAbsolutePath(){
        return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
    }

    /**
    *@ORM\PrePersist()
    *@ORM\PreUpdate()
    */
    public function preUpload(){
        $this->tempFile = $this->getAbsolutePath();
        $this->oldFile = $this->getPath();
            if(null != $this->file){
                $this->path = sha1(uniqid(mt_rand(),true)).'.'.$this->file->guessExtension();
            }
    }

    /**
    *@ORM\PostPersist()
    *@ORM\PostUpdate()
    */
    public function upload(){
        if(null != $this->file){
            $this->file->move($this->getUploadRootDir(),$this->path);
            unset($this->file);
        }
        if($this->oldFile != null){
            unlink($this->tempFile);
        }
    }

    /**
    *@ORM\PreRemove()
    */
    public function preRemoveUpload(){
        $this->tempFile = $this->getAbsolutePath();
    }

    /**
    *@ORM\PostRemove()
    */
    public function removeUpload(){
        if(file_exists($this->tempFile)){
            unlink($this->tempFile);
        }
    }

    public function getPath(){
        return $this->path;
    }

    public function __toString(){
        return $this->getName();
    }

    /**
     * Set path
     *
     * @param string $path
     * @return Media
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    /**
     * Set page
     *
     * @param \Core\PageBundle\Entity\Page $page
     * @return Gallery
     */
    public function setPage(\Core\PageBundle\Entity\Page $page = null)
    {
        $this->page = $page;

        return $this;
    }

    /**
     * Get page
     *
     * @return \Core\PageBundle\Entity\Page 
     */
    public function getPage()
    {
        return $this->page;
    }

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

        return $this;
    }

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

    public function setFile($file)
    {
        $this->file = $file;
 
        // On vérifie si on avait déjà un fichier pour cette entité
        if (null !== $this->path) {
            // On sauvegarde l'extension du fichier pour le supprimer plus tard
            $this->tempFile = $this->path;
 
            // On réinitialise les valeurs des attributs url et alt
            $this->path = null;
        }
    }
 
    public function getFile()
    {
        return $this->file;
    }
}

这是我的控制器的动作:

public function createGalleryAction(Request $request, $slug){
        $entity = new Gallery();
        $em = $this->getDoctrine()->getManager();
        $page = $em->getRepository('PageBundle:Page')->findOneBySlug($slug);
        $page = $em->getRepository('PageBundle:Page')->find($page->getId());

        $entity->setPage($page);

        $form = $this->createForm(new GalleryType(), $entity);
        $form->bind($request);
        if ($form->isValid()) {
            $em->persist($entity);
            $em->flush();
            $this->get('session')->getFlashBag()->add('success', 'L\'image a été crée !');
            if(isset($_POST['create_new'])){
                return $this->redirect($this->generateUrl('pa_gallery_new', array('slug'                                                        => $slug)));
            }else{
                return $this->redirect($this->generateUrl('pa_gallery_show', array('slug'                                                        => $slug) ));
            }  
        }

        return $this->render('::crudGenerator/new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
            'actionList'            => $this->generateUrl('pa'),
            'actionGalleryList'     => $this->generateUrl('pa_gallery_show', array('slug'   => $slug)),
            'actionCreate'      => $this->generateUrl('pa_gallery_create', array('slug'                                                        => $slug)),
            'name'                  => 'page',
            'subName'               => 'galerie photos',
        ));
    }

1 个答案:

答案 0 :(得分:0)

order是mysql中的保留关键字(用于order by),并在保留关键字导致语法错误后命名列。将order字段重命名为其他字段可以解决问题。

https://dev.mysql.com/doc/refman/5.0/en/keywords.html