doulei2100 2011-09-08 09:25
浏览 15
已采纳

您的函数在Zend_Model中的具体程度如何?

if someone could help me with a conceptual question it would be great: Suppose I have a model that deals with a table called Persons. Normally I would have a standard fetch function like this:

public function fetchPersonById($person_id)
{
$result = 0;

if ((int)$person_id > 0) {
    $select = $this->select()
                    ->from($this->_name, array('Id' => 'Person_Id',
                                            'Name' => 'Person_Name',
                                            'Age'  => 'Person_Age',
                                    'Sex'  => 'Person_Sex'));

                    ->where('Person_Id = ?', $person_id);

    $result = $this->fetchRow($select);
}
return $result;
}

Now suppose for some reason I need to fetch a person's Sex by it's Name.. and later on it's age by it's name. Would you them add different functions like:

public function fetchPersonSexByName($person_name)
{
// ...
    ->from($this->_name, array('Sex'  => 'Person_Sex');
    ->where('Person_Name = ?', $person_name); ...
// ...
}

and so on... After a while you could see yourself with thousands of short methods like this.. Are you guys that specific or you wether pull the whole record (fetchall) and than later in the code just keep the column you want to use? On this case wouldn't you be breaking the whole MVC because if I want to get someone's Sex my model (or whoeve is calling the function) would need to know the columns name in the database?

I also tought about doing something more generic like

public function $this->fetchColumnA_By_ColumnB_ColumnBValue($columnA_name,  
$columnB_name, $columnA_name)
{
    //...
}

And than have my short methods to be calling this more flexible column. So that I would have something like:

public function fetchPersonSexByName($person_name)
{
    //...
    $this->fetchColumnA_By_ColumnB_ColumnBValue('sex', 'name', 'martin');
}

Anyway.. How do you guys approach this probably common issue?

  • 写回答

2条回答 默认 最新

  • duandaotui5633 2011-09-08 09:36
    关注

    I would tend to do a version of your last generic example. The generic method would be protected (or even private) and your more specific (public) methods would call this. To avoid code repetition.

    However, I'm not sure how generic I would go. May be just...

    protected function _fetchColumnById($id, $column) {...}
    
    protected function _fetchColumnByName($name, $column) {...}
    

    ...but this would depend on the requirements.

    you could see yourself with thousands of short methods

    If you think you'll get to 1000's of requests, then it might be better to read the whole record(s) and cache this somehow?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用