【发布时间】:2015-08-21 09:05:42
【问题描述】:
我的“mvc_posts”表:
- id (int)
- 标题(varchar)
- CATEGORY (varchar) -> 示例:1,2,3
- 网址(varchar)
我的“mvc_categories”表:
- id (id)
- 名称(varchar)
我的帖子模型类:
class Post extends Database{
protected $table = 'mvc_posts';
public function __construct(){
$this->db = new Database;
}
public function viewByUrl($url){
$obj = array();
$where = array('url' => $url);
$obj = $this->db->select("*", $this->table, $where);
if($obj == true){
$obj->category_name = $this->category->getName($obj->category);
return $obj;
}
}
}
我的 Category 模型类:
class Category extends Database{
protected $table = 'mvc_categories';
public function __construct(){
$this->db = new Database;
}
public function getName($category){
$categories = explode(",", $category);
$category_name = "";
foreach($categories as $key => $value){
$where = array('id' => $value);
$category = $this->db->select("*", $this->table, $where);
$category_name .= $category->name.', ';
}
return rtrim($category_name, ', ');
}
}
如何在 Post 类中使用 Category 类的 getName() 方法?
【问题讨论】:
标签: php model-view-controller model relationship