Zend Select with self join overwriting fields

前端 未结 3 1694

Posts and comments are stored in the same table. So to get each post and its comments we do this:

    $posts = $this->select()->setIntegrityCheck(false)
           


        
3条回答
  •  天涯浪人
    2021-01-27 11:49

    Zend does not make your preferred form easy, but it might be possible. Note, however, that you are asking the database server to do far more work than you actually want, because the post and user information are duplicated for each comment. Justin is correct that a second query is easier and probably faster. However, I can provide some pointers toward a solution.

    To start, consider what you would get by using the Zend_Db::FETCH_NUM fetch mode:

    Array(
        [0] => Array(
            [0] => 12
            [1] =>
            [2] => 1
            [3] => 84
            [4] => This is a post...,
            [5] => 12/this-is-a-post
            [6] =>
            [7] =>
            [8] => 2009-07-21 16:39:09
            [9] => 84
            [10] => John
            [11] => Doe
            [12] => john-doe
            [13] => uploads/userprofiles/0/84/pic84_14
            [14] => 15
            [15] => 12
            [16] => 1
            [17] => 79
            [18] => This is a comment...,
            [19] =>
            [20] =>
            [21] =>
            [22] => 2009-07-21 17:40:10
        ),
        [1] => Array(
            [0] => 12
            [1] =>
            [2] => 1
            [3] => 84
            [4] => This is a post...,
            [5] => 12/this-is-a-post
            [6] =>
            [7] =>
            [8] => 2009-07-21 16:39:09
            [9] => 84
            [10] => John
            [11] => Doe
            [12] => john-doe
            [13] => uploads/userprofiles/0/84/pic84_14
            [14] => 17
            [15] => 12
            [16] => 1
            [17] => 127
            [18] => This is another comment...,
            [19] =>
            [20] =>
            [21] =>
            [22] => 2009-07-20 10:31:26
        )
    )
    

    Then somehow you have to come up with the mapping of column numbers to table and column names:

    Array(
        [0] => post.idPost
        [1] => post.idTopic
        [2] => post.idGroup
        [3] => post.idUser
        [4] => post.postContent
        [5] => post.postUrl
        [6] => post.postVotes
        [7] => post.postScore
        [8] => post.date
        [9] => user.idUser
        [10] => user.fname
        [11] => user.lname
        [12] => user.profileUrl
        [13] => user.photoUrl
        [14] => comment.idPost
        [15] => comment.idTopic
        [16] => comment.idGroup
        [17] => comment.idUser
        [18] => comment.postContent
        [19] => comment.postUrl
        [20] => comment.postVotes
        [21] => comment.postScore
        [22] => comment.date
    )
    

    This is the part where it gets tricky, because the table part of that is strongly specific to the database interface, and not always possible. Because it is tied to the result set, the adapter will also be the best place to obtain it; that means hacking Zend classes, possibly by providing your own adapter class. Depending on your database, the information might come from:

    • PDO: PDOStatement->getColumnMeta
    • Mysqli: mysqli_fetch_field_direct

    Other adapters might not have a way to obtain the table name, unfortunately.

提交回复
热议问题