dongyan7950 2012-08-08 07:38
浏览 56
已采纳

使用PDO :: FETCH_CLASS从数据库设置自己的参数

I have created a class user which reflects row in a user table. There is one method populate in which I am fetching values from database based on primary key and try to store it in self object. I can do it by getting all variables and setting them one by one per field. Is there any way to do it using PDO::FETCH_CLASS or any other simple method ?

If I try to store it using $this = $stmt->fetch( PDO::FETCH_CLASS ) it gives Fatal error: Cannot re-assign $this in /var/www/html/php/classes.php on line 51 error.

I don't want to use ORM style tools and want to develop the class by my own.

  • 写回答

1条回答 默认 最新

  • douhuilin1152 2012-08-08 07:45
    关注

    Have you tried something like list($this->a, $this->b, $this->c) = array((...)), where array is (probably) your fetched result? Because you cannot assignt $this variable, only refernce it's fields and methods.

    Another solution is to implement ArrayAccess interface and store data in underlying array.

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

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义