du512053619 2011-02-06 17:13
浏览 44
已采纳

将集合检索方法添加到模型中

Hey, I'm looking for a bit of advice here, I have a simple model with the usual save, delete and find methods, but wanted to create a getAll method aswell, it feels wrong putting it in the model as its not related to the actual model. Which way should I design this?

My current model looks like this:

<?php
class User {

    $id;
    $name;
    $address;

    public function __construct() {
        // do some constructor stuff    
    }

    public function save() {
        // insert into....  
    }

    public function delete() {
        // delete from user where ....
    }

    public function find($id) {
        // select * from user where ...     
    }
}
?>

Any help would be much appreciated, Thanks

  • 写回答

3条回答 默认 最新

  • dongzhimin2231 2011-02-06 18:04
    关注

    Well, after you have methods for finding, creating and deleting users, there's nothing to worry. In my opinion, there are two approaches

    1) You can use your find() method in a special way, in order to get all users (a bit of a hack)
    2) You can create a public static method getAll() to do the job (I think it should be static, because getAll() is not related to a single instance of User object)

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大