1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

question about function fetchAllKeyed

Discussion in 'XenForo Development Discussions' started by typostudy, Apr 19, 2013.

  1. typostudy

    typostudy Member

        * Fetches results from the database with each row keyed according to preference.
        * The 'key' parameter provides the column name with which to key the result.
        * For example, calling fetchAllKeyed('SELECT item_id, title, date FROM table', 'item_id')
        * would result in an array keyed by item_id:
        * [$itemId] => array('item_id' => $itemId, 'title' => $title, 'date' => $date)
        * Note that the specified key must exist in the query result, or it will be ignored.
        * @param string SQL to execute
        * @param string Column with which to key the results array
        * @param mixed Parameters for the SQL
        * @return array
    public function fetchAllKeyed($sql$key$bind = array())
    $results = array();
    $i 0;
    $stmt $this->_getDb()->query($sql$bindZend_Db::FETCH_ASSOC);
            while (
    $row $stmt->fetch())
    $results[(isset($row[$key]) ? $row[$key] : $i)] = $row;
    Above code is from XenForo/Model.php, although there is comment, I still do not understand the usage of $key, Can anyone show me the examples with $key and without $key? In thay way, I can see the difference. Thanks.
  2. Jeremy

    Jeremy Well-Known Member

    Key will return a keyed array versus 0-x as the keys. So if we used this on, say, the user table and set the key to be the user id, you would be able to access my information using:

    $keyed = $this->fetchAllKeyed($sql, 'userid');

    Without fetchAllkeyed, my information could be stored at say, 74, if they removed 7 of the users before me from teh database. In BBCM, I could do a fetch all keyed on the tag name, and if a user had a tag of "spoiler", I could access it using $codes['spoiler'] versus a number.
  3. typostudy

    typostudy Member

    Got it. Thanks for your explantion.

Share This Page