PHP code example of hectorqin / think-core

1. Go to this page and download the library: Download hectorqin/think-core library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hectorqin / think-core example snippets


    // 使用模型文件里面relations方法获取预定义的关联模型配置
    BookModel::with(['author','comments'])->select();
    BookModel::instance()->with(['author','comments'])->select();

    // 直接传入关联模型配置,将与预定义关联模型合并
    BookModel::with(['author'=>[
        'relation_model' => AuthorModel::class,
        'relation_table' => 'authors',
        'relation_key'   => 'id',
        'foreign_key'    => 'author_id',
        'mapping_name'   => 'author',
        'mapping_type'   => RelationModel::BELONGS_TO,
        'condition'      => [],
    ]])->select();

    // 设置关联查询回调
    BookModel::with(['author'=>function($query){
        $query->where(['name'=>'张三']);
    }])->select();

    // 子关联模型
    BookModel::with(['author.books','comments'=>['with'=>'user']])->select();
    BookModel::with(['author.books','comments'=>['with'=>['user']]])->select();

    // 子关联模型动态配置
    BookModel::with(['author.books' => [
        'condition' => false, // 取消默认查询条件
    ],'comments'])->select();

    // 子关联模型查询回调
    BookModel::with(['author.books' => function($query){
        $query->where(['sale_num', ['gt', 100]]);
    },'comments'])->select();