symfony doctrine查询结果和执行函数

时间:2010-09-24 17:09:26

标签: symfony1 doctrine

我在symfony中有这个学说查询。当我在phpBB中运行由此dql查询生成的mysql代码时,它会返回很多行,但是当我在symfony中运行它并使用此代码访问其结果时:

foreach ($this->courses as $course){
 echo "<br>".$course->firstname;}

它只返回一个名字。此外,当我尝试获取 $ course-&gt; title 时,会出现此错误 “学生”上的未知记录属性/相关组件“标题”

查询:

$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 = ?",2);
$this->courses=$q->execute();

的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

4 个答案:

答案 0 :(得分:4)

这就是我如何运作......

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

//$this->query=$q->getSqlQuery();
$q->setHydrationMode(Doctrine_Core::HYDRATE_SCALAR);
$this->Student=$q->execute(array());

并在模板中

<?php foreach ($Student as $student): ?>
<tr>
  <td><?php echo $i; ?></td>
  <td><?php echo $student['c_title'] ?></td>
  <td><?php echo $student['pc_year'] ?></td>
  <td><?php echo $student['p_program'] ?></td>
</tr>
<?php endforeach; ?>

答案 1 :(得分:2)

您的顶级节点是Students对象,因为您在Students

中使用了from()

您的错误描述了这个

  

此外,当我尝试获取$course->title时,此错误会显示为Unknown record property / related component "title" on "Students"

因为您有Students个对象,而不是Courses个对象(title所属的对象)

你使用

让自己感到困惑
  

$这 - &GT;课程= $ Q-&GT;执行();


$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); // beware to SQL injection, use parameters

$this->Student=$q->fectchOne(); // as you are only retrieve one in your where clause
$this->Student=$q->execute()->getFirst(); // same

<tr>
  <td><?php echo $i; /* not sure what this is for */ ?></td>
  <td><?php echo $Student->Programs->Programcourses->Courses->title ?></td>
  <td><?php echo $Student->Programs->Programcourses->year ?></td>
  <td><?php echo $Student->Programs->program ?></td>
</tr>

如果您希望Courses成为最佳人选,请选择以下内容:

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

答案 2 :(得分:1)

一些事情:

  • 而不是$ course-&gt; firstname和$ course-&gt; title,你想要$ course-&gt; getFirstName()和$ course-&gt; getTitle()。
  • 您应该为表格使用单数名称而不是复数,例如课程而不是课程。
  • symfony中的约定是使用“id”作为主键名而不是你正在做的事情。

解决这些问题 - 特别是第一个问题 - 你的整体问题应该消失。

答案 3 :(得分:0)

准备这样的查询:

public function getAttendanceRecord($employeeId, $date) {
try {
    $query = Doctrine_Query::create()
            ->from("attendanceRecord")
            ->where("employeeId = ?", $employeeId);
    $records = $query->execute();
    if (is_null($records[0]->getId())) {
        return null;
    } else {
        return $records;
    }
} catch (Exception $ex) {
    throw new DaoException($ex->getMessage());
}

}

并试着像这样得到/打印:

  

$ attendanceOfTheDay = AttendanceDao :: getAttendanceRecord($ parameters [&#39; employeeId&#39;],$ parameters [&#39; date&#39;]) - &gt; toArray();   回声&#39; &#39 ;;的print_r($ punchInTImeOfTheDay);   出口();