本文介绍了PHP PDO :: FETCH_CLASS映射到所有小写属性,而不是camelCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例类映射到MySql表(如下所示).当我使用PDO :: FETCH_CLASS并执行* var_dump *时,我看到映射将转到所有小写属性.即'shortName'映射为'shortname',但应为'shortName'(驼峰大小写,类似于定义的属性);

I have the following sample class that maps to a MySql table (shown below). When I use PDO::FETCH_CLASS and do a *var_dump* I see that the mapping is going to all lowercase properties. i.e. 'shortName' maps to 'shortname', but should be 'shortName' (camel case, like the defined property);

为什么它映射到所有小写字母,我该怎么做才能将它们映射到来自SELECT的确切SQL名称?

Why is it mapping to all lowercase and what can I do to map them to the exact sql name coming from the SELECT?


class Category {
  public $id;
  public $fkId;
  public $shortName;
  public $longName;

  public static function getCategories() {

    $db = ezcDbInstance::get();

    $stmt = $db->prepare('SELECT
                                 id,
                                 fk_id AS `fkId`,
                                 short_name AS `shortName`,
                                 long_name As `longName`
                          FROM `Category`');

    $stmt->execute();

    return $stmt->fetchAll(PDO::FETCH_CLASS, "Category");
  }
}

推荐答案

更改此项:

$db = ezcDbInstance::get();

$stmt = $db->prepare('SELECT...

对此:

$db = ezcDbInstance::get();
$db->setAttribute( PDO::ATTR_CASE, PDO::CASE_NATURAL );

$stmt = $db->prepare('SELECT ...

这篇关于PHP PDO :: FETCH_CLASS映射到所有小写属性,而不是camelCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:03