【问题标题】:PDO Fetch Class as a grouped collection object rather than as individual objects in an arrayPDO Fetch Class 作为一个分组的集合对象,而不是作为数组中的单个对象
【发布时间】:2018-08-07 14:25:19
【问题描述】:

我正在开发一个轻型 ORM,它使用基本模型从数据库表中获取所有行。我有多个模型(问题、答案、评论),它们使用每个模型中声明的受保护的 $table 属性扩展基本模型。这是我在基本模型中获取表行的方法:

public function all() {

    $query = "SELECT * FROM {$this->table}";
    $statement = $this->database->getConnected($this->connection)->prepare($query);
    $statement->setFetchMode(PDO::FETCH_CLASS, 'Core\Database\Collection');
    $statement->execute();
    return $statement->fetchAll();

}

当我转储结果时,我会得到类似的结果:

array(5) {
    [0]=>
    object(Core\Database\Collection)#31 (4) {
        ["id"]=>
        string(1) "1"
        ["title"]=>
        string(34) "What is your favourite video game?"
    }
    [1]=>
    object(Core\Database\Collection)#244 (4) {
        ["id"]=>
        string(1) "2"
        ["title"]=>
        string(28) "What is your favourite food?"
    }
}

我想知道是否有可能不是作为数组或单个对象返回,而是每个集合对象可以组合在一起作为一个大集合对象?

我知道我仍然可以使用 foreach 获得结果,但只是想让它成为一个实际的集合,而不仅仅是一个数组。

一个大集合对象

object(Core\Database\Collection) {
    all: {
        Namespace\Question {
            ["id"]=>
            string(1) "1"
            ["title"]=>
            string(34) "What is your favourite video game?"
        },
        Namespace\Question {
            ["id"]=>
            string(1) "2"
            ["title"]=>
            string(28) "What is your favourite food?"
        }
    }
}

【问题讨论】:

  • 你有一个一个大集合对象是什么样子的例子吗?
  • PDOStatement::fetchAll — Returns an array containing all of the result set rows 因此,您的解决方案是创建一个while 并将每条获取的记录添加到您的集合中。
  • 您的 Collection 类是否有某种 add() 方法?集合中的单个项目是否应该只是 StdClass 对象?
  • @AbraCadaver 用“集合对象”编辑了我的问题。目前我的集合类没有任何方法,因为我不确定如何获取结果集并按照我描述的方式进行分配。 Laravel 做了类似的事情,你可以在这个视频的 1:05ish 看到:laracasts.com/series/laravel-from-scratch-2017/episodes/7
  • 在该视频中,有一个带有 all 属性的对象,它是一个对象数组,类似于您所拥有的。

标签: php class oop object pdo


【解决方案1】:

我的观点是,使用当前的对象数组可能会更好。但是,您也许能够获取一个现有对象,并使用神奇的__set() 方法将值附加到命名数组:

class Collection {

    public function __set($name, $value) {
        $this->$name[] = $value
    }
}

据我所知PDO::FETCH_INTO 不能与fetchAll 一起使用,所以你需要一个while

$collection = new Core\Database\Collection;
$statement->setFetchMode(PDO::FETCH_INTO, $collection);
$statement->execute();
while($statement->fetch()){}
return $collection;

应该返回类似:

object(Core\Database\Collection)#1 (2) {
    ["id"]=>
    array(2) { 
        [0]=>string(1) "1",
        [1]=>string(1) "2"
    }
    ["title"]=>
    array(2) {
        [0]=>string(34) "What is your favourite video game?",
        [1]=>string(28) "What is your favourite food?"
    }
}

实际上,唯一的另一种可能性是拥有一个您在编辑示例中显示的对象,其属性是一个对象数组。你会这样做类似于你所拥有的。只需获取 someNamespace\Question 类的对象数组并将其分配给 Core\Database\Collection 类的另一个对象的属性:

$collection = new Core\Database\Collection;
$statement->setFetchMode(PDO::FETCH_CLASS, 'someNamespace\Question');
$statement->execute();
$collection->all = $statement->fetchAll();
return $collection;

应该返回类似:

object(Core\Database\Collection)#1 (1) {
    ["all"]=> array(2) {
        [0]=>
        object(someNamespace\Question)#1 (2) {
            ["id"]=>
            string(1) "1"
            ["title"]=>
            string(34) "What is your favourite video game?"
        }
        [1]=>
        object(someNamespace\Question)#2 (2) {
            ["id"]=>
            string(1) "2"
            ["title"]=>
            string(28) "What is your favourite food?"
        }
    }
}

我不知道 Laravel,而且视频中的对象/数组语法似乎是本土开发的,所以有点难以破译。

【讨论】:

  • 这与我所追求的非常相似!除了我不希望每个 ID 和 Title 组合成这样的数组外,我用一个粗略的例子来编辑我的问题,说明我想要实现的目标。 (很抱歉在发布我的问题后离开,我急于离开办公室但想在我离开之前发布我的问题,我很抱歉!)
  • 谢谢,我可以在早上试试这个,然后告诉你它是怎么回事,但如果它在你的编辑中返回类似的东西,那应该是我所追求的
  • 我认为您对使用标准数组的初步反应可能是我最好的选择。 Laravel 集合(scotch.io/tutorials/laravel-collections-php-arrays-on-steroids)非常复杂。我想我需要合并这样的东西来获得我想要的东西:github.com/italolelis/collections。因此,将每个 fetchAll 结果分配给这些集合之一,然后将该集合值分配给我自己的 Collection 类并返回它。对于我追求的东西来说似乎太过分了。不过感谢您的帮助,我比以前更了解 PDO 获取方法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多