Skip to content

Commit 3f89750

Browse files
committed
Make inflected column name to be in natural case instead of lowercase
The 'inflected_name' property of column metadata must be in the same letter case as column names seen in a response from a mysql server to work properly. The column names also must be in the natural case to make it compatible with mysql_* functions responses and to make model attributes in the phpactiverecord usage also in the natural case. To make it happen: * StandardInflector->variablize($s) no more lowercases column names. * Connection::$PDO_OPTIONS[PDO::ATTR_CASE] is set to PDO::CASE_NATURAL. After this change all usage of model attributes that correspond to columns having uppercase letters must be used as-is now. Example: Table `commutators` have a column `networkNode_id`. Before this change it should be written as: $commutator->networknode_id After this change it should be written as: $commutator->networkNode_id
1 parent 81b902b commit 3f89750

File tree

2 files changed

+120
-120
lines changed

2 files changed

+120
-120
lines changed

lib/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ abstract class Connection
7373
* @var array
7474
*/
7575
static $PDO_OPTIONS = array(
76-
PDO::ATTR_CASE => PDO::CASE_LOWER,
76+
PDO::ATTR_CASE => PDO::CASE_NATURAL,
7777
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
7878
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
7979
PDO::ATTR_STRINGIFY_FETCHES => false);

lib/Inflector.php

