views:

105

answers:

2

I am trying to run this query and getting error "Unknown relation alias programs". this is the query.

$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');

I tried this one too.

$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);

This is my 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

also is there a mysql to dql converter tool? first i make mysql queries and then i change those queries to dql. Is there an easy method?

+2  A: 

You can easily try your dql using the command line symfony doctrine:dql Now, in your query your relation programs is in lowercase, whereas it is in uppercase in your schema. If I were you, I would try something like this:

$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);
greg0ire
Those joins should have capital names of the relations. Also, you don't have to cast $studentid as an array.
jeremy
I'm answering too quickly today. Thank god, you're here :)
greg0ire
thanks greg..its working now but i am confused..i made my database in phpbb..and all relations were in lowercase..then i generated schema.yml from this database..in phpbb i still have lowercase relation names..then what for what reason symfony using this kind of convention?.. also what does idstudents = ? mean?
mysterious
the question mark is a placeholder for a parameter ($studentid in this case). I triggers a prepared statement. See here what this is : http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html
greg0ire
A: 

There was a little mistake in your answer greg, if u can see select function, each column name is separated by comma, it should be like this..

$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);
mysterious
Using that style where condition is a BIG mistake! You are by passing PDO by doing that and all of the benefits that come with it (like preventing SQL injection!).
jeremy
Fixed. I definitely should get some sleep.
greg0ire