Use ThinkPHP5-- model (model) of

In the process of using ThinkPHP5, I often use db database operation, and later exposed to the model (model), feeling nothing different with db Just use, then check the official website to know the model base class also provides other more of The method can be easily acquired using, for example, a modifier, and so on to complete the data functions. Therefore function more powerful model, TP5 official website recommended model.

In terms of data format in an array format using the DB is used. The uniform use of objects in the model. Wherein the interactive database relates to the format converter. Therefore, in the same circumstances db data with the model slightly faster way. Thus a specific choice is not mandatory in accordance with personal views embodiment ThinkPHP5 used for programming the model of DB. Sometimes in order to package the project uses some of the modeling approach may be more appropriate

 

1, model definition

Blog model in a model class definition:

<?php
namespace app\demo\model;
use think\Model;
class Blog extends Model
{
    // The default primary key for the automatic identification, if desired specified attributes can be set:
    //protected $pk = 'uid';
}

Utilize the data model table, model class naming rules table name table is to remove the prefix naming method using hump, first letter capitalized, for example:

Model name Conventions corresponding data table (assuming the database is defined prefix think_)
User think_user
UserType think_user_type

 

2, the model calls

Model class may be used a static call or instantiated call in two ways, for example:

    // static call 
        $ Blog = Blog :: GET (1); // with id 1 as a condition (to check automatically based on the primary key)

        Examples of model // 
        $ Blog = new new Blog ();
         // use of class instance Loader (singleton) 
        $ Blog = Loader Model :: ( 'Blog' );
         // or using helper model` ` 
        $ Blog = Model ( 'Blog');

 

3, using the model change check deletions

Inquire

        // query 
        $ Blog = :: GET Blog (2); // to id is a condition that (automatically be checked based on the main key) 
        echo  $ Blog -> title; // Output: php combat

        User $ = new new the User ();
         // query single data 
        $ User -> WHERE ( 'name', 'thinkphp' )
             -> Find ();

        // query data getByxxx () method according to a condition 
        $ Bolg = Blog :: getByTitle ( 'Model. 1' );
         echo  $ Bolg -> Content;

        // multi-criteria query, the array can be passed as a query 
        $ Bolg = Blog :: GET ([ 'title' => 'Model 1', 'content' => ' content model 2' ]);
         $ Bolg = Blog: : where ([ 'title' = > ' model 1', 'content' => ' content model 2']) -> the Find ();
         echo  $ Bolg -> the above mentioned id;
         // query all 
        $ Bolg = Blog :: all ();
         the foreach ( $ Bolg  AS  $ Key => $ V ) {
             echo  $ V -> title "<br>." ;
        }

    User $ = new new the User ();
    // query data set 
    $ User -> WHERE ( 'name', 'thinkphp' )
         -> limit (10 )
         -> Order ( 'ID', 'desc' )
         -> SELECT () ;

Add to

    // add a single 
        $ Blog -> title = 'model' ;
         $ Blog -> author = 'LHS' ;
         $ Blog -> publish_time = '. 11' ;
         $ Blog -> Content = 'model content' ;
         $ Blog -> Save ();
         // batch add 
        $ List = [
            [ 'Title' => 'model 1', 'author' => 'lhs',' publish_time '=>' 12 ',' content '=>' model content 2 '], 
            [ ' title '=>' Model 2 ',' author '=>' lhs', 'publish_time' => '13', 'content' => ' content model 3' ]
        ];
        IF ( $ Blog -> saveAll ( $ List )) {
             echo 'user successfully added! ' ;
        }

Update

// Update 
        $ Blog = new new Blog ();
         $ Blog -> = 24 ID ;
         $ Blog -> title = 'updated models' ;
         $ Blog -> author = 'LHS' ;
         $ Blog -> publish_time = '1,572,919,302' ;
         Blog $ -> content = 'content model' ;
         $ Blog -> isUpdate () -> Save ();

        // call this method, isUpdate change true (which is the default update operations) 
        $ Blog = Blog :: GET (1 );
         $ Blog -> title = 'updating model' ;
         $ Blog -> author = 'lhs' ;
         $ Blog -> publish_time = '1553011200' ;
         $ Blog -> content = 'content model' ;
         IF ( $ Blog -> Save ()) {
             echo "updated successfully!" ;
        } 
// Add to use this method for data $ Blog = Blog :: GET (. 1 ); $ Blog -> title = 'GET addition model' ; $ Blog -> author = 'LHS' ; $ Blog -> publish_time = '1568908800' ; $ Blog -> content = 'content model' ; $ Blog -> ID = null ; IF ( $ Blog -> isUpdate ( to false ) -> Save ()) { echo "successfully!" ; }

delete

        $bolg = Blog::get(25);
        $bolg->delete();
        Blog::destroy(26);    

 

3, the reader and modifiers

The role of the reader is processed automatically obtain the value of the data modifier effect may be made automatically when the data conversion processing assignment, for example :

<?php
namespace app\demo\model;
use think\Model;

class Codes extends Model
{
    // reader 
    public  function getPublishTimeAttr ( $ value )
    {
        return date("Y-m-d H:i",$value);
    }
    // $ throughout the Data Object 
    public  function getNameAttr ( $ value , $ the Data )
    {
        return 'Title:'. $ Data [ 'name'] ', price:'.. $ Data [ '. price' ];
    }

    // modifier 
    public  function setPublishTimeAttr ( $ value )
    {
        return strtotime($value);
    }
}

Use reader and modifier

public  function Read () {
         // reader naming methods: get + property name (hump nomenclature) the Attr + 
        / * $ code = Codes :: GET (. 1);
        echo $ code-> publish_time; // If you have customized getXXXAttr is called, is not self-definition is output
        echo $code->name;*/

        // add 
        $ code = new new Codes ();
         $ code -> name = 'Alipay funds' ;
         $ code -> category =' economy ' ;
         $ code ->. Price =' 161 ' ;
         $ code -> publish_time =' 2019 -9-27 '; // call setPublishTimeAttr method, add a timestamp is automatically converted to 
        $ code -> the Save ();
    }

 

Guess you like

Origin www.cnblogs.com/bushui/p/11801997.html