duanjie2940 2009-12-11 20:02
浏览 29
已采纳

too long

is there a better way to work with ZF useing the mappers, real life objects and table_objects.

This is how I do it with Zend Framework:

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

the user class -> user object:

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....
}

the 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;
   }
}

usage in controller: list action:

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

or add/save action:

$newUser = new User();
$newUser->set_name('somename');
$userMP = new UserMapper();
$userMP->save($newUser);
  • 写回答

1条回答 默认 最新

  • doudou_3636 2009-12-11 21:21
    关注

    ZF lets you extend the Zend_Db_Table_Row class, and tell your Zend_Db_Table class to always use it. This way, you can access all of the Zend_Db_Table_Row features, while adding your own logic on top.

    You can do that like this

    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....
    }
    

    You can see more on Zend_Db_Table_Row here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等