在这一节我们将介绍如何在控制器中使用模型来优化控制的代码
1,本节目标
了解模型,并且知道如何在控制器中使用模型
2,改进控制器
在之前实现编辑和删除功能的时候,我们是直接在控制器里面写SQL来直接删除的。这是不好的方法,有违控制器的职责。现在需要将所有的数据库操作都集中到模型中,然后控制器通过调用模型来实现对应的动作。
在模型中,定义删除,保存,和获得数据的方法。新的item.php模型的代码如下:
<?php
/**
* description:ZMAXBOOK组件 图书模型
* author:min.zhang
* Email:zhang19min88AT163.com
* Url:http://www.zmax99.com
* copyright:南宁市程序人软件科技有限责任公司保留所有权利
* date:2021-05-25
* version:v2.0.0
* @license GNU General Public License version 3, or later
*/
defined('_JEXEC') or die;
/**
* 图书的管理模型类.
*
* @since 2.0
*/
class zmaxbookModelItem extends JModelLegacy
{
public function getItem()
{
//STEP 1:获得需要编辑记录的ID
$app = JFactory::getApplication();
$id = $app->input->get("id",'0','INT');
//STEP 2:查询数据库,得到编辑记录的详情
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select("*")->from("#__zmaxbook_item")->where("id=".$db->quote($id));
$db->setQuery($query);
$item = $db->loadObject();
return $item;
}
//保存方法
public function save($id,$title,$author,$price)
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->update("#__zmaxbook_item")->set("title=".$db->quote($title));
$query->set("author=".$db->quote($author));
$query->set("price=".$db->quote($price));
$query->where("id=".$db->quote($id));
$db->setQuery($query);
$result = $db->execute();
return $result;
}
//删除方法
public function delete($cid)
{
$db = JFactory::getDBO();
$query ='DELETE FROM #__zmaxbook_item WHERE id IN('.implode(',',$cid).')';
$db->setQuery($query);
$result = $db->execute();
if($result)
{
return true;
}
else
{
$msg = $db->getError();
$this->setError($msg);
return false;
}
}
}
在控制器中调用模型的方法,实现相应的动作
items控制的代码如下:
<?php
/**
* description:ZMAXBOOK组件 Items控制器文件
* author:min.zhang
* Url:http://www.zmax99.com
* copyright:南宁市程序人软件科技有限责任公司保留所有权利
* date:2021-05-25
* version:v2.0.3
* @license GNU General Public License version 3, or later
*/
defined('_JEXEC') or die;
class zmaxbookControllerItems extends JControllerLegacy
{
/**
* 删除功能的实现
*
* @return void
*/
public function delete()
{
//STEP 1:得到提交的ID
$app = JFactory::getApplication();
$cid = $app->input->get('cid','','ARRAY');
$model = $this->getModel("item");
if($model->delete($cid))
{
//STEP 3:提示用户删除成功,并且完成重定向
$this->setRedirect("index.php?option=com_zmaxbook&view=items","操作成功");
}
else
{
//STEP 3:提示用户删除失败,并且完成重定向
$msg = $model->getError();
$this->setRedirect("index.php?option=com_zmaxbook&view=items",$msg,"error");
}
return true;
}
}
?>
item控制器的代码如下:
<?php
/**
* description:ZMAXBOOK组件 Item控制器文件
* author:min.zhang
* Email:zhang19min88AT163.com
* Url:http://www.zmax99.com
* copyright:南宁市程序人软件科技有限责任公司保留所有权利
* date:2021-05-20
* version:v2.0.3
* @license GNU General Public License version 3, or later
*/
defined('_JEXEC') or die;
class zmaxbookControllerItem extends JControllerLegacy
{
public function save()
{
//STEP 1:获得表单的数据
$app = JFactory::getApplication();
$title = $app->input->get("title","",'STRING');
$author = $app->input->get("author","",'STRING');
$price = $app->input->get("price","",'FLOAT');
$id = $app->input->get("id",'','INT');
//STEP 2:更新数据库
$model = $this->getModel("item");
$model->save($id,$title,$author,$price);
//STEP 3:重定向
$this->setRedirect("index.php?option=com_zmaxbook&view=items","保存成功");
return true;
}
public function edit()
{
//STEP 1:获得当前选中的项目的ID
$app = JFactory::getApplication();
$cids = $app->input->get("cid",'','ARRAY');
//STEP 2:检查有效性
if(empty($cids))
{
$this->setRedirect("index.php?option=com_zmaxbook&view=items","当前没有选择任何的项目,请选择需要编辑的项目","warning");
return true;
}
//STEP 3:重定向到编辑页面
$id = $cids[0];//如果选择多个,默认编辑第一个
$editLink = "index.php?option=com_zmaxbook&view=item&id=".$id;
$this->setRedirect($editLink);
return true;
}
}
?>
整体的思路就是在控制器中,通过调用控制器的getModel方法来获得对应的模型,getModel方法的第一个参数就是需要获得模型的名称。
$model = $this->getModel("item");
当获得模型后,就可以调用模型的方法来进行相应的数据库操作。代码上面没有什么特别的,注意是一种组织代码的方法。
另外,上面的代码大家需要注意的是模型有一个setEroor和getError的方法,通过这个方法,可以将模型的错误信息传递给控制器。
4,v2.0.5版本
实现模型后的完整版本代码下载:[下载文件:]com_zmaxbookv2.0.5_2021-05-25_for_j3x.zip

评论 (0)