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.

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

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站