11

假设我有 2 个 php 对象:

<?php
class Post {
    public $id;
    public $text;
    public $user_id;
}
?>

<?php
class User {
    public $id
    public $name
}
?>

每个帖子都有一个唯一的约束,数据库中有 1 个用户。

我想使用适用于所有“发布”属性的 PDO“FETCH_CLASS”方法将数据填充到“发布”对象中,但是如何填充“用户”中的属性?

我的 SQL 语句如下所示:

SELECT post.id, 
       post.text, 
       post.user_id, 
       user.id, 
       user.name 
FROM POST INNER JOIN User on post.user_id = user.id

谢谢!

更新:

ATM 我这样填写我的“邮政”类:

    $statement = $db -> prepare($query);
    $statement -> execute();
    $statement -> setFetchMode(PDO::FETCH_CLASS, 'Post');
    $posts = $statement -> fetchAll();

那么我将如何更改它以填充另一个类“用户”?

解决方案:

$statement = $db -> prepare($query);
$statement -> execute();
$posts = array();
while (($row = $statement->fetch(PDO::FETCH_ASSOC)) !== false) {
    $post           = new Post();
    $post->id       = $row['post_id'];
    $post->text     = $row['post_text'];
    $post->created  = $row['post_created'];
    $post->image    = $row['post_image'];
    $post->url      = $row['post_url'];
    $post->weight   = $row['post_weight'];
    $post->likes    = $row['post_likes'];
    $user           = new User();
    $user->id       = $row['user_id'];
    $user->nickname = $row['user_nickname'];
    $user->created= $row['user_created'];
    $user->locked   = $row['user_locked'];
    $post->user     = $user;
    $posts[] = $post;
}
return $posts;
4

5 回答 5

3

您可以尝试使用 __set 方法,如下所示:

<?php

include 'connection.php';

class Post {

    public $id;
    public $text;
    public $user;

    public function __construct() {
        $this->user = new User();
    }

    public function __set($name, $value) {

        if (array_key_exists($name, get_object_vars($this->user))) {
            $this->user->$name = $value;
        } else {
            $this->$name = $value;
        }
    }

}

class User {

    public $id;
    public $name;

}

$statement = $pdo->prepare("SELECT * FROM post "
        . "LEFT JOIN user "
        . "ON post.user_id = post.id");
$statement->execute();

$result = $statement->fetchAll(\PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, Post::class);
echo "<pre>";
var_dump($result);
于 2019-06-04T17:42:53.343 回答
2

据我所知,不支持直接在 PDO 中。通常,如果您需要从查询结果创建复杂的对象图,那是 ORM 的职责。

如果你需要这个功能,我会推荐使用DoctrinePropel,而不是自己写东西。还有其他可能更轻的重量,但我没有使用它们的经验。

编辑:

我想也许我误解了这个问题,因为我相信其他人可能会。我认为真正的问题是如何访问连接的列,而不是如何从它们创建对象。

在这种情况下,只需使用标准的 arry fethc 方法,例如PDO::FETCH_ASSOC, PDO::FETCH_NUMERICorPDO::FETCH_BOTH就会为您提供您查询的所有列。

所以如果你想把它变成一个“对象图”,你必须手动而不是使用PDO::FETCH_CLASS.

例如:

//$db is pdo:
// also notice im aliase the columns prefixing the name so that we can tell what belongs to
// post and what belongs to user, an alternative approach would be to use FETCH_NUMERIC,
// which just uses the column positions from the seelct statement as the keys
// so in this case post.id would be in the array as key 0, and user.name would be in the
// array as key 4
$stmt = $db->prepare('SELECT post.id as p_id, 
       post.text as p_text, 
       post.user_id as p_user_id, 
       user.id as u_id, 
       user.name as u_name
FROM POST INNER JOIN User on post.user_id = user.id');

$stmt->execute();

while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) !== false) {
   print_r($row);
   /* will output:
      Array (
         'p_id' => 'value'
         'p_text' => 'value'
         'p_user_id' => 'value'
         'u_id' => 'value',
         'u_name' => 'value'
      )
   So now you need to decide how to create your objects with the information returned
   */
}
于 2013-03-04T13:40:54.853 回答
1

不是对 OQ 的真正回应,而是因为它不断在 Google 上弹出(是的,我知道它已经有一年多了)。您会发现跳过循环并分别查询每个表会快得多。

    选择 post.id,
           post.text,
           post.user_id,
    FROM POST INNER JOIN 用户在 post.user_id = user.id
      $statement = $db -> 准备($query);
        $语句->执行();
        $statement -> setFetchMode(PDO::FETCH_CLASS, 'Post');
        $posts = $statement -> fetchAll();

    选择用户 ID,
           用户名
    FROM POST INNER JOIN 用户在 post.user_id = user.id
      $statement = $db -> 准备($query);
        $语句->执行();
        $statement -> setFetchMode(PDO::FETCH_CLASS, 'User');
        $users = $statement -> fetchAll();
    
于 2014-08-15T07:36:06.467 回答
0

如果您处理多个表,则可能使用 PDO::FETCH_NAMED 。或者使用 PDO::ATTR_FETCH_TABLE_NAMES。

于 2015-04-23T14:26:38.457 回答
0

我的解决方法:

function groupQueryJoinClasses(\PDOStatement $stmt, $joinInfo = [], $idProperty = 'id')
{
    $result = [];

    $records = $stmt->fetchAll();

    if ( !empty($joinInfo) ) {
        foreach ($records as $record) {
            if ( !isset($result[$record->$idProperty]) ) {
                $result[$record->$idProperty] = $record;
            }
            foreach ($joinInfo as $target => $classInfo) {
                $vars = get_object_vars($record);
                $class = new $classInfo['class']();
                foreach ($vars as $key => $value) {
                    $keyData = explode('.', $key);
                    if ( $keyData[0] == $classInfo['prefix']) {
                        $class->$keyData[1] = $value;
                        unset($result[$record->$idProperty]->$key);
                    }
                }
                if ( !is_array( $result[$record->$idProperty]->$target) ) {
                    $result[$record->$idProperty]->$target = [];
                }
                $targetArray = &$result[$record->$idProperty]->$target;
                $targetArray[] = $class;
            }
        }
    } else {
        $result = $records;
    }

    return $result;
}

function getModel($query, $data, $entryClass, $joinInfo, $idProperty = 'id') {
    $pdo = new PDO(...);
    $stmt = $pdo->prepare($query);
    $stmt->execute($data);

    $stmt->setFetchMode(\PDO::FETCH_CLASS, $entryClass);

    return groupQueryJoinClasses($stmt, $joinInfo , $idProperty);
}

// Sample request

$query =
    'SELECT
        u.id as "id",
        p.id as "Post.id",
        p.name as "Post.name"
    FROM `user` u
    LEFT JOIN `posts` p ON p.user_id = u.id
    where id = :id'
;
$data = [ ':id' => 1 ];

$joinInfo = [
    'posts' => [
        'class' => Post::class,
        'prefix'=> 'Post'
    ]
];
$flowRules = getModel($query, $data, User::class, $joinInfo);

也许对任何人来说都很有趣,或者也许有人会在这种方法中看到问题

于 2020-07-01T08:54:33.050 回答