Yaf集成think-orm4

目前最新yaf版本3.3.6,可以在PHP8.3上成功安装。

安装步骤
#下载最新代码
wget https://pecl.php.net/get/yaf-3.3.6.tgz
#解压
tar -zxvf yaf-3.3.6.tgz
#安装
cd yaf-3.3.6/
phpize
./configure --with-php-config=/www/server/php/83/bin/php-config
make && make install
#修改php.ini 添加yaf.so
#克隆yaf
git clone https://github.com/laruence/yaf.git
#生成项目skeleton
cd /www/wwwroot/yaf/tools/cg
php yaf_cg -d Sample -n
安装think-orm
sudo -u www composer require topthink/think-orm
#入口引入think-orm
require __DIR__ . '/vendor/autoload.php';
添加数据库配置
[think-orm]
db.type     = mysql
db.hostname = 127.0.0.1
db.username = yaf
db.password = myGEFhY7FKhKME3m
db.database = yaf
db.charset  = utf8mb4
db.prefix   = yaf_
db.debug    = true
初始化数据库配置
<?php
class Bootstrap extends \Yaf\Bootstrap_Abstract {
    public function _initDb(){
        $ini = new \Yaf\Config\Ini(__DIR__."/../conf/db.ini");
        $arr = $ini->toArray();
        $config = ['default'=>'mysql','connections'=>['mysql'=>$arr['think-orm']['db']]];
        (new \think\DbManager())->setConfig($config);
    }
}
配置模型
<?php
use think\Model;
/**
 * @name SampleModel
 * @desc sample模型类 可以使用模型的属性和方法
 * @author root
 */
class SampleModel extends Model{
    protected $table = "yaf_sample";
    public function selectSample() {
        return 'Hello World!';
    }

    public function insertSample($arrInfo) {
        return true;
    }
}
think-orm4新特性Enity引入
<?php
class Bootstrap extends \Yaf\Bootstrap_Abstract {
    public function _initLoader($dispatcher) {
        \Yaf\Loader::getInstance()->registerNamespace('App\Entity', realpath(APPLICATION_PATH . '/application/entity'));
    }
}
Enity sample
<?php
namespace App\Entity;
use think\Entity;
/**
 * @name SampleEntity
 * @desc sample实体类 可以扩展业务逻辑 让模型专注查询、持久化数据
 * @author root
 */
class SampleEntity extends Entity
{
    public function getOptions():array
    {
    	return [
            'modelClass'      => \SampleModel::class,
    	];
    }
}
Enity使用
$entity = new \App\Entity\SampleEntity();
$one = \App\Entity\SampleEntity::find(1);

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注