drwpbrv668670 2013-10-09 19:22
浏览 34
已采纳

如何使用pdo作为getter来获取最佳数据

I'm hoping you can shed some light to my questions guys. What I need is basically the best practise of getting and assigning properties from database rows (instance getter). It is a pretty standart issue and I have know how it works in C#, but I'm unclear how to accomplish similar results in php and havent found any solution online which covers my needs.

So lets assume I have this simplified code:

Class User{
  private $Pdo;
  private $UserID;
  private $UserName;

  function GetUserName(){
    return $this->UserName;
    }

  function GetUserInfo(){
    // return user object with given user id, in this example just use "1"
    $Pdo = $this->Pdo;
    $Query = $Pdo->query('select * from User where UserID = 1');
    $Query->setFetchMode(PDO::FETCH_CLASS, "User", array($Pdo) ); // this is returning object
    $UserInfo = $Query->fetch();
    return $UserInfo;
    }

  }

Then when I wanted to get the user object I would call it like:

$User = new User($Pdo);
$UserInfo = $User->GetUserInfo();
echo $UserInfo->GetUserName();

This works, however I dont like to do it this way. One option would be to use static method so in the end I would end up with something like:

$UserInfo = User::GetUserInfo()

Which I suppose is called "Singleton" (edit: not a singleton:)) and is generally noted as bad practise.

My question is how it should look like? What I would like to have is this:

$UserInfo = new User($Pdo);
$UserInfo->GetUserInfo();
echo $UserInfo->GetUserName();

I know that in GetUserInfo method I can manually assign the values to current object ($this) such as:

function GetUserInfo(){
    // get data from db
    $this->UserName = "John"; // this value I will get from db
   }

However I would like to use FetchClass so I can get all the properties based on their names in one line and not assign them manually. I hope that you understand what my issue is :) I would love to hear your opinions of what is the best way of doing this.

Thank you very much for any input.

  • 写回答

1条回答 默认 最新

  • dongtun1209 2013-10-09 20:06
    关注

    One step in the direction to a best pratice and good design would be to separate your domain models and the persistence layer.

    So you are independent of the used database or could even replace the database with a webservice for example. Look at the Data Mapper pattern.

    So your User model would only consist of the properties + getters/setters and methods that use these properties in some way (business logic).

    class User
    {
        protected $UserID;
        protected $UserName;
    
        public function getUserId()
        {
            return $this->UserID;
        }
    
        public function setUserId($userId)
        {
            $this->UserID = userId;
            return $this;
        }
    
        ...
    
    }
    

    Your mapper holds the database connection and is responsible for saving/fetching the User object.

    class UserMapper
    {
        protected $_pdo;
    
        public function __construct($pdo)
        {
            $this->_pdo = $pdo;
        }
    
        public function getUserById($id)
        {
            // TODO: better use prepared statements!
            $query = $this->_pdo->query("select * from User where UserID = ".id);
            $query->setFetchMode(PDO::FETCH_CLASS, "User");
            return $query->fetch();
        }
    
        public function save(User $user)
        {
            // insert/update query
        }
    
        ...
    
    }
    

    You can use it like:

    $userMapper = new UserMapper($pdo);
    $user = $userMapper->getUserById(1);
    
    echo $user->getUserName();
    
    $user->setUserName('Steve');
    $userMapper->save($user);
    

    There are other similar patterns like Table Gateway pattern. But I prefer the data mapper because of the independecy of the data source.

    Look at the whole catalog from Martin Fowler: Catalog of Patterns of Enterprise Application Architecture

    Another useful thread: What is the difference between the Data Mapper, Table Data Gateway (Gateway), Data Access Object (DAO) and Repository patterns?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题