0

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

4

1 に答える 1

0

他の誰もあなたに返信していない理由がわかりません...私はそれを突き刺すつもりです. ちょっとした用語から始めましょう。クラスはクラスであり、クラス内の関数はメソッドです。

MVC システムを実装する方法はたくさんありますが、あなたの方法はその 1 つではないと思います。モデル クラスを作成するのではなく、モデルとなる組織というクラスを作成します。一般的な方法は、

インデックス/リスト 表示/データ 新規/作成 編集 更新 破棄

コントローラーはページロジックを処理します。目標は、ページロジックをできるだけ薄く保つことです。コントローラーを完全にクラスにすることができるかどうかはわかりません...可能かもしれませんが、通常はフラットファイルとして持っています。ビューは出力を処理します。あなたは本当にMVCをもっと読むべきです...それについてはたくさんの良い記事があります。PDO ラッパー クラスも調べる必要があります。http://www.imavex.com/php-pdo-wrapper-class/をお勧めします。また、シングルトン パターンがなぜ悪いのか、依存性注入がどのように良いのかについても読む必要があります。

とにかく....簡単なコードをいくつか紹介しましょう...それはきれいでも機能するわけでもありませんが、一般的なアイデアを理解する必要があります....関数型プログラミングで組織を表示したいようですおそらく org.php?id=123 のようなコードを作成します....より良いアプローチは REST セットアップ /org/view/123 です...コードは同じですが、コードの呼び出し方法が変わります...

したがって、コントローラーは次のようになります

$Org = new Org($dbClass);
if (isset($_REQUEST["id"]){
    $orgData = $Org->data($_REQUEST["id");
}

今、モデルは..

class Org
{
    public $dbClass; //stores db class

    public function __construct($dbClass){
         //its usually good form to type hint the db class interface
         $this->dbClass = $dbClass;
    }

    public function data($id){
        return $this->dbClass->run("SELECT * FROM org WHERE orgID = $id");
        // NOTE THIS IS VERY INSECURE 
    }
}

そして最後にあなたの見解

foreach ($orgData as $key=>$value){
echo $key." : ".$value."<br/>";
}
于 2013-07-12T20:17:39.973 に答える