1

私は明らかに最深部にいます。基本モデルを拡張する Doctrine ModelClass と Model Table Class の利点を理解できていません

例えば

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
 * 
 */
4

1 に答える 1

1

getStaffInformation()関数でクエリを組み立てる方法であるクエリ ビルダー API を使用すると、execute()メソッドは反復可能なカーソルを返します。このカーソルは本質的に配列です。そのため、$q[0] は最初の要素にアクセスします。

代わりに 1 つの結果のみを返そうとする場合は、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;
}
于 2012-06-16T01:08:27.867 に答える