PHP code example of windwork / model

1. Go to this page and download the library: Download windwork/model library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

windwork / model example snippets


// 启用应用模块
文件夹为:app/{$mod}/model/
命名空间:app\{$mod}\model
// 不启用模块
文件夹为:app/model/
命名空间:app\model

类命名为:XxxModel,类名以大写开头,Model作为后缀,使用驼峰命名规则。
文件名为:XxxModel.php,类名后面加.php,大小写敏感;
需要继承:需要数据库读写的模型继承\wf\model\Model类,实现业务逻辑但不需要数据存取的继承\wf\model\Base类;
对应表类:protected $table = '数据表名称';

$user = new \app\user\model\UserModel();

// 加载uid=1的用户信息
if($user->setPkv(1)->load()) {
    // 模型加载一条记录后,自动映射到属性,我们可以通过属性来获取表中对应字段的值
    echo $user->uname; // 输出uname属性(字段)值
    echo $user->email; // 输出email属性(字段)值
} else {
    print '该用户不存在';
}


namespace app\user\model;

class UserModel extends \wf\model\Model {    
    protected $table = 'user';    
    
    /**
     * 属性和表字段的绑定
     * 
     * 设置表字段对应模型类的属性,以实现把类属性绑定到表字段,并且Model->toArray()方法可获取绑定属性的值。
     * 表字段名和属性名都是大小写敏感。
     * 
     * @var array = [
     *     '表字段1' => '属性1',
     *     '表字段2' => '属性2',
     *     ...
     * ]
     */
    protected $columnMap = [
        'uname' => 'userName', // 把字段uname映射到name属性,属性名和字段名可以不一样
    ];

    private $userName;
}
    
    /**
     * 获取所有记录
     * 
     * @param string $sql
     * @param array $args = [] sql格式化参数值列表
     * @param bool $allowCache
     */
    public function getAll($sql, array $args = [], $allowCache = false);
    
    /**
     * 获取第一列
     * 
     * @param string $sql
     * @param array $args = []  sql格式化参数值列表
     * @param bool $allowCache
     */
    public function getRow($sql, array $args = [], $allowCache = false);
            
    /**
     * 获取第一列第一个字段
     * 
     * @param string $sql
     * @param array $args =[]  sql格式化参数值列表
     * @param bool $allowCache
     */
    public function getColumn($sql, array $args = [], $allowCache = false);

    /**
     * 执行写入SQL
     * 针对没有结果集合返回的写入操作,
     * 比如INSERT、UPDATE、DELETE等操作,它返回的结果是当前操作影响的列数。
     * 
     * @param string $sql
     * @param array $args = []  sql格式化参数值列表
     * @throws \wf\db\Exception
     * @return int
     */
    public function exec($sql, array $args = []);
    
    /**
     * 取得上一步 INSERT 操作产生的 ID
     *
     * @return string 
     */
    public function lastInsertId();
    
    /**
     * 插入多行数据
     * 过滤掉没有的字段
     *
     * @param array $rows
     * @param string $table  插入表
     * @param array $fields  允许插入的字段名
     * @param string $isReplace = false 是否使用 REPLACE INTO插入数据,false为使用 INSERT INTO
     */
    public function insertRows(array $rows, $table, $fields = [], $isReplace = false);
    

namespace app\user\model;

class UserModel extends \wf\model\Model 
{    
    protected $table = 'user';

    public function create() 
    {
        $trans = $this->getDb()->beginTransaction();
        try {
            parent::create();
            // 其他数据库写入业务
            // ……

            // 没异常则提交事务
            $trans->commit();
        } catch(\Exception $e) {
            // 出现异常则回滚事务
            $trans->rollback();
        }
    }
}


// app/trade/service/OrderService.php
namespace app\trade\service;

class OrderService extends wf\model\Model
{
    // 确认下单
    public function createOrder($attr)
    {
        // 
    }
}