💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
模型初始化 initialize( ) 1. 作用:创建模型对象之前做的预处理工作。 在ThinkPHP5中,创建数据模型是一个非常容易的过程,整个过程是透明的。 2. 实例演示:创建模型对象前,初始化部分模型类属性 模型对象创建时,除了模型名$name和所继承的类$class(命名空间表示)之外,其它所有的模型属性都是空的或只有默认值。随着以后的操作,这些属性会根据操作要求自动完成初始化。 第一步:在上节课创建的模型类Staff.php里,添加初始化方法 ~~~ <?php namespace app\index\model; //导入模型类(首字母大写) use think\Model; class Staff extends Model { //在子类重写父类的初始化方法initialize() protected function initialize(){ //继承父类中的initialize() parent::initialize(); //初始化数据表名称,通常自动获取不需设置 $this->table = 'tp5_staff'; ~~~ 获取数据表字段信息 ~~~ //获取数据表字段信息 $this->field = $this->db()->getTableInfo('', 'fields'); ~~~ 输出数据库表字段 ~~~ array(6) { [0] => string(2) "id" [1] => string(4) "name" [2] => string(3) "sex" [3] => string(6) "salary" [4] => string(4) "dept" [5] => string(8) "hiredate" } ~~~ 获取数据表字段类型 ~~~ //初始化数据表字段类型 $this->type = $this->db()->getTableInfo('', 'type'); ~~~ 输出数据表字段类型 ~~~ array(6) { ["id"] => string(15) "int(4) unsigned" ["name"] => string(11) "varchar(30)" ["sex"] => string(19) "tinyint(2) unsigned" ["salary"] => string(11) "float(10,2)" ["dept"] => string(11) "varchar(20)" ["hiredate"] => string(4) "date" } ~~~ 获取数据表主键 ~~~ //获取数据表主键 $this->pk = $this->db()->getTableInfo('', 'pk'); ~~~ 输出数据表主键 ~~~ string(2) "id" ~~~ 初始化之后得到的数据模型对象 ~~~ object(app\index\model\Staff)#5 (32) { ["connection":protected] => array(0) { } ["parent":protected] => NULL ["query":protected] => NULL ["name":protected] => string(5) "Staff" ["table":protected] => string(9) "tp5_staff" ["class":protected] => string(21) "app\index\model\Staff" ["error":protected] => NULL ["validate":protected] => NULL ["pk":protected] => string(2) "id" ["field":protected] => array(6) { [0] => string(2) "id" [1] => string(4) "name" [2] => string(3) "sex" [3] => string(6) "salary" [4] => string(4) "dept" [5] => string(8) "hiredate" } ["readonly":protected] => array(0) { } ["visible":protected] => array(0) { } ["hidden":protected] => array(0) { } ["append":protected] => array(0) { } ["data":protected] => array(0) { } ["origin":protected] => array(0) { } ["relation":protected] => array(0) { } ["auto":protected] => array(0) { } ["insert":protected] => array(0) { } ["update":protected] => array(0) { } ["autoWriteTimestamp":protected] => bool(false) ["createTime":protected] => string(11) "create_time" ["updateTime":protected] => string(11) "update_time" ["dateFormat":protected] => string(11) "Y-m-d H:i:s" ["type":protected] => array(6) { ["id"] => string(15) "int(4) unsigned" ["name"] => string(11) "varchar(30)" ["sex"] => string(19) "tinyint(2) unsigned" ["salary"] => string(11) "float(10,2)" ["dept"] => string(11) "varchar(20)" ["hiredate"] => string(4) "date" } ["isUpdate":protected] => bool(false) ["updateWhere":protected] => NULL ["failException":protected] => bool(false) ["useGlobalScope":protected] => bool(true) ["batchValidate":protected] => bool(false) ["resultSetType":protected] => string(5) "array" ["relationWrite":protected] => NULL } ~~~ * * * * * [《ThinkPHP5模型实例详解》](https://www.kancloud.cn/ldkt/tp5_model)