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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图