Home >Backend Development >PHP Tutorial >thinkPHP模型初始化实例分析_PHP

thinkPHP模型初始化实例分析_PHP

WBOY
WBOYOriginal
2016-05-29 11:47:541076browse

ThinkPHP

本文实例讲述了thinkPHP模型初始化的方法。分享给大家供大家参考,具体如下:

/*
  $dsn = 'mysql://bookman:book123@localhost:3306/Weapons';
  $guns_tab = M('guns','',$dsn);
  $gun1 = $guns_tab ->select();
  echo $guns_tab -> getLastSql();
  dump($gun1);
*/
/*
  $guns_tab = M('guns');
  $gun1 = $guns_tab ->select();
  echo $guns_tab -> getLastSql();
  dump($gun1);
*/
  $guns_tab = new GunsModel();
  $gun1 = $guns_tab ->select() -> page('1,1');
  echo $guns_tab -> getLastSql();
  dump($gun1);
  $guns_tab -> showInfo();
/*
  $guns_tab = new DetectModel('guns');
  $gun1 = $guns_tab -> select();
  echo $guns_tab -> getLastSql();
  dump($gun1);
  $guns_tab -> showInfo();
*/

1.脱离配置文件产生的连接方式

$dsn = 'mysql://bookman:book123@localhost:3306/Weapons';
$guns_tab = M('guns','',$dsn);
$gun1 = $guns_tab ->select();
echo $guns_tab -> getLastSql();
dump($gun1);

2.依照配置文件连接数据库,表名一定要区分清楚

$guns_tab = M('guns');
$gun1 = $guns_tab ->select();
echo $guns_tab -> getLastSql();
dump($gun1);

3.我有一个自定义的模型,里面实现了自己的业务方法,我还有一个guns的表于是可以这样初始化模型。既可以获取guns表中的数据,还可以使用我自定义模型中的业务方法。

$guns_tab = new DetectModel('guns');
$gun1 = $guns_tab -> select();
echo $guns_tab -> getLastSql();
dump($gun1);
$guns_tab -> showInfo();

希望本文所述对大家基于thinkPHP框架的PHP程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn