2010-09-24 135 views
1

我試圖運行這個查詢,並得到錯誤「未知的關係別名程序」。 這是查詢。學說symfony多個leftjoin查詢

$q= Doctrine_Query::create() 
      ->select('students.firstname', 
         'students.middlename', 
         'students.lastname', 
         'programs.program', 
         'courses.title', 
         'programcourses.year') 
      ->from('students s, s.programs p, p.programcourses p2, p2.courses c'); 

我也試過這個。

$q= Doctrine_Query::create() 
      ->select('students.firstname', 
         'students.middlename', 
         'students.lastname', 
         'programs.program', 
         'courses.title', 
         'programcourses.year') 
      ->from('students') 
      ->leftJoin('programs') 
      ->leftJoin('programcourses') 
      ->leftJoin('courses') 
      ->where("idstudents=".$studentid); 

這是我的Schema.yml。

Courses: 
    connection: doctrine 
    tableName: courses 
    columns: 
    idcourses: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    title: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    relations: 
    Programcourses: 
     local: idcourses 
     foreign: idcourses 
     type: many 
Programcourses: 
    connection: doctrine 
    tableName: programcourses 
    columns: 
    idprogramcourses: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    idprograms: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    idcourses: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    year: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    relations: 
    Courses: 
     local: idcourses 
     foreign: idcourses 
     type: one 
    Programs: 
     local: idprograms 
     foreign: idprograms 
     type: one 
Programs: 
    connection: doctrine 
    tableName: programs 
    columns: 
    idprograms: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: false 
    program: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    relations: 
    Programcourses: 
     local: idprograms 
     foreign: idprograms 
     type: many 
    Students: 
     local: idprograms 
     foreign: idprograms 
     type: many 
Roles: 
    connection: doctrine 
    tableName: roles 
    columns: 
    idroles: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: false 
    role: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
Students: 
    connection: doctrine 
    tableName: students 
    columns: 
    idstudents: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    firstname: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    middlename: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    lastname: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    idprograms: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    session: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    username: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    password: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    email: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    relations: 
    Programs: 
     local: idprograms 
     foreign: idprograms 
     type: one 
Teachers: 
    connection: doctrine 
    tableName: teachers 
    columns: 
    idteachers: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    firstname: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    lastname: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    username: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    password: 
     type: string(45) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    email: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 

還有一個mysql的dql轉換工具嗎?首先我進行mysql查詢,然後將這些查詢更改爲dql。有一個簡單的方法嗎?

回答

3

您可以使用命令行symfony doctrine輕鬆地嘗試您的dql:dql 現在,在您的查詢中,您的關係程序使用小寫字母,而在模式中使用大寫字母。 如果我是你,我會嘗試這樣的事:

$q= Doctrine_Query::create() 
      ->select('s.firstname, 
         s.middlename, 
         s.lastname, 
         p.program, 
         c.title, 
         pc.year') 
      ->from('Students s') 
      ->leftJoin('s.Programs p') 
      ->leftJoin('p.Programcourses pc') 
      ->leftJoin('pc.Courses') 
      ->where("idstudents = ?", $studentid); 
+1

那些連接應該有關係的資本名稱。另外,您不必將$ studentid轉換爲數組。 – 2010-09-24 15:06:36

+0

今天我的回答太快。感謝上帝,你在這裏:) – greg0ire 2010-09-24 15:11:29

+0

謝謝greg..its現在工作,但我很困惑..我在phpbb ..我所有的關係都是小寫..然後我從這個數據庫生成schema.yml我的數據庫.. ..在phpbb我仍然有小寫關係名稱..然後什麼原因symfony使用這種約定?..什麼idstudents =?意思? – mysterious 2010-09-24 15:47:37

0

有在你的答案格雷格稍有不慎,如果u可以看到選擇功能,每列名由逗號分隔,它應該是這樣的..

$q= Doctrine_Query::create() 
     ->select('s.firstname, 
        s.middlename, 
        s.lastname, 
        p.program, 
        c.title, 
        pc.year') 
     ->from('Students s') 
     ->leftJoin('s.Programs p') 
     ->leftJoin('p.Programcourses pc') 
     ->leftJoin('pc.Courses c') 
     ->where("idstudents = ".$studentid); 
+2

使用這種風格的條件是一個大錯誤!您通過傳遞PDO來實現這一點以及隨之而來的所有好處(如防止SQL注入!)。 – 2010-09-24 18:31:54

+0

固定。我絕對應該睡一覺。 – greg0ire 2010-09-24 23:59:37