如何在 Zend Framework 中为 Db_Table 类编写自定义行类(扩展 Zend_Db_Table_Row)
我为 books、book_sections 和 users (系统最终用户)设置了单独的 db_table 类。在图书表中有列用于book_title、section_id(book_section)、data_entered_user_id(谁输入了图书信息)。
转到此网址查看图像(我不允许发布图像,因为我是 stackoverflow 的新手) img685.imageshack.us/img685/9978/70932283.png
在系统后端我添加了一个表单来编辑现有书籍(从GET参数获取书籍ID并将相关数据填充到表单中) 。表单包含 book_title、book_section 和 data_entered_user 元素。
为了获得令人兴奋的图书数据以“编辑图书表单”,我将 book_section 和用户表与 book 表连接起来,以获取book_section_name 和用户名(data_entered_user 的:只读 - 显示在侧栏上) )
转到此网址查看图像(我不允许发布图像,因为我是 stackoverflow 的新手) img155.imageshack.us/img155/2947/66239915.jpg
在 App_Model_Book 类中扩展 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();
}
在 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'),
//....
);
编辑现有书籍时,
- 从 App_Model_Book 类上的 getBookData() 方法获取数据
- 如果提交编辑数据后表单数据有效,则使用 App_Model_Book 类上的 updateBookData() 方法保存数据
但我看到如果我创建了一个自定义 Db_Table_Row(扩展 Zend_Db_Table_Row) 书桌类 书本章节名称和 data_entered_user_name 我可以使用它 用于获取现有的图书数据并保存它 编辑图书数据后没有 创建新的 Book(db_table) 类,并且不调用 updateBookData() 来保存更新的数据。但我不知道应该在自定义上编写哪些代码 Db_Table_Row(扩展 Zend_Db_Table_Row) 类。
我想你可以简单地理解我的问题
如何编写自定义 db_table_row 类来创建包含数据的自定义行 为特定的 db_table 类形成 2 连接表?
我是 zendframewok 和 stackoverflow 的新手。如果您对我的第一个问题感到困惑,请原谅我。
再次感谢。
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
- get data from getBookData() method on App_Model_Book class
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)在您的 db_table 类中创建包含行类的字段,例如:
并为父表添加引用映射:
2)在行类中,您必须为父表定义变量:
,我创建名为“load”的方法:
在这种情况下 构造函数我调用此方法:
作为结果:
您可以从引用表访问数据,例如:
1) at your db_table class create field which contain row class, for example:
and add reference map for parent tables:
2) at row class you must define variables for parent tables:
In such cases i create method called "load":
And in constructor i call this method:
As a result after:
you can access data from reference table, for example: