简体   繁体   English

PHP MYSQL构建父模型

[英]PHP MYSQL Building Parent Model

Is it common and\\or wise to write a generic insert function for a parent model that child models will inherit from? 为子模型将继承的父模型编写通用的插入函数是常见的还是明智的做法? I am working with CodeIgniter\\PHP on a very simple CRUD app but this question is really language\\framework independent. 我正在一个非常简单的CRUD应用程序上使用CodeIgniter \\ PHP,但是这个问题实际上与语言\\框架无关。 Inheriting a read function that reads(selects) one value works very well and feels right: Parent Model 继承一个读取(选择)一个值的read函数,效果很好,感觉不错:父模型

public function read($columnSelect,$columnCondition,$value) {
    $query = "SELECT $columnSelect FROM $tableName WHERE $columConditionn=?";
    $res = $this->db->query($query,$columnName);
    if ($res->result()->num_rows()>0)
        return $res->result();
    else 
        return false;

this can allow you to retrieve user id's , emails , check for existence of certain columns etc, definitely something I feel will be useful in most or all my models so it's a good idea to write that function in the parent model. 这可以让您检索用户ID,电子邮件,检查某些列是否存在等,绝对可以肯定的是,在我的大多数或所有模型中,我都会觉得有用,因此最好在父模型中编写该函数。 All the read functions that are more complicated will obviously be specific to each model (eg queries that read more than one value etc) Now what about insert? 所有更复杂的读取函数显然将特定于每种模型(例如,读取多个值的查询等)那么插入呢? I can't think of any generic insert besides writing functions to insert one value, two values etc or writing an 'ugly' function to will get an array of parameters and build the insert query according to the array, eg: 除了编写用于插入一个值,两个值等的函数或编写“ ugly”函数以获取参数数组并根据该数组构建插入查询之外,我无法想到任何通用的插入方法,例如:

        public function genericInsert($columnList,$valueList) {

              $query = " INSERT INTO $tableName(
                        //now code that parses columnList and adds ','
                       //and then code that adds VALUES(..) in the same manner

This definitely doesn't feel like a "pattern" to me and feels wrong. 对我来说,这绝对不像是一种“模式”,而且感觉不对。 Am I overlooking something? 我在俯视什么吗? What do you guys do for your inserts, is it specific to each model or do you try to figure a way to inherit from a parent model? 你们为插入物做什么?它是特定于每个模型的吗?还是您尝试找出从父模型继承的方法? And also, if you will, what are functions do you find yourself putting in your parent models? 而且,如果愿意,您会发现自己在父模型中添加了哪些函数? (updates,deletes etc)? (更新,删除等)?

This is what my parent model looks like so far: 到目前为止,这是我的父模型:

class MY_Model extends CI_Model {
    public $tableName;

    public function __construct($tableName) {
        parent::__construct();
        $this->tableName = $tableName;

    }

    public function read($columnSelect,$columnCondition,$value) {
        $query = "SELECT $columnSelect FROM $tableName WHERE $columConditionn=?";
        $res = $this->db->query($query,$columnName);
        if ($res->result()->num_rows()>0)
            return $res->result();
        else 
            return false;
    }
}

Why reinvent the wheel ? 为什么要重新发明轮子 Codeigniter's standard db class can already do everything that the parent models you want to create will do. Codeigniter的标准db类已经可以完成您要创建的父模型可以完成的所有工作。 It would've made sense if you're writing your own db wrapper class and not working with CI, but CI already offers those features and can be customized/extended however you want. 它所做的,如果你写你自己的分贝包装类,而不是与CI的工作,但CI已经提供了这些功能,可定制/扩展不过你想要的感觉。

讨厌回答我自己的问题,但这就是我在寻找的http://thephpcode.com/blog/codeigniter/a-smart-codeigniter-model

Just some advice, be very careful when creating queries like this. 只是一些建议,在创建这样的查询时要非常小心。 I don't see any escaping taking place in your code, leaving your code open for SQL injection. 我看不到您的代码中发生了任何转义,使您的代码可以进行SQL注入。

If, for example, $columnSelect becomes; 例如,如果$ columnSelect变为;

"x'; DROP TABLE members; --"

Your query becomes: 您的查询变为:

"SELECT 'x'; DROP TABLE members; -- FROM $tableName WHERE $columConditionn=?";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM