Simple Model
class Database {
   protected $_conn;
   public function __construct($connection) {
       $this->_conn = $connection;
   }
   public function ExecuteObject($sql, $data) {
       // stuff
   }
}
abstract class Model {
   protected $_db;
   public function __construct(Database $db) {
       $this->_db = $db;
   }
}
class User extends Model {
   public function CheckUsername($username) {
       // ...
       $sql = "SELECT Username FROM" . $this->usersTableName . " WHERE ...";
       return $this->_db->ExecuteObject($sql, $data);
   }
}
$db = new Database($conn);
$model = new User($db);
$model->CheckUsername('foo');


Everything that is business logic belongs in a model, whether it is a database query, calculations, a REST call, etc.


You can have the data access in the model itself (the MVC pattern doesn't restrict you from doing that), but it's easier to have a separate object that actually executes the database queries instead of having them being executed in the model directly: this will especially come in handy when unit testing (because of the easiness of injecting a mock database dependency in your model):


修改时间 2016-08-28

真诚赞赏,手留余香
赞赏
随机推荐
PHP ExcelReader使用教程
PSR规范
Node.js 文件系统
Windows下Cordova环境搭建及如何用android studio导入cordova项目生成apk
Node.js实现的简单爬虫
Base64
微信支付的一个问题
AI 制作的图标上传到 iconfont 以后丢失描边的解决办法
RESTful就是个骗局 (蛤蛤)
PM2 负载均衡