Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-15-2007, 12:02 AM
Junior Member
 
Join Date: Oct 2007
Posts: 16
Default Transform column names into camelCase?

I'm working on a project where the column names are in uppercase, with words separated by underscores.
Is there any built in option in Zend_Db to automagically have these column names transformed into camelCase when you fetch stuff?
For instance, if I'm fetching an object it would be nice to have $obj->userId instead of $obj->USER_ID.
Edit: And by the way, I'm using the Pdo_Mssql driver.

Best Regards,
Troxy

Last edited by troxy : 10-15-2007 at 12:59 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-05-2007, 11:05 PM
Junior Member
 
Join Date: Oct 2007
Posts: 16
Default

Okay, so you have to extend the Zend_Db_Table and Zend_Db_Table_Row.
The extended table class might look like this:
PHP Code:
class MyTable extends Zend_Db_Table_Abstract {
    protected 
$_name 'TABLE_NAME';
    protected 
$_primary 'ID';
    protected 
$_rowClass 'MyTable_Row'// Tell it to use your custom extended row class

And in the extended row class is where the magic occurs:
PHP Code:
<?php
class MyTable_Row extends Zend_Db_Table_Row_Abstract {

    protected function 
_transformColumn($columnName) {
        if (!
is_string($columnName)) {
            require_once 
'Zend/Db/Table/Row/Exception.php';
            throw new 
Zend_Db_Table_Row_Exception('Specified column is not a string');
        }
        return 
strtoupper(preg_replace('/(?<=\\w)([A-Z])/''_\\1'$columnName)); // Transform the camelCase into UPPER_CASE
    
}
}
And then you can simply use it like this
PHP Code:
$table = new MyTable();
$result $table->find(10);
$row $result->current();
$value $row->fooBar// The actual name in the table is FOO_BAR 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-20-2008, 11:04 PM
Junior Member
 
Join Date: Aug 2008
Posts: 3
Default

Quote:
Originally Posted by troxy View Post
I'm working on a project where the column names are in uppercase, with words separated by underscores.
Is there any built in option in Zend_Db to automagically have these column names transformed into camelCase when you fetch stuff?
For instance, if I'm fetching an object it would be nice to have $obj->userId instead of $obj->USER_ID.
Edit: And by the way, I'm using the Pdo_Mssql driver.

Best Regards,
Troxy
I like this solution.. I just found this thread on google and I want to know what the general solution is for this problem?

The schema is defined using lower_case_underscore format, and the ZF standard is to use lowerCamelCase. Do we really have to resort to a hack like this? It's a nice hack, and I think we'll probably use it for my project, but I'm wondering if there's anything else out there.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-21-2008, 08:28 PM
Senior Member
 
Join Date: Jun 2008
Location: Florida
Posts: 108
Default

I don't consider this solution a "hack".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:26 PM.