Horde_Db_Adapter_Base::selectAssoc PHP Method

selectAssoc() public method

selectAssoc("SELECT id, name FROM companies LIMIT 3") => [1 => 'Ford', 2 => 'GM', 3 => 'Chrysler']
public selectAssoc ( string $sql, mixed $arg1 = null, string $arg2 = null ) : array
$sql string SQL statement.
$arg1 mixed Either an array of bound parameters or a query name.
$arg2 string If $arg1 contains bound parameters, the query name.
return array
    public function selectAssoc($sql, $arg1 = null, $arg2 = null)
    {
        $result = $this->selectAll($sql, $arg1, $arg2);
        $values = array();
        foreach ($result as $row) {
            $values[current($row)] = next($row);
        }
        return $values;
    }

Usage Example

示例#1
0
文件: Sql.php 项目: kossamums/horde
 /**
  * Returns the attribute values and names of a ticket.
  *
  * @param integer|array $ticket_id  One or more ticket IDs.
  *
  * @return array  If requesting a single ticket, an attribute name =>
  *                attribute value hash. If requesting multiple tickets, a
  *                list of hashes with ticket ID, attribute ID, attribute
  *                name, and attribute value.
  * @throws Whups_Exception
  */
 public function getTicketAttributesWithNames($ticket_id)
 {
     if (is_array($ticket_id)) {
         // No need to run a query for an empty array, and it would result
         // in an invalid SQL query anyway.
         if (!count($ticket_id)) {
             return array();
         }
         try {
             $attributes = $this->_db->selectAll('SELECT ticket_id AS id, d.attribute_name, ' . 'a.attribute_id, a.attribute_value ' . 'FROM whups_attributes a INNER JOIN ' . 'whups_attributes_desc d ' . 'ON (d.attribute_id = a.attribute_id)' . 'WHERE a.ticket_id IN (' . str_repeat('?, ', count($ticket_id) - 1) . '?)', $ticket_id);
         } catch (Horde_Db_Exception $e) {
             throw new Whups_Exception($e);
         }
         $attributes = $this->_fromBackend($attributes);
         foreach ($attributes as &$ticket) {
             if (is_string($ticket['attribute_value']) && defined('JSON_BIGINT_AS_STRING')) {
                 $ticket['attribute_value'] = json_decode($ticket['attribute_value'], true, 512, constant('JSON_BIGINT_AS_STRING'));
             } else {
                 try {
                     $ticket['attribute_value'] = Horde_Serialize::unserialize($ticket['attribute_value'], Horde_Serialize::JSON);
                 } catch (Horde_Serialize_Exception $e) {
                 }
             }
         }
     } else {
         try {
             $attributes = $this->_db->selectAssoc('SELECT d.attribute_name, a.attribute_value ' . 'FROM whups_attributes a INNER JOIN ' . 'whups_attributes_desc d ' . 'ON (d.attribute_id = a.attribute_id)' . 'WHERE a.ticket_id = ? ORDER BY d.attribute_name', array((int) $ticket_id));
         } catch (Horde_Db_Exception $e) {
             throw new Whups_Exception($e);
         }
         $attributes = $this->_fromBackend($attributes);
         foreach ($attributes as &$attribute) {
             try {
                 $attribute = Horde_Serialize::unserialize($attribute, Horde_Serialize::JSON);
             } catch (Horde_Serialize_Exception $e) {
             }
         }
     }
     return $attributes;
 }