Maping column with another name

View: New views
3 Messages — Rating Filter:   Alert me  

Maping column with another name

by Chucre :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all,

I Have the table Person with have a column call "nm_person". In the object Zend_Db_Table_Row I want call the column "nm_person" by "name". Anyone knows how I make it?

--
Fernando Chucre - LPIC-1
http://www.horizontesdigitais.com

Re: Maping column with another name

by Neil Garb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Fernando

You need to create a new Row object, say My_Db_Table_Row_Person, and
put protected $_rowClass = 'My_Db_Table_Row_Person' in your table
class.

Then in your row class:

class My_Db_Table_Row_Person extends Zend_Db_Table_Row
{
 public function __get($columnName)
 {
  switch ($columnName) {
    case 'name':
     return $this->_data['nm_person'];
    default:
     return $this->_data[$columnName];
   }
 }
}

Now you can do $person->name.

Is there a quicker way?

- Neil

http://codecaine.co.za

On Tue, Jul 1, 2008 at 11:26 PM, Fernando chucre
<fernandochucre@...> wrote:
> Hello all,
>
> I Have the table Person with have a column call "nm_person". In the object
> Zend_Db_Table_Row I want call the column "nm_person" by "name". Anyone knows
> how I make it?
>
> --
> Fernando Chucre - LPIC-1
> http://www.horizontesdigitais.com

Re: Maping column with another name

by Chucre :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well,

I extend 3 Class:
  • Tuangr_Db_Table extends Zend_Db_Table_Abstract
  • Tuangr_Db_Table_Rowset extends Zend_Db_Table_Rowset_Abstract
  • Tuangr_Db_Table_Row extends Zend_Db_Table_Row_Abstract
this class cam be load by Zend_Loader.

So, if I've a table called Person and this table have the fallowing fields:
  • id_person
  • nm_person
  • description
I can now mapped using fallow way:

<?php
require_once "Tuangr/Db/Table.php";

class Person extends Tuangr_Db_Table {
    protected $_name = 'Person';
    protected $_primary = 'id_person';

    protected $_columnMap = array('name'=>'nm_person');
}

?>

and used like this:

<?php

$personModel = new Person();

$person = $personModel->find(1)->current
();
//lock the attribute name!!
$person->name = "Real Name";
$person->save();

$people = $personModel->fetchAll("name='Real Name'");

?>

I can't think in all problens this can throw, so please, test!!


2008/7/2 Neil Garb <neil.garb@...>:
Hi Fernando

You need to create a new Row object, say My_Db_Table_Row_Person, and
put protected $_rowClass = 'My_Db_Table_Row_Person' in your table
class.

Then in your row class:

class My_Db_Table_Row_Person extends Zend_Db_Table_Row
{
 public function __get($columnName)
 {
 switch ($columnName) {
   case 'name':
    return $this->_data['nm_person'];
   default:
    return $this->_data[$columnName];
  }
 }
}

Now you can do $person->name.

Is there a quicker way?

- Neil

http://codecaine.co.za

On Tue, Jul 1, 2008 at 11:26 PM, Fernando chucre
<fernandochucre@...> wrote:
> Hello all,
>
> I Have the table Person with have a column call "nm_person". In the object
> Zend_Db_Table_Row I want call the column "nm_person" by "name". Anyone knows
> how I make it?
>
> --
> Fernando Chucre - LPIC-1
> http://www.horizontesdigitais.com



--
Fernando Chucre - LPIC-1
http://www.horizontesdigitais.com

Tuangr.zip (6K) Download Attachment
LightInTheBox - Buy quality products at wholesale price