Lines changed: 119 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,119 @@
1-
<?php
2-
/**
3-
* @package ActiveRecord
4-
*/
5-
namespace ActiveRecord;
6-
7-
/**
8-
* @package ActiveRecord
9-
*/
10-
abstract class Inflector
11-
{
12-
/**
13-
* Get an instance of the {@link Inflector} class.
14-
*
15-
* @return object
16-
*/
17-
public static function instance()
18-
{
19-
return new StandardInflector();
20-
}
21-
22-
/**
23-
* Turn a string into its camelized version.
24-
*
25-
* @param string $s string to convert
26-
* @return string
27-
*/
28-
public function camelize($s)
29-
{
30-
$s = preg_replace('/[_-]+/','_',trim($s));
31-
$s = str_replace(' ', '_', $s);
32-
33-
$camelized = '';
34-
35-
for ($i=0,$n=strlen($s); $i<$n; ++$i)
36-
{
37-
if ($s[$i] == '_' && $i+1 < $n)
38-
$camelized .= strtoupper($s[++$i]);
39-
else
40-
$camelized .= $s[$i];
41-
}
42-
43-
$camelized = trim($camelized,' _');
44-
45-
if (strlen($camelized) > 0)
46-
$camelized[0] = strtolower($camelized[0]);
47-
48-
return $camelized;
49-
}
50-
51-
/**
52-
* Determines if a string contains all uppercase characters.
53-
*
54-
* @param string $s string to check
55-
* @return bool
56-
*/
57-
public static function is_upper($s)
58-
{
59-
return (strtoupper($s) === $s);
60-
}
61-
62-
/**
63-
* Determines if a string contains all lowercase characters.
64-
*
65-
* @param string $s string to check
66-
* @return bool
67-
*/
68-
public static function is_lower($s)
69-
{
70-
return (strtolower($s) === $s);
71-
}
72-
73-
/**
74-
* Convert a camelized string to a lowercase, underscored string.
75-
*
76-
* @param string $s string to convert
77-
* @return string
78-
*/
79-
public function uncamelize($s)
80-
{
81-
$normalized = '';
82-
83-
for ($i=0,$n=strlen($s); $i<$n; ++$i)
84-
{
85-
if (ctype_alpha($s[$i]) && self::is_upper($s[$i]))
86-
$normalized .= '_' . strtolower($s[$i]);
87-
else
88-
$normalized .= $s[$i];
89-
}
90-
return trim($normalized,' _');
91-
}
92-
93-
/**
94-
* Convert a string with space into a underscored equivalent.
95-
*
96-
* @param string $s string to convert
97-
* @return string
98-
*/
99-
public function underscorify($s)
100-
{
101-
return preg_replace(array('/[_\- ]+/','/([a-z])([A-Z])/'),array('_','\\1_\\2'),trim($s));
102-
}
103-
104-
public function keyify($class_name)
105-
{
106-
return strtolower($this->underscorify(denamespace($class_name))) . '_id';
107-
}
108-
109-
abstract function variablize($s);
110-
}
111-
112-
/**
113-
* @package ActiveRecord
114-
*/
115-
class StandardInflector extends Inflector
116-
{
117-
public function tableize($s) { return Utils::pluralize(strtolower($this->underscorify($s))); }
118-
public function variablize($s) { return str_replace(array('-',' '),array('_','_'),strtolower(trim($s))); }
119-
}
1+
<?php
2+
/**
3+
* @package ActiveRecord
4+
*/
5+
namespace ActiveRecord;
6+
7+
/**
8+
* @package ActiveRecord
9+
*/
10+
abstract class Inflector
11+
{
12+
/**
13+
* Get an instance of the {@link Inflector} class.
14+
*
15+
* @return object
16+
*/
17+
public static function instance()
18+
{
19+
return new StandardInflector();
20+
}
21+
22+
/**
23+
* Turn a string into its camelized version.
24+
*
25+
* @param string $s string to convert
26+
* @return string
27+
*/
28+
public function camelize($s)
29+
{
30+
$s = preg_replace('/[_-]+/','_',trim($s));
31+
$s = str_replace(' ', '_', $s);
32+
33+
$camelized = '';
34+
35+
for ($i=0,$n=strlen($s); $i<$n; ++$i)
36+
{
37+
if ($s[$i] == '_' && $i+1 < $n)
38+
$camelized .= strtoupper($s[++$i]);
39+
else
40+
$camelized .= $s[$i];
41+
}
42+
43+
$camelized = trim($camelized,' _');
44+
45+
if (strlen($camelized) > 0)
46+
$camelized[0] = strtolower($camelized[0]);
47+
48+
return $camelized;
49+
}
50+
51+
/**
52+
* Determines if a string contains all uppercase characters.
53+
*
54+
* @param string $s string to check
55+
* @return bool
56+
*/
57+
public static function is_upper($s)
58+
{
59+
return (strtoupper($s) === $s);
60+
}
61+
62+
/**
63+
* Determines if a string contains all lowercase characters.
64+
*
65+
* @param string $s string to check
66+
* @return bool
67+
*/
68+
public static function is_lower($s)
69+
{
70+
return (strtolower($s) === $s);
71+
}
72+
73+
/**
74+
* Convert a camelized string to a lowercase, underscored string.
75+
*
76+
* @param string $s string to convert
77+
* @return string
78+
*/
79+
public function uncamelize($s)
80+
{
81+
$normalized = '';
82+
83+
for ($i=0,$n=strlen($s); $i<$n; ++$i)
84+
{
85+
if (ctype_alpha($s[$i]) && self::is_upper($s[$i]))
86+
$normalized .= '_' . strtolower($s[$i]);
87+
else
88+
$normalized .= $s[$i];
89+
}
90+
return trim($normalized,' _');
91+
}
92+
93+
/**
94+
* Convert a string with space into a underscored equivalent.
95+
*
96+
* @param string $s string to convert
97+
* @return string
98+
*/
99+
public function underscorify($s)
100+
{
101+
return preg_replace(array('/[_\- ]+/','/([a-z])([A-Z])/'),array('_','\\1_\\2'),trim($s));
102+
}
103+
104+
public function keyify($class_name)
105+
{
106+
return strtolower($this->underscorify(denamespace($class_name))) . '_id';
107+
}
108+
109+
abstract function variablize($s);
110+
}
111+
112+
/**
113+
* @package ActiveRecord
114+
*/
115+
class StandardInflector extends Inflector
116+
{
117+
public function tableize($s) { return Utils::pluralize(strtolower($this->underscorify($s))); }
118+
public function variablize($s) { return str_replace(array('-',' '),array('_','_'),trim($s)); }
119+
}

0 commit comments

Comments
 (0)