سؤال

هل هناك طريقة أفضل للعمل مع ZF استخدام الأفخانات والأشياء الحقيقية والحياة الجدولية_OBJECS.

هذه هي الطريقة التي أفعلها مع إطار zend:

class User_DbTable extends Zend_DB_Table_Abstract{
      protected $_name = "user"; // name of the table
}

كائن المستخدم -> كائن المستخدم:

class User{
  protected $_id;
  protected $_name;
  protected $_addresses; //list of OBJs

  public function set_name($_name){
    $this->_name = $_name;
  }
  public function get_name(){
    return $this->_name;
  }
  public function set_adresses($_addresses){
    $this->_addresses = $_addresses;
  }
// and so on....
}

mapper:

class UserMapper{
   protected $userTBL;

   public function __construct(){
     $this->userTBL = new User_DbTable();
   }
   public function __fatchAll(){
     $select = $this->userTBL->select();
     foreach($this->userTBL->fetchAll($select) as $row){
       $user = new User(); // model
       $user->set_name($row->name);
       // gat all the addreses of this user with eg. AddressMapper()
       $user->set_addresses($addresses); // array of object of address just like User
       $users[] = $user;
     }
     return $users;
   }
}

الاستخدام في تحكم: قائمة الإجراء:

$userMP = new UserMapper();
$this->view->users = $userMP->__fatchAll();

أو إضافة / حفظ الإجراء:

$newUser = new User();
$newUser->set_name('somename');
$userMP = new UserMapper();
$userMP->save($newUser);
هل كانت مفيدة؟

المحلول

ZF يتيح لك تمديد Zend_Db_Table_Row فئة، وأبلغ فئة zend_db_table لاستخدامها دائما. بهذه الطريقة، يمكنك الوصول إلى كل من Zend_Db_Table_Row الميزات، مع إضافة المنطق الخاص بك في الأعلى.

يمكنك أن تفعل ذلك مثل هذا

class User_DbTable extends Zend_DB_Table_Abstract{
      protected $_name = "user"; // name of the table
      protected $_rowClass = "User"; // The name of your Zend_Db_Table_Row class
}

class User extends Zend_Db_Table_Row {

  public function set_name($_name){
    $this->name = $_name;
  }
  public function get_name(){
    return $this->name;
  }
  public function set_adresses($_addresses){
    $this->addresses = $_addresses;
  }
// and so on....
}

يمكنك أن ترى المزيد على Zend_Db_Table_Row هنا

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top