ThinkPHP Framework Model

1. There is a test method in MainController.class.php controller, and there is also a deng method. I want to use the deng method in the test method.

Expressed as

<?php
namespace Home\Controller;
use Think\Controller;
class MainController extends controller
{
    public function test()
    {
       $this->deng();
    }

2. There is a test method in the MainController.class.php controller, and under the same template there is a controller named IndexController.class.php. There is an index method in it. I want to use the index method in the test method to express as follows:

<?php
namespace Home\Controller;
use Think\Controller;
class MainController extends controller
{
    public function test()
    {
 
   //Cross-Controller Call Method
     $index=new IndexController();
     $index->index();
       $index=A("Index");
        $index->index();
      R("Index/index");
   }

These are three different invocation methods

3. There is a test method in MainController.class.php controller under Home folder, and a controller named TextController.class.php under different template Admin folder. There is an aa method in it. I want to use aa method in test method to express as follows:

 

<?php
namespace Home\Controller;
use Think\Controller;
class MainController extends controller
{
    public function test()
    {
       //Calling methods across modules
       $index=new \Admin\Controller\TextController();
      $index->aa();
       $index=A("Admin/Text");
        $index->aa();
        R("Admin/Text/aa");
   }

 

4. Use the model to operate the database. Call out the child class object with D, if M ("nation") calls out the parent class object, and select() calls out all the objects, which is a two-dimensional array, if find() calls out a data, such as find("n001").

$nation=D("nation");
$arr=$nation->select();
var_dump($arr);

5 Query where (condition) with where statement

 $nation=D("nation");
        $arr=$nation->where("code='n005' or code='n003'")->select();
        var_dump($arr)

Two figures were found.

6 TABLE () can be used to switch tables. I chose table cname in club to switch tables.

$nation=D("nation");
$arr=$nation->table("cname")->select();
var_dump("$arr");

7 field (a field of a specified database), only the data of this field is queried

$nation=D("nation");
$arr=$nation->field("code")->select();
var_dump($arr);

8 order () means that the contents of the query are arranged in order

 $nation=D("nation");
$arr=$nation->order("code desc")->select();
        var_dump($arr);

9 limit () Paging query If there is only one value in (), it means how many data are displayed, and if there are two values (,), it means how many pages are skipped and how many pages are displayed.

$nation=D("nation");
$arr=$nation->limit(6)->select();
        var_dump($arr);

If you use page() paging queries, you will be more humane. There are two values in parentheses. The first value represents the number of pages, and the second value represents how many items are displayed per page.

10 grouping groups () For example, I would like to look up how many pieces of data are grouped by brand in the car table.

$nation=D("nation");
$arr=$nation->field("brand,count(*)")->table("car")->group("brand")->select();
var_dump($arr);

11 join to associate two tables

 

$login=D("login");
$arr=$login->field("login.uid,login.pwd,cname.name")->join("cname on cname.code=login.code")->select();
var_dump($arr);

 

As shown in the figure.

Keywords: PHP Database

Added by marq on Thu, 04 Jul 2019 02:41:23 +0300