2013-03-08 26 views
1

我在這裏新很新的symfony 2.Symfony的2類表繼承夾具SQL錯誤

我嘗試建立一個實體「MailProcess」,從一個實體「進程」繼承。這裏是我的實體定義:

// src/MW/TodoBundle/Entity/Process.php 
namespace MW\TodoBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\InheritanceType("JOINED") 
* @ORM\DiscriminatorColumn(name="process_type", type="string") 
* @ORM\DiscriminatorMap({"mailprocess" = "MailProcess", "process" = "Process"}) 
* @ORM\Table(name="process") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Process 
{ 

    /** 
    * 
    * @ORM\Id 
    * @ORM\Column(type="integer", unique=true) 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * 
    * @ORM\Column(type="string") 
    */ 
    protected $title; 

    /** 
    * 
    * @ORM\Column(type="datetime") 
    */ 
    protected $created; 



    /** 
    * 
    * @ORM\Column(type ="text", nullable= true) 
    */ 
    protected $comment; 
    protected $options; 

    // Getters and Setters 
} 

// src/MW/TodoBundle/Entity/MailProcess.php 
namespace MW\TodoBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use MW\TodoBundle\Entity\Process; 

/** 
* 
* @ORM\Entity 
* @ORM\Table(name="process_mailprocess") 
*/ 
class MailProcess extends Process 
{ 

    /** 
    * 
    * @ORM\Column(type="string") 
    */ 
    protected $from; 

    /** 
    * 
    * @ORM\Column(type="string") 
    */ 
    protected $to; 

    /** 
    * 
    * @ORM\Column(type="string", nullable=true) 
    */ 
    protected $subject; 

    /** 
    * 
    * @ORM\Column(type="text", nullable=true) 
    */ 
    protected $message; 
} 

你可以看到ORM類表繼承使用:兩隻表「過程」和「process_mailprocess」與鑑別列「process_type」。

我確實使用 'php app/console doctrine:schema:update --force' 將模式更新到我的MySQL數據庫,它工作正常。

但後來有我的數據夾具:我在這裏剝離了他們一個,但要確保我有額外的固件也測試實體的'進程'哪些工作正常。

// src/MW/TodoBundle/DataFixtures/ORM/ProcessFixtures.php 

namespace MW\TodoBundle\DataFixtures\ORM; 

use Doctrine\Common\DataFixtures\FixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
use MW\TodoBundle\Entity\Process; 
use MW\TodoBundle\Entity\MailProcess; 

class ProcessFixtures implements FixtureInterface 
{ 

    public function load(ObjectManager $manager) 
    { 
     $mp1= new MailProcess(); 
     $mp1->setTitle("Invoice from heaven") 
      ->setComment("sitebot: Irgendwer will was von euch!") 
      ->setCreated(new \DateTime()) 
      ->setFrom("m") 
      ->setTo("m") 
      ->setSubject("m") 
      ->setMessage("m"); 
     $manager->persist($mp1); 
     $manager->flush(); 
    } 
} 

唉,的 'php應用程序/控制檯學說執行:燈具:load' 確認 'Y',以清除使我

[Doctrine\DBAL\DBALException] 
    An exception occurred while executing 'INSERT INTO process_mailprocess (id, from, to, subject, message) VALUES (?, 
    ?, ?, ?, ?)' with params {"1":28,"2":"m","3":"m","4":"m","5":"m"}: 

    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 'from, to, subject, message) VALUES ('28 
    ', 'm', 'm', 'm', 'm')' at line 1 






    [PDOException] 
    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 'from, to, subject, message) VALUES ('28 
    ', 'm', 'm', 'm', 'm')' at line 1 

我能看到什麼都沒有錯,而且,學說和Symfony創建SQL查詢。 任何人都可以指出我在這裏做錯了嗎?這對我來說是一個真正的頭腦風暴。

回答

0

我想你會得到一個語法錯誤,因爲from是MySql中的保留字。您可以在註釋中設置列名並正確引用。

/** 
* 
* @ORM\Column(type="string", name="`from`") 
*/ 
protected $from; 
+0

非常感謝你,你確實是對的。結合使用保留的「TO」,這可以正常工作。 – marc 2013-03-08 21:49:41