doutu6658 2013-05-27 09:54
浏览 25
已采纳

组合模型 - 在控制器中或使用新的PHP MVC层

Having an ActiveRecord style class in php, what is the correct way to pass a model to a view for a model that contains foriegn keys / realtionships to other models? Should my Controller instantiate the related classes (user_id, category_id) and then pass all the information to the view, or should I generate a new layer in between the model and controller that combines the two? In essence I guess I'm asking where the sql "join" queries should go?

In my database I have a table called Article which looks like,

+------------------+------------+------+-----+-------------------+-----------------------------+
| Field            | Type       | Null | Key | Default           | Extra                       |
+------------------+------------+------+-----+-------------------+-----------------------------+
| id               | int(11)    | NO   | PRI | NULL              | auto_increment              |
| user_id          | int(11)    | NO   | MUL | NULL              |                             |
| title            | char(30)   | NO   |     | NULL              |                             |
| subtitle         | char(60)   | YES  |     | NULL              |                             |
| time             | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| visible          | tinyint(1) | NO   |     | 1                 |                             |
| body             | mediumtext | YES  |     | NULL              |                             |
| category_id      | int(10)    | NO   | MUL | NULL              |                             |
| featured_mediaid | int(10)    | YES  |     | NULL              |                             |
+------------------+------------+------+-----+-------------------+-----------------------------+

I implemented this table in my article class:

Class Article extends DatabaseTable{

    public $id = null;
    public $title = null;
    public $subtitle = null;
    public $body = null;
    public $time = null;
    public $visible = null;
    public $category_id = null;
    public $user_id = null; 
    public $featured_mediaid = null;

/*.....methods for  Article*/

}

and a related table:

+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| id          | int(11)    | NO   | PRI | NULL    | auto_increment |
| name        | char(50)   | NO   | UNI | NULL    |                |
| description | char(50)   | NO   |     |         |                |
| visible     | tinyint(1) | NO   |     | 1       |                |
+-------------+------------+------+-----+---------+----------------+

and a category class:

Class Category extends DatabaseTable {

    public $id = null;
    public $name = null;
    public $description = null;
    public $visible = null;

    /*... Some methods here */
}

all my classes inherit from DatabaseTable which has the getById method:

Class DatabaseTable {
    // ...
    public static function getById($id){
        try {
            $connection = new PDO(Database::DB_DSN, Database::DB_USERNAME, Database::DB_PASSWORD);
            $sql = "SELECT * FROM " . strtolower(self::get_called()) . " WHERE id = :id";
            $statement = $connection->prepare($sql);
            $statement->bindValue(":id", (int) $id, PDO::PARAM_INT);
            $statement->execute();
            $row = $statement->fetch(PDO::FETCH_ASSOC);
            $connection = null; 
        }
        /*catch(PDOException $exception) {

        }*/
        catch(Exception $exception) {
            echo "An Unknown Exception Occured";
            echo "Calling Class [" . self::get_called() . "] " . "Method [" . __FUNCTION__ . "]" ;
            $connection = null;
            echo $exception->getMessage();
        }
        $class = self::get_called();
        return new $class($row);
    }

    public static function get_called(){
        return get_called_class() ; 
    }
    //...

}
  • 写回答

1条回答 默认 最新

  • dseax40600 2013-05-27 10:55
    关注

    MVC is enough. You don't need to create "new layer" anywhere.

    In most cases you only receive(or use a corresponding model method to fetch) and display data in the view side. i.e Controller initiates and pass the models to the view so that view calls the corresponding methods in the model to fetch data.

    Having said so, if a table is related to another table, and if you want to get all the data from both tables, you can create method in the "referring" model to do the join and return a combined result. Or if you like, you can do this in the controller side by using the two models and use a key from one table and give it to another model method (as an argument) and finally join/merge the outputs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配