model

通过加载器加载并返回一个model的实例。

函数原型

    /**
     * 获取一个model
     * @param $model string
     * @param $parent CoreBase
     */
    function model($model, $parent)

其中$model是Model的类名,根据SD的传统该类优先在app/Models中寻找,如果不存在则在Server/Models中寻找。

$parent是调用的容器,一般都是传入$this。

例子:

    public function test_model()
    {
        $testModel = $this->loader->model('TestModel',$this);
        $testModel->timerTest();# Model

Model是专门和数据打交道的模块。  


## 协程模式

model中可以调用redis,mysql,task的协程模式,但是请注意,如果model是使用协程的,那么controller或者model调用这个包含协程model接口时,也要加上yeild关键字。  
例子:

```php
public function http_testCoroutine()
    {
        $this->testModel = $this->loader->model('TestModel', $this);
        $result = yield $this->testModel->test_coroutine();
        $this->http_output->end($result);
    }

Model层级

1.7.4版本支持深层级的Model。

$this->testModel = $this->loader->model('Test/TestModel', $this);

如上会调用Test目录下TestModel,请注意命名空间的规范。

在1.7.4版本之前需要这样

$this->testModel = $this->loader->model('Test\\TestModel', $this);
    $this->destroy();
}

```

注意

在1.7.5版本之前loader->model不允许出现循环loader,比如A加载了B,B又加载了A,又或者A加载了A,这种会引发严重的服务器故障,出现死循环问题。

1.7.5版本后框架会自动发现这种情况并会将上层的实例直接传回。

比如A->B->C->A这种,C里面的A其实就是最初的A。

results matching ""

    No results matching ""