views:

66

answers:

1

I have separate db_table classes for books, book_sections and users (system end users). in book table has columns for book_title, section_id(book_section) , data_entered_user_id(who entered book info).

go to this url to see the image(I'm not allow to post images bacause I'm new to stackoverflow) img685.imageshack.us/img685/9978/70932283.png

in the backend of the system I added a form to edit existing book (get book id from GET param and fill the relevant data to the form). form has elements for book_title, book_section and data_entered_user.

to get exciting book data to "edit book form" I join book_section and user tables with book table to get book_section_name and username(of data_entered_user: read only- display on side bar)

go to this url to see the image(I'm not allow to post images bacause I'm new to stackoverflow) img155.imageshack.us/img155/2947/66239915.jpg

In class App_Model_Book extends Zend_Db_Table_Abstract

public function getBookData($id){
     $select = $this->select();
    $select->setIntegrityCheck(false);
    $select->from('book', array('id','section_id','data_entered_user_id',...));
    $select->joinInner('section','book.section_id = section.id',array('section_name' =>'section.name' ));
    $select->joinInner(array('date_entered_user' => 'user'),'book.date_entered_user_id = date_entered_user.id',array('date_entered_user_name' =>'date_entered_user.user_name' ));
    $select->where("book.id = ?",$id);
    return $this->fetchRow($select);
}

public function updateBookData($id,$title,$section_id,...)
{
   $existingRow = $this->fetchRow($this->select()->where('id=?',$id));
   $existingRow->title = $title;
   $existingRow->section_id = $section_id;
   //....
   $existingRow->save();
}

In Admin_BookController -> editAction()

$editForm = new Admin_Form_EditBook();
$id = $this->_getParam('id', false);
$bookTable = new App_Model_Book();
$book_data = $bookTable ->getBookData($id);
//set values on form to print on form when edit book
$editForm->book_title->setValue($book_data->title);
$editForm->book_section->setValue($book_data->section_name);
//........
//If form data valid
if($this->getRequest()->isPost() && $editForm->isValid($_POST)){
$bookTable = new App_Model_Book();
$bookTable ->updateBookData(
$id,
//get data from submitted form
$editForm->getValue('title'),
//....
);

when editing an exsiting book

  1. get data from getBookData() method on App_Model_Book class
  2. If form data is valid after submiting edited data, save data with updateBookData() method on App_Model_Book class

but I saw that if I created a custom Db_Table_Row(extends Zend_Db_Table_Row) class for book table with book_section_name and data_entered_user_name I can use it for get existing book data and save it after editing book data without creating new Book(db_table) class and without calling updateBookData() to save updated data.But I don't know which code I should write on custom Db_Table_Row(extends Zend_Db_Table_Row) class.

I think you can understand my problem, in simple

how to write a custom db_table_row class to create a custom row with data form 2 joined tables for a perticular db_table class ?

I'm new to zend framewok and to stackoverflow. forgive me if you confused with my first question.

Thanks again.

A: 

1) at your db_table class create field which contain row class, for example:

protected $_rowClass  = 'App_Model_Db_Books_Row';

and add reference map for parent tables:

protected $_referenceMap = array(
    'Section' => array(
        'columns'           => 'sectionId',
        'refTableClass'     => 'App_Model_Db_Sections',
        'refColumns'        => 'id'
    ),
    'User' => array(
        'columns'           => 'userId',
        'refTableClass'     => 'App_Model_Db_Users',
        'refColumns'        => 'id'
    )
);

2) at row class you must define variables for parent tables:

protected $section;
protected $user;

In such cases i create method called "load":

public function load()
{
    $this->section = $this->findParentRow('App_Model_Db_Sections');
    $this->user = $this->findParentRow('App_Model_Db_Users');
}

And in constructor i call this method:

public function __construct(array $config = array())
{
    parent::__construct($config);
    $this->load();
}

As a result after:

$book_data = $bookTable ->getBookData($id);

you can access data from reference table, for example:

$book_data->section->name = "Book Section";
$book_data->section->save();
Iscander