具有联接表的PDO FETCH_CLASS [英] PDO FETCH_CLASS with joined tables

查看:93
本文介绍了具有联接表的PDO FETCH_CLASS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有2个php对象:

Let's say I have 2 php objects:

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

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

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

Every post has a unique constraint with 1 user in the database.

我想使用PDO的"FETCH_CLASS"方法将数据填充到"Post"对象中,该方法适用于所有"Post"属性,但是如何在"User"中填充属性?

I want to fill data into the "Post"-object with PDOs "FETCH_CLASS" method which works for all the "Post" attributes but how do I fill the attributes in "User"?

我的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 I fill my "Post"-class like this:

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

那么,为了填充其他类"User",我又该如何更改呢?

So how would I have to change that for also filling the other class "User"?

解决方案:

$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;

据我所知,

推荐答案

PDO中不直接支持PDO.通常,如果您需要根据查询结果创建复杂的对象图,那就是ORM的职责.

Theres no support for the directly in PDO as far as I'm aware. Typically if you need to create a complex object graph from the result of query thats the responsibility of an ORM.

如果您需要此功能,我建议您使用 Doctrine

If you need this functionality i wold recommend using Doctrine or Propel as opposed to writing something yourself. There are others too that may be lighter weight, but i have no experience with them.

我认为也许我误以为别人可能会误解这个问题.我认为真正的问题是如何访问联接的列,而不是必须如何从它们创建对象.

I think maybe i misunderstood the question as im sure others might. I think the real question was how to get access to the joined columns, not cessarially how to create an object from them.

在这种情况下,只需使用标准的arry fethc方法(如PDO::FETCH_ASSOCPDO::FETCH_NUMERICPDO::FETCH_BOTH),就会为您提供所有查询的列.

In that case simply using a standard arry fethc method like PDO::FETCH_ASSOC, PDO::FETCH_NUMERIC or PDO::FETCH_BOTH will give you all the columns you queried.

因此,如果要将其转换为对象图",则不必使用PDO::FETCH_CLASS进行手动操作.

So if you want to turn that into an "object graph" you have to do it manually not by using 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
   */
}

这篇关于具有联接表的PDO FETCH_CLASS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