【问题标题】:How to write a custom row class (extends Zend_Db_Table_Row) for a Db_Table class in Zend Framework如何在 Zend Framework 中为 Db_Table 类编写自定义行类(扩展 Zend_Db_Table_Row)
【发布时间】:2010-08-14 06:33:40
【问题描述】:

我为 booksbook_sectionsusers(系统最终用户)设置了单独的 db_table 类。在 book table 中有 columns 用于 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 和 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() 方法保存数据

但我发现如果我创建了一个 custom Db_Table_Row(扩展 Zend_Db_Table_Row) 书桌类 book_section_name 和 data_entered_user_name 我可以使用 获取现有图书数据并保存 编辑后图书数据没有 创建新的 Book(db_table) 类并且不调用 updateBookData() 来保存更新的数据。但我不知道应该在自定义上编写哪个 代码 Db_Table_Row(扩展 Zend_Db_Table_Row) 类

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

如何编写自定义 db_table_row 类来创建包含数据的自定义行 为特定的 db_table 类形成 2 个连接表?

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

再次感谢。

【问题讨论】:

  • 请发布您的代码。如果我们能准确地看到你在做什么,那么为你提供最好的方法会更容易。没有看到它,我唯一能想到的就是使用 ->update($data,'id =1') 方法而不是再次创建本书的实例。
  • 抱歉,我是 zend framewok 和 stackoverflow 的新手。我用 2 张图片更新了它,但无法发布它,因为我是 stackoverflow 的新手,所以我添加了 url。再次感谢
  • 一个唯一教程是here

标签: zend-framework zend-db-table


【解决方案1】:

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2010-10-13
    相关资源
    最近更新 更多