BFWSOA由于是面向服务治理的框架,所有控制器不能直接访问数据库,必须通过调用client内的方法对服务层进行调用,服务层再通过model对数据进行grud操作,首先我们在App\Cms\Model\目录下新建一个Model_Article.php,内容如下:
namespace App\Cms\Model; use Lib\Bfw; use Lib\BoModel; /** * @author Herry * 文章数据模型 */ class Model_Article extends BoModel { protected $_prikey = "id"; protected $_isview = false; private static $_instance; function __construct() { $this->_connarray = Bfw::Config("Db", "adminconfig"); parent::__construct(); } /** * 获取单例 * * @return Model_Article */ public static function getInstance() { if (! (self::$_instance instanceof self)) { self::$_instance = new self(); } return self::$_instance; } }
这里面在构造函数中载入数据库的配置文件,并且指定了prikey,Bfw::Config("Db", "adminconfig")是调用App\Cms\Config\目录下的Db.php里面的数组adminconfig,这里面指定了数据库连接方式与账号密码,所有的model里面如果没有指定table,那么他的tablename就是这个表前缀+modelname,TB_PRE在config文件中的定义,如果要另外定义tablename,那么就要在Db.php中增加map数组,如下:
$_config_arr['Db']['map'] = [ "Article" => "pre_article", ];这样就把tablename映射为pre_article表了,创建完Model后,我们还要创建Service,在App\Service\Cms\文件夹下新建一文件Service_Article.php
namespace App\Cms\Service; use Lib\Bfw; use Lib\BoService; use App\Cms\Model\Model_Article; /** * * @author Herry * 文章服务 */ class Service_Article extends BoService { protected $_model = "Article"; private static $_instance; /** * 获取单例 * * @return Service_Article */ public static function getInstance() { if (! (self::$_instance instanceof self)) { self::$_instance = new self(); } return self::$_instance; } function getkey() { return "123"; } }这里面的$_model指定了绑定的model,getkey指定了在分布式服务下的验证秘钥,服务写好后,我们要客户调用类,在App\Cms\Client\文件夹下新建一文件Client_Article.php
namespace App\Cms\Client; use Lib\BoClient; /** * @author Herry * 文章调用类 */ class Client_Article extends BoClient { protected $_service_remote = false; private static $_instance; /** * 获取单例 * * @return Client_Aricle */ public static function getInstance() { if (! (self::$_instance instanceof self)) { self::$_instance = new self(); } return self::$_instance; } }这里面有一个变量叫$_service_remote,这个将在分布式服务中用到,现在相关的类建好后,我们要创建数据库了
DROP TABLE IF EXISTS `cms_Article`; CREATE TABLE `cms_Article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` char(50) DEFAULT NULL, `content` text, `classname` char(50) DEFAULT NULL, `atime` int(11) DEFAULT NULL, `sortid` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;创建完数据库后,我们再修改一下我们的控制器
namespace App\Cms\Controler; use Lib\Bfw; use Lib\BoControler; use App\Cms\Client\Client_Article; class Controler_Article extends BoControler { /** * 文章添加 */ function AddData() { $this->OutCharset("utf-8"); if ($this->IsPost()) { $_formdata = $this->FormArray(array( "title", "classname", "content" ), false, "Article"); if ($_formdata['err']) { return $this->Error($_formdata['data']); } $_formdata['data']['atime'] = UNIX_TIME; $_insertdata = Client_Article::getInstance()->Insert($_formdata['data']); if ($_insertdata['err']) { return $this->Error($_insertdata['data']); } return $this->Alert("添加成功", array( array( "返回", Bfw::ACLINK("Article", "ListData"), "" ) )); } $this->Display(); } /** * 文章删除 */ function DelData($id) { $_deldata = Client_Article::getInstance()->Delete($id); if ($_deldata['err']) { return $t...
点击查看剩余70%
网友评论0