我在模型部分有一个选择数据映射器的类,在我的情况下,我关心 sql 数据库和设置现有对象的属性。
所以目前我这样做是为了让我的组织类
控制器
class controller
{
public function invoke()
{
$organization = $this->model->organization($id);
//pick view
}
}
模型
class model
{
public $database
//construct $database connection
public function organization($id)
{
$organization = $this->database->get_by_id($id, 'organizations', 'organization');
//$organization = new organization();
$organization->stakeholder = $this->database->join_one('stakeholders', 'stakeholder_category', 'stakeholder_id', '1', 'entity_id', $id);
$organization->document['document_affiliated_organization'] = $this->database->join_one('documents', 'authors', 'document_id', 1, 'author_id', $id);
$organization->document['document_audience_organization'] = $this->database->join_one('documents', 'document_audience', 'document_id', 1, 'audience_id', $id);
//people that are members of this organization
$organization->membership['organization_membership_person'] = $this->database->join_one('people', 'membership', 'member_id', 0, 'organization', $id);
//organizations that are members of this organization
$organization->membership['organization_membership_organization'] = $this->database->join_one('organizations', 'membership', 'member_id', 1, 'organization', $id);
//organizations that this organization is a member of
$organization->membership['member_of'] = $this->database->join_one('organizations', 'membership', 'organization', 1, 'member_id', $id);
$organization->event['event_affiliated_organization'] = $this->database->join_one('events', 'event_organizer', 'event', 1, 'organizer', $id);
return $organization;
}
}
class organization//extends model if I were to add the funcion discussed below
{
//database table values
public $model_type = 'Organization';
public $id;//organization table
public $name;//organization table
public $stakeholder;//organization table join with stakeholder table (array)
public $membership;//organization table join with membership table fetchall (array)
public $document;//organization table join with document table fetchall (array)
public $event;//organization table join with event table fetchall (array)
你对这个组织类中的一个功能有什么看法?
public function organization($id)
{
$organization = $this->database->get_by_id($id, 'organizations', 'organization');
//$organization = new organization();
$organization->stakeholder = $this->database->join_one('stakeholders', 'stakeholder_category', 'stakeholder_id', '1', 'entity_id', $id);
$organization->document['document_affiliated_organization'] = $this->database->join_one('documents', 'authors', 'document_id', 1, 'author_id', $id);
$organization->document['document_audience_organization'] = $this->database->join_one('documents', 'document_audience', 'document_id', 1, 'audience_id', $id);
//people that are members of this organization
$organization->membership['organization_membership_person'] = $this->database->join_one('people', 'membership', 'member_id', 0, 'organization', $id);
//organizations that are members of this organization
$organization->membership['organization_membership_organization'] = $this->database->join_one('organizations', 'membership', 'member_id', 1, 'organization', $id);
//organizations that this organization is a member of
$organization->membership['member_of'] = $this->database->join_one('organizations', 'membership', 'organization', 1, 'member_id', $id);
$organization->event['event_affiliated_organization'] = $this->database->join_one('events', 'event_organizer', 'event', 1, 'organizer', $id);
return $organization;//End of proposed function which I am not currently using
}
这是我上面调用的数据库类函数
public static function get_by_id($id,$table,$class)
{
try
{
$core = self::getInstance();
$core->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM $table where id = :id limit 1";
$statement = $core->dbh->prepare($sql);
$statement->bindParam(':id', $id, PDO::PARAM_INT);
if (!isset($class))
{
$class = '__CLASS__';
}
$statement->setFetchMode(PDO::FETCH_CLASS, $class);
$statement->execute();
$result = $statement->fetch();
可选地,我在想我可能会更好地执行 PDO::FETCH_INTO,$class
// Return results
return $result;
}
catch (PDOException $exception)
{
die($exception->getMessage());
}
}
public static function join_one($table1,$table2,$join_column_id,$person_or_organization,$where_column_id,$id)
{
$sql = "SELECT $table1.name, $table1.id
FROM $table1
JOIN $table2 ON $table1.id=$table2.$join_column_id
WHERE $table2.person_or_organization=$person_or_organization AND $table2.$where_column_id=$id";
return self::get_by_sql($sql);
}
这对我有用。以下是一些我想知道我应该做什么的事情。
我的属性现在像这个主对象(组织)一样工作,其属性由一次获取设置,然后对象设置为某些属性,这些属性是现有组织类的类型(数组)。您对类型转换(对象与对象中的数组)有何看法?我应该把什么逻辑放在哪里?