doumao1887 2019-05-07 20:21
浏览 221

使用FETCH_CLASS将数据加载到类本身

I want to load data from the database (with PDO) and push it into a class. But it shall be generic, so I am using an abstract class which shall do all the basic stuff. And each sub-class (1 for each database table) shall be as reduced as possible.

At the same time, I want to keep control of the public properties and functions.

abstract class myTable {
    protected static $table
    protected static $classname

    public function __construct($id) {
        $pdo = new MyMagicPdoClass();

        $sql  = "SELCT * FROM ".self::$table." WHERE id = {$id}";
        $stmt = pdo->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_CLASS, self::$classname);

        // Here is my OBJECT in the CLASS-TYPE i want (POINT 1)
        // Show evidence for testing: 
        echo "<pre>",print_r($result),"</pre>";
    }

}


class User extends myTable {

    // for each accessable field: property-name = column-name --> as PRIVATE
    private $id;
    private $FirstName;
    private $LastName;
    private $Email;

    // avoid automated property creation (by PDO::FETCH_CLASS), the
    // use of "__set()"-method helps to filter all properties out.
    // Only properties (declared in this class) that match with the name of the column
    // will be initiated. If not doing this, ALL columns would be
    // automatically created as an PUBLIC property of the class. 
    // So no control via Gettes and Setters.
    public function __set($name, $value) {}

    public function __construct($id = null) {
        self::$classname = __CLASS__; // will be used in abstract class
        self::$table = "tblUser";     // will be used in abstract class

        // call onstructer of parent-class
        parent::__construct($id);

        /**
         * I GUESS, HERE SHOULD BE THE MAGIC (POINT 2)
         */
    }

    // SETTERS
    public function setFirstName($value) {
        $this->FirstName = $value;
    }
    [...]

    // GETTERS
    public function getFirstName() {
       return $this->FirstName;
    }
    [...]

    // SOME METHODS
    public function doWhatOnlyThisClassShallDo() {...}
}

Example of usage

$id = 234;
$user = new User($id);

echo $user->FirstName;                    // shall be forbidden
echo $user->getFirstName();               // shall be used instead
echo $user->doWhatOnlyThisClassShallDo(); // shall be possible

In the parent constructor, I have the OBJECT that I am looking for in $result. Now I want to have this as the result of my constructor (see Point 2) from the sub-class.

For sure I can now load manually each property in the constructor by the result of the abstract class, but as written above, I want to keep each sub-class as simple as possible. On top is that all the effort with FETCH_CLASS is useless as I simply allocate values to properties in the sub-class' constructor.

Any suggestions? Or am I using the wrong approach?

  • 写回答

1条回答 默认 最新

  • duangai9678 2019-05-07 22:44
    关注

    So you want to fetch the row from the database and populate each column value into the matching property? Also, I'm sure you want to perform safe and secure database queries. I think this is what you're looking for:

    abstract class myTable {
        protected static $table
        protected static $classname
    
        public function __construct($id) {
            $pdo = new MyMagicPdoClass();
    
            $sql  = "SELECT * FROM " . self::$table . " WHERE id = ?";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([$id]);
            $result = $stmt->fetch(\PDO::FETCH_ASSOC);
            foreach ($result as $k=>$v) {
                if (property_exists($this, $k)) {
                    $this->$k = $v;
                }
            }
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题