POD::FETCH_CLASS 会导致无限循环,而 PDO::FETCH_BOTH 则不会

发布于 2024-12-09 13:01:21 字数 1001 浏览 0 评论 0原文

我遇到了一个难以跟踪的错误,但我不确定是什么导致了这个错误。我有一个类 Property,我想使用名为 loadProperty() 的方法从表 property 中获取一个条目。此方法是单例类 (Registry) 的一部分。

public function loadProperty() {
    $this->load('model', 'property');
    $sth = $this->dbh->prepare("SELECT * FROM property WHERE subdomain = :subdomain LIMIT 1");
    $sth->setFetchMode(PDO::FETCH_CLASS, 'property');
    $data = array('subdomain' => $this->router->subdomain);

    try {
        $sth->execute($data);

        if ($sth->rowCount() == 1) {
            $this->property = $sth->fetch();
        } else {
            $this->property = null;
        }

    } catch (PDOException $exception) {
        // HANDLING EXCEPTION
    }
}

该方法的第一行加载模型。它只是查找类文件并使用 require_once 来要求它。

当我使用 PDO::FETCH_BOTH 而不是 PDO::FETCH_CLASS 时,所有这些工作正常。我的猜测是 PDO 正在幕后做一些我不知道的事情,但这会导致我的 loadProperty 方法被无限次调用。

我在这里忽略了什么?

I've come across a difficult to track bug, but I'm not sure what is causing this bug. I have a class Property and I want to fetch one entry form the table property with a method named loadProperty(). This method is part from a singleton class (Registry).

public function loadProperty() {
    $this->load('model', 'property');
    $sth = $this->dbh->prepare("SELECT * FROM property WHERE subdomain = :subdomain LIMIT 1");
    $sth->setFetchMode(PDO::FETCH_CLASS, 'property');
    $data = array('subdomain' => $this->router->subdomain);

    try {
        $sth->execute($data);

        if ($sth->rowCount() == 1) {
            $this->property = $sth->fetch();
        } else {
            $this->property = null;
        }

    } catch (PDOException $exception) {
        // HANDLING EXCEPTION
    }
}

The first line of the method loads the model. It just looks for the class file and requires it with require_once.

All this works fine when I use PDO::FETCH_BOTH instead of PDO::FETCH_CLASS. My guess is that PDO is doing some things behind the scenes that I am not aware of, but that cause my loadProperty method to be called an infinite number of times.

What am I overlooking here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2024-12-16 13:01:21

无限循环原来是由我自己的错误引起的——谁会想到呢。通过将 PDO 的获取模式设置为 PDO::FETCH_CLASSPDO 会尝试实例化 Property 的实例,这正是人们所期望的。但是,该模型在其构造函数方法中创建对 Registry 类的引用,从而导致调用 Registry 类的构造函数,其中包括 loadProperty > 方法如上所示。结果是无限循环。

The infinite loop turned out to be caused by an error of my own - who'd've thought. By setting PDO's fetch mode to PDO::FETCH_CLASS, PDO attempts to instantiate an instance of Property, which one might expect. However, the model creates a reference to the Registry class in its constructor method, causing the constructor of the Registry class to be invoked which includes the loadProperty method shown above. The result is an infinite loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文