2012-06-15 33 views
1

我明显在深入。我无法把握学说ModelClass和模型表格类的益处延伸的基础模型ZF主义模型类分机基类

例如

class StaffStaff extends Base_StaffStaff 
{ 
    public function getStaffInformation($id_staff_staff){ // == getInformationsByStaff() 

     $query = Doctrine_Query::create() 
         ->from("StaffStaff s") 
         ->innerJoin('s.StaffContract c') 
         ->where("s.id_staff_staff = ?", $id_staff_staff); 
     $result = $query->execute(); 

     return $result; 

    } 
} 

和在控制器

StaffController{ 

    public function readAction() { 

     $id = $this->getRequest()->getParam("id_staff_staff"); 

     // Get information about a staff 
     $model = new StaffStaff(); 
     $q = $model->getStaffInformation($id); 

     $this->view->data = $q[0]; 

/** 
* 
* Why do you have to say $q[0] ? 
* Is there no better way of doing it? 
* How can we access the properties from other tables contained inside the BaseClass extended by the ModelClass 
* 
*/ 
} 

型号:

/** 
* Base_StaffStaff 
* 
* This class has been auto-generated by the Doctrine ORM Framework 
* 
* @property integer $id_staff_staff 
* @property integer $fk_id_staff_individual 
* @property integer $fk_id_staff_team 
* @property integer $fk_id_staff_function 
* 
*/ 

回答

1

使用查询生成器API时,您在中如何组装查询函数,​​方法返回可迭代的游标。这个游标本质上是一个数组,这就是$ q [0]将访问第一个元素的原因。

如果你不是试图只返回一个结果,你应该使用getSingleResult()代替:

$query = Doctrine_Query::create() 
    ->from("StaffStaff s") 
    ->innerJoin('s.StaffContract c') 
    ->where("s.id_staff_staff = ?", $id_staff_staff); 
return $query->getSingleResult(); 

在另一方面可迭代光标整齐有,因为你可以这样做:

$staff = $model->getStaffInformation($id); 
foreach($staff as $person) 
{ 
    // Assign all staff to view array 
    $this->view->staff[] = $person; 
}