モデル パーツにデータ マッパーを選択するクラスがあります。私の場合、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);
}
これは私にとってはうまくいきます。どうしようかなと思っているところをいくつか挙げてみます。
私のプロパティは現在、このメイン オブジェクト (組織) のように機能し、プロパティは 1 つのフェッチによって設定され、オブジェクトは既存の組織クラスの型 (配列) である特定のプロパティに設定されます。型キャスト (オブジェクトとオブジェクト内の配列) についてどう思いますか? どのロジックをどこに配置すればよいですか?