Let's say I have a Model associated to a table with a column called title. I understand that I can do a $model->setTitle("Hello"); to set a value to this field. I can probably do the same for reading like so ``$model->getTitle();`.

An example would be:

$blogpost = Mage::getModel('weblog/blogpost');
    $blogpost->setTitle('Code Post!');
    $blogpost->setPost('This post was created from code!');

However, what if the column name was title_name? I could do it with $this->getTitleName() and it worked.

So, I was thinking, if I could use getTitleName() for a column called title_name, then what if I had another column called titleName in the database? How is Magento going to decide between titleName and title_name? How is Magento going to know which column I am referring to?

  • This is interesting, but I wouldn't give hard time to Magento by creating titleName and title_name column name, I would make them unique within a table. – Adarsh Khatri May 25 '16 at 3:40
  • @AdarshKhatri haha... yea, definitely. I guess I was just curious. :P – xenon May 25 '16 at 3:52

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];
    $result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
    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')


Convention is to use lower_snake_case for columns and CamelCase in the getters and setters. Best follow these conventions to not run into weird problems.

It's possible to use getData($columName) and setData($columnName, $value) to access arbitrary column names but I highly recommend following the convention.

(it's also possible to access title_name with getTitle_name() but don't do everything that is possible just because you can. This is not the intended usage!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.