【问题标题】:Standard for setting properties of existing object with PDO使用 PDO 设置现有对象属性的标准
【发布时间】:2013-07-12 16:27:11
【问题描述】:

我在模型部分有一个选择数据映射器的类,就我而言,我关心的是 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);
}

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

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

【问题讨论】:

    标签: php oop pdo


    【解决方案1】:

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

    实现 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/>";
    }
    

    【讨论】:

    • 我确实创建了一个模型类,它被称为模型(我上面的格式可能会令人困惑)。我的模型类有调用数据模型类的方法,在这种情况下是一个数据库类(其中有两个用于单独的 mysql 用户插入和更新)。我也使用 DOM 和平面文件。在数据库类中,我设置了属性 (PDO::FETCH_CLASS,'organization'),所以现在我的组织类已创建但尚未完全创建。模型类方法继续用与该组织(人)关联的数组填充我创建的组织,但我可以用人对象填充它们吗?
    • 抱歉耽搁了,我不清楚你在问什么。我上面的回答是我认为实现 MVC 的最佳方式。我不太了解您的方法,但我认为一旦您开始处理大量资源,它就不会成立。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    相关资源
    最近更新 更多