1

我为booksbook_sectionsusers(系统最终用户)提供了单独的 db_table 类。在book 表中有book_title section_id ( book_section ) 、data_entered_user_id(输入书籍信息的人)的列。

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

在系统的后端,我添加了一个表单来编辑现有书籍(从 GET 参数获取书籍 ID 并将相关数据填写到表单中)。表单包含book_titlebook_sectiondata_entered_user的元素。

为了获得令人兴奋的书籍数据以“编辑书籍表单”,我将 book_section 和 user 表与 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() 方法保存数据

但我看到,如果我为具有 book_section_name 和 data_entered_user_name的书表创建了一个自定义 Db_Table_Row(extends Zend_Db_Table_Row) 类,我可以使用它来获取现有的书籍数据并在编辑书籍数据后保存它而不创建新的 Book(db_table) 类并且不调用 updateBookData () 来保存更新的数据。但我不知道应该在自定义 Db_Table_Row(extends Zend_Db_Table_Row) 类上编写哪个代码

我想你可以简单地理解我的问题

如何编写一个自定义 db_table_row 类来创建一个自定义行,其中包含一个特定 db_table 类的数据表单 2 连接表?

我是 zend framewok 和 stackoverflow 的新手。如果您对我的第一个问题感到困惑,请原谅我。

再次感谢。

4

1 回答 1

3

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();
于 2010-08-19T12:51:22.840 回答