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


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-07-2008, 09:05 AM
stm stm is offline
Junior Member
 
Join Date: Jun 2008
Posts: 4
Default Zend_Db_Table result formatting

Is it possible to write a function in my Zend_Db_Table class which will do all the formatting on a column in the result?
E.g.
PHP Code:
$users = new Users();
$result $users->fetchAll();
// $result->created = 000000229233 (unix timestamp)
// $result->createdFormatted = Thurs 12 Feb 2008 (function from above) 
Any advice would be greatly appreciated.

Last edited by stm : 06-07-2008 at 09:08 AM. Reason: formatting
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-07-2008, 11:16 AM
Junior Member
 
Join Date: Nov 2007
Location: Elmshorn, Germany
Posts: 9
Send a message via ICQ to t-mow
Default

Maybe you should use a view-helper (create one by yourself)...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-10-2008, 11:46 PM
stm stm is offline
Junior Member
 
Join Date: Jun 2008
Posts: 4
Default

Thanks for the advice.

I could easily make a view helper but I was wondering if I could do it within the db_table function.

Can anyone confirm whether this is possible and give advice on a solution.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-04-2008, 11:26 PM
Junior Member
 
Join Date: Oct 2007
Posts: 27
Default

Use a custom Row class: Zend Framework: Documentation

You can override the __get method to call functions in your custom row class for properties that don't exist.

For example:

PHP Code:
//model
class User extends Zend_Db_Table_Abstract
{
    protected 
$_name 'user';
//use custom row class
    
protected $_rowClass 'UserRow';
}

//custom row class
class UserRow extends Zend_Db_Table_Row_Abstract {

    function 
__get($key){
    
        if(
method_exists($this$key)){
            return 
$this->$key();
        }
        
        return 
parent::__get($key);
    }
    
    function 
fullname(){
        
        return 
$this->forename ' ' $this->lastname;
        
    }


In the above example, you'd be able to access a fullname property of rows, even though there is no fullname column.

PHP Code:
$fullname $row->fullname
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-09-2008, 03:55 PM
Member
 
Join Date: Jun 2008
Location: Florida
Posts: 88
Default

What about doing it with SQL?

Code:
SELECT CONVERT(VARCHAR, created, 101) AS createdFormatted, ...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-15-2008, 05:58 PM
Junior Member
 
Join Date: May 2008
Posts: 12
Default

Another way would be to provide an init() method in your custom row class, to populate a member variable after the row is created.
Building on Davidoff's example:
PHP Code:
//custom row class
class UserRow extends Zend_Db_Table_Row_Abstract 
{
    public 
$fullname;

    public function 
init()
    {
        
$this->fullname $this->forename ' ' $this->lastname
    }

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 07:44 AM.