我有三个模型(数据库表的表示),它们基本上具有 setter 和 getter 方法。我有一个具有操作方法的服务类。在一个服务类方法中,我正在编写一个连接两个表的查询。我想知道如何为查询结果创建单个对象。
例如:
我有两个模型文件 m1.php 和 m2.php
m1.php
class M1Model {
public $id;
public $name;
public function setM1ID($id) {
$this->id = $id;
}
public function setM1Name($name) {
$this->name = $name;
}
public function getM1ID() {
return $this->id;
}
public function getM1Name() {
$this->name;
}
}
m2.php
class M2Model {
public $id;
public $cat_name;
public $m1_id;
public function setM2ID($id) {
$this->id = $id;
}
public function setM2CatName($cat_name) {
$this->cat_name = $cat_name;
}
public function setM1ID($m1_id) {
$this->m1_id = $m1_id;
}
public function getM2ID() {
return $this->id;
}
public function getM2CatName() {
return $this->cat_name;
}
public function getM1ID() {
return $this->m1_id;
}
}
服务.php
include m1.php;
include m2.php;
class DataService {
function __construct() {
$m1 = new M1Model();
$m2 = new M2Model();
}
function getAllData() {
$query = "database query using join";
//Here I want to return object (Not generic object)
return $object;
}
}
从 getAllData(),我想返回单个对象和非通用对象。我该怎么做呢?