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);
}

这对我有用。以下是一些我想知道我应该做什么的事情。

我的属性现在像这个主对象(组织)一样工作,其属性由一次获取设置,然后对象设置为某些属性,这些属性是现有组织类的类型(数组)。您对类型转换(对象与对象中的数组)有何看法?我应该把什么逻辑放在哪里?

4

1 回答 1

0

我不知道为什么没有其他人没有回复你……我要试一试。让我们从一个小术语开始,类是类,类中的函数是方法。

有很多方法可以实现 MVC 系统,但我认为您的方法不是其中之一。您不创建模型类,而是创建一个称为组织的类,它是您的模型。一般的方法是

索引/列表显示/数据新/创建编辑更新销毁

您的控制器处理页面逻辑,目标是使其尽可能薄。我不确定你是否可以让控制器完全成为一个类......也许有可能,但我通常将我的作为平面文件。视图处理输出。你真的应该更多地阅读 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 回答