扩展[Namespace] _Model_ [Table]类。这样安全吗?

问题描述 投票:1回答:1

我是php和zend框架的新手,并且非常不确定以下内容:

例如,我有两个表:

table1:box(id,height,width,length,weight) table2:标签(id,boxid,text,position)

labels.boxid - > box.id

(labels.boxid是primary_key box.id的foreign_key)

我为两个表生成了两个单独的标准Zend_Db_Table_Abstract + Model + Mapper类,其中包含所有set,get,fetchAll,fetchList方法。

问题:将Box模型扩展到LabeledBox模型类是否安全,如下所示:

class NS_Model_LabeledBox extends NS_Model_Box {
     protected $_labels;

     public function setLabels($objs)
     {
        $this->_labels=$objs;
        return $this;
     }

     public function getLabels()
     {
        return $this->_labels;
     }

     public function getMapper()
     {
        if (null === $this->_mapper) {
            $this->setMapper(new NS_Model_LabeledBoxMapper());
        }
        return $this->_mapper;
     }

}

以及像这样的Mapper类:

  class NS_Model_LabeledBoxMapper extends NS_Model_BoxMapper {
         public function fetchAll()
        {
            $resultSet = $this->getDbTable()->fetchAll();
            $entries   = array();

            foreach ($resultSet as $row) {
               $entry = new NS_Model_Box();
               ...setting box properties code...

               //fetching and setting labels for the box
               $labels = new NS_Model_Labels();

               $entry->setLabels(
                     $labels->fetchList("boxid='" . $row->id . "'") 
                     );
            }

            $entries[] = $entry;
        }
  }

这样我才能最终运行:

$labeledbox = new NS_Model_LabeledBox();
$entries  = $labeledbox->fetchAll();

foreach($entries as $entry)
{
   echo $entry->getId();
   echo $entry->getWeight();
       foreach($entry->getLabels() as $lables)
       {
          echo $lables->getText();
       }
}

这段代码对我来说很好。如果ZF的新版本发布任何可能影响这个“技巧”的重大更改(它仍然适用于OOP范例),我只是不想被抛在脑后。

php oop zend-framework design-patterns
1个回答
1
投票

你确实提出了一个关于击中一个移动目标的好点,到目前为止,Zend Framework就是这样。我在ZF 1.7.x上开始了两个不同的项目,并且由于ZF的变化已经重写了一些类。当然,这更像是希望与ZF的“稳定”版本保持同步,所以我没有必要这样做。

Zend Framework当然是一个快速移动的框架,在编写代码时需要考虑这一点,特别是因为有人谈到在2.0.x发布时打破向后兼容性。

© www.soinside.com 2019 - 2024. All rights reserved.