一度に複数のテーブルを更新するモジュール(4)BmEditAction.class.php
本記事では、html フォームから送られてきたデータを元に一度に複数のテーブルを更新するモジュールについて解説します。
対象は,Cubson でいちおうモジュールを作れる方です。
function _setupObject()
$id = $this->_getId(); $this->mObjectHandler =& $this->_getHandler(); $this->mObject =& $this->mObjectHandler->get($id); if ($this->mObject == null && $this->_isEnableCreate()) { $this->mObject =& $this->mObjectHandler->create(); } //kilica for tag table $this->mObjectHandler2 =& $this->mAsset->load('handler', "tag"); $this->mObject2 =& $this->mObjectHandler2->get('tag_id'); if ($this->mObject2 == null && $this->_isEnableCreate()) { $this->mObject2 =& $this->mObjectHandler2->create(); }
_getHandlerもテーブルごとに作ったほうが綺麗ですが,ここではもうそのまま書いてしまいました。
function &_setupActionForm()
$this->mActionForm =& $this->mAsset->create('form', "edit_bm"); $this->mActionForm->prepare(); //kilica $this->mActionForm2 =& $this->mAsset->create('form', "edit_tag"); $this->mActionForm2->prepare();
function getDefaultView()
//kilica if ($this->mObject == null or $this->mObject2 == null) { return BOOKMARK_FRAME_VIEW_ERROR; } $this->mActionForm->load($this->mObject); $this->mActionForm2->load($this->mObject2); return BOOKMARK_FRAME_VIEW_INPUT;
function executeViewInput(&$render)
$render->setTemplateName("bookmark_bm_edit.html"); $render->setAttribute('actionForm', $this->mActionForm); $render->setAttribute('actionForm2', $this->mActionForm2); $render->setAttribute('object', $this->mObject); $render->setAttribute('object2', $this->mObject2);
function execute()
//kilica if ($this->mObject == null or $this->mObject2 == null) { return BOOKMARK_FRAME_VIEW_ERROR; } if (xoops_getrequest('_form_control_cancel') != null) { return BOOKMARK_FRAME_VIEW_CANCEL; } $this->mActionForm->load($this->mObject); $this->mActionForm->fetch(); $this->mActionForm->validate(); //kilica $this->mActionForm2->load($this->mObject2); $this->mActionForm2->fetch(); $this->mActionForm2->validate(); if ($this->mActionForm->hasError() or $this->mActionForm2->hasError()) { return BOOKMARK_FRAME_VIEW_INPUT; } $this->mActionForm->update($this->mObject); //kilica transaction start $res1 = $this->mObjectHandler->db->queryF("begin;"); if($this->_doExecute($this->mObject) == 'BOOKMARK_FRAME_VIEW_ERROR){ return BOOKMARK_FRAME_VIEW_ERROR; } //kilica for Tag table. Set bm table key(bm_id). if($this->mObject->get('bm_id') > 0){ $this->mActionForm2->set('bm_id', intval($this->mObject->get('bm_id'))); }else{ $res4 = $this->mObjectHandler->db->queryF("rollback;"); return BOOKMARK_FRAME_VIEW_ERROR; } $this->mActionForm2->update($this->mObject2); if ($this->mObjectHandler2->insert($this->mObject2)) { $res2 = $this->mObjectHandler->db->queryF("commit;"); return BOOKMARK_FRAME_VIEW_SUCCESS; }else{ //if db update failed, rollback bm table and tag table. $res3 = $this->mObjectHandler->db->queryF("rollback;"); return BOOKMARK_FRAME_VIEW_ERROR; }
いろいろ突っ込みどころがありそうですが以上です。