That's a very specific case so let me clear that up for you.
The magic getters/setters are just wrappers for getData/setData
methods.
So in your case:
title_name
can be retrieved by using getData('title_name')
titleName
can be retrieved by using getData('titleName')
However, if you call getTitleName()
it will automatically retrieve the title_name
column.
Here's why.
Those magic getters/setters are defined in the Varien_Object
class as well as two other magic methods has
and uns
:
public function __call($method, $args)
{
switch (substr($method, 0, 3)) {
case 'get' :
//Varien_Profiler::start('GETTER: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method,3));
$data = $this->getData($key, isset($args[0]) ? $args[0] : null);
//Varien_Profiler::stop('GETTER: '.get_class($this).'::'.$method);
return $data;
case 'set' :
//Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method,3));
$result = $this->setData($key, isset($args[0]) ? $args[0] : null);
//Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method);
return $result;
case 'uns' :
//Varien_Profiler::start('UNS: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method,3));
$result = $this->unsetData($key);
//Varien_Profiler::stop('UNS: '.get_class($this).'::'.$method);
return $result;
case 'has' :
//Varien_Profiler::start('HAS: '.get_class($this).'::'.$method);
$key = $this->_underscore(substr($method,3));
//Varien_Profiler::stop('HAS: '.get_class($this).'::'.$method);
return isset($this->_data[$key]);
}
throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
}
As you can see the _underscore()
method is called on the attribute of the method called.
protected function _underscore($name)
{
if (isset(self::$_underscoreCache[$name])) {
return self::$_underscoreCache[$name];
}
#Varien_Profiler::start('underscore');
$result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
#Varien_Profiler::stop('underscore');
self::$_underscoreCache[$name] = $result;
return $result;
}
So in your case, the code that runs would be:
strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", "TitleName"))
Which returns title_name
So getTitleName()
will always get you this column and thus the only way to get titleName
is by calling getData('titleName')
titleName
andtitle_name
column name, I would make them unique within a table. – Adarsh Khatri May 25 '16 at 3:40