如何在 Zend Framework 中为 Db_Table 类编写自定义行类(扩展 Zend_Db_Table_Row)

发布于 2024-09-14 19:15:21 字数 3186 浏览 7 评论 0原文

我为 booksbook_sectionsusers (系统最终用户)设置了单独的 db_table 类。在图书表中有用于book_titlesection_id(book_section)、data_entered_user_id(谁输入了图书信息)。

转到此网址查看图像(我不允许发布图像,因为我是 stackoverflow 的新手) img685.imageshack.us/img685/9978/70932283.png

在系统后端我添加了一个表单来编辑现有书籍(从GET参数获取书籍ID并将相关数据填充到表单中) 。表单包含 book_titlebook_sectiondata_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'),
//....
);

编辑现有书籍时,

  1. 从 App_Model_Book 类上的 getBookData() 方法获取数据
  2. 如果提交编辑数据后表单数据有效,则使用 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

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魄砕の薆 2024-09-21 19:15:21

1)在您的 db_table 类中创建包含行类的字段,例如:

protected $_rowClass  = 'App_Model_Db_Books_Row';

并为父表添加引用映射:

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)在行类中,您必须为父表定义变量:

protected $section;
protected $user;

,我创建名为“load”的方法:

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

在这种情况下 构造函数我调用此方法:

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

作为结果:

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

您可以从引用表访问数据,例如:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文