2012-06-15 52 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; 
}