使用模型查询数据之getAttr()

查询分为两种方式,一种是Db查询,一种是模型查询。

Login.php

<?php 

namespce appindexcontroller;

use thinkController;

use thinkLoader;

use appindexmodelData;

class Login extends Controller

{

  public function _initialize()

  {

    $this->data=Loader::model('data');

  }

  public function test()

  {

    $model=new Data();

    $data=$this->data->getMenu();

    //data()方法完成前面模型对象的$data属性赋值  //使模型对象转换成数据对象,现在$model 已经是数据对象啦

    $model->data($data[0]);

    $res=$model->getAttr('node_name');

    dump($res);  //管理员管理

  }

}

data()方法,将模型对象转化成数据对象

getAttr()方法,获取一维的索引数组的某个字段的值

Data.php

<?php

namespace appindexmodel;

use thinkDb;

use thinkModel;

class Data extends Model

{

  protected $table='data';

  public function getMenu()

  {

    $res=Db::name($this->table)->select();

    return $res;

  }

}

原文地址:https://www.cnblogs.com/ymdphp/p/10949136.html