PHP code example of wzzwx / yii1-model-extension

1. Go to this page and download the library: Download wzzwx/yii1-model-extension 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/ */

    

wzzwx / yii1-model-extension example snippets

$xslt
// main.php

return [
    ...
    'components' => [
        ...
        'db' => [
            'class' => \wzzwx\yii1model\db\DbConnection::class, // 此处使用
            'connectionString' => ...
            ...
        ],
        ...
    ],
    ...
];
$xslt
    // 查询用户的订单
    $ret = UserModel::query()
        ->with('order')     // 在UserModel中配置的withConfigs
        ->queryAll();

    // 查询某个订单
    $ret = OrderModel::query()
        ->with('items,user')
        ->andWhere(['order_no' => 'JD123d1awaasdwa221'])
        ->queryRow();
    // 或者
    $ret = OrderModel::query()
       ->with([
            'user',
            // 'user' => fn($query) => $query->select('id,name,age'),  // php7.4 简写
            'items' => function($query){
                $query->select('xx,xx')
                    ->andWhere(['status' => xxx])     // 增加条件筛选
                    ->with('goods');    // 查出产品相关信息, 需要在OrderItems中配置好withConfigs
            }
        ])
       ->andWhere(['order_no' => 'JD123d1awaasdwa221'])
       ->queryRow();
    // 也可将 model 中 withConfigs()方法的返回值直接传入with()方法