easy-swoole / easyswoole

swoole,easyswoole,swoole framework
https://www.easyswoole.com/
Apache License 2.0
4.73k stars 511 forks source link

当不是主键去联表的时候,获取不到数据 #494

Closed jkaltopp closed 2 years ago

jkaltopp commented 3 years ago

不是主键去关联的时候就获取不到,如果用主键的时候就获取的到,我的主键是id image image

XueSiLf commented 3 years ago

请提供一下相关表结构,方便我们这边测试复现。

jkaltopp commented 3 years ago

CREATE TABLE yl_unit ( id int(11) NOT NULL AUTO_INCREMENT, goods_id int(11) NOT NULL DEFAULT '0', name varchar(120) NOT NULL DEFAULT '', PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;

CREATE TABLE yl_component ( id int(11) NOT NULL AUTO_INCREMENT, bom_id int(11) NOT NULL DEFAULT '0', goods_id int(11) NOT NULL DEFAULT '0', PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8mb4;

XueSiLf commented 3 years ago

好的,感谢反馈。

jkaltopp commented 3 years ago

这个表特别奇怪,我直接JOIN 联表的信息都不会出来, 我打印出sql,去查询就可以,用orm这样就只能看到主表,联表的就不能带出来了 BomComponentModel::create()->alias('a')->join('yl_goods goods','goods.goods_id=a.goods_id')->where('a.bom_id',16)->get();

XueSiLf commented 3 years ago

不可能啊------------------ 原始邮件 ------------------ @.> 发送时间: 2021年7月27日(星期二) 晚上10:44 @.>; @.**@.>; 主题: Re: [easy-swoole/easyswoole] 当不是主键去联表的时候,获取不到数据 (#494)

XueSiLf commented 3 years ago

BomComponentModel::create()->alias('a')->join('yl_goods goods','goods.goods_id=a.goods_id')->where('a.bom_id',16)->get();

我进行了测试没有出现你这个情况:

表结构sql

CREATE TABLEyl_component( idint(11) NOT NULL AUTO_INCREMENT, bom_idint(11) NOT NULL DEFAULT '0', goods_idint(11) NOT NULL DEFAULT '0', PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8mb4; INSERT INTOyl_component` VALUES (16, 16, 16);

CREATE TABLE yl_goods ( goods_id int(11) NOT NULL, goods_name varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, PRIMARY KEY (goods_id) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; INSERT INTO yl_goods VALUES (16, 'goods1'); `

注册 orm 连接:

配置文件 dev.php :

<?php

use EasySwoole\Log\LoggerInterface;

return [
    // ....
    // 添加 MySQL 及对应的连接池配置
    /*################ MYSQL CONFIG ##################*/
    'MYSQL' => [
        'host'          => '127.0.0.1', // 数据库地址
        'port'          => 3306, // 数据库端口
        'user'          => 'es_orm_debug', // 数据库用户名
        'password'      => 'es_orm_debug', // 数据库用户密码
        'timeout'       => 45, // 数据库连接超时时间
        'charset'       => 'utf8', // 数据库字符编码
        'database'      => 'es_orm_debug', // 数据库名
        'autoPing'      => 5, // 自动 ping 客户端链接的间隔
        'strict_type'   => false, // 不开启严格模式
        'fetch_mode'    => false,
        'returnCollection'  => true, // 设置返回结果为 数组
        // 配置 数据库 连接池配置,配置详细说明请看连接池组件 https://www.easyswoole.com/Components/Pool/introduction.html
        'intervalCheckTime' => 15 * 1000, // 设置 连接池定时器执行频率
        'maxIdleTime'   => 10, // 设置 连接池对象最大闲置时间 (秒)
        'maxObjectNum'  => 20, // 设置 连接池最大数量
        'minObjectNum'  => 5, // 设置 连接池最小数量
        'getObjectTimeout'  => 3.0, // 设置 获取连接池的超时时间
        'loadAverageTime'   => 0.001, // 设置 负载阈值
    ],
];

注册 orm 连接 EasySwooleEvent.php:

<?php

namespace EasySwoole\EasySwoole;

use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;

class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');

        ###### 注册 mysql orm 连接池 ######
        $config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
        // 【可选操作】我们已经在 dev.php 中进行了配置
        # $config->setMaxObjectNum(20); // 配置连接池最大数量
        DbManager::getInstance()->addConnection(new Connection($config));
    }

    public static function mainServerCreate(EventRegister $register)
    {

    }
}

模型文件:BomComponentModel.php

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link https://www.easyswoole.com
 * @document https://www.easyswoole.com
 * @contact https://www.easyswoole.com/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\Model;

use EasySwoole\ORM\AbstractModel;

class BomComponentModel extends AbstractModel
{
    protected $tableName = 'yl_component';
}
`

# 在控制器中调用模型:

`
<?php

namespace App\HttpController;

use App\Model\BomComponentModel;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\ORM\DbManager;

class Index extends Controller
{

    public function index()
    {
        $ret = BomComponentModel::create()->alias('a')->join('yl_goods goods', 'goods.goods_id=a.goods_id')->where('a.bom_id', 16)->get();
        // 打印结果:
        var_dump($ret->lastQueryResult()->getResult());
        // 结果:
        /**
         * array(1) {
             [0]=>
             array(4) {
                ["id"]=>
                int(16)
                ["bom_id"]=>
                int(16)
                ["goods_id"]=>
                int(16)
                ["goods_name"]=>
                string(6) "goods1"
             }
           }
         */
        $sql = DbManager::getInstance()->getLastQuery()->getLastQuery();
        // 打印执行sql:
        var_dump($sql);
        // 结果:string(118) "SELECT  * FROM yl_component AS `a`  JOIN yl_goods goods on goods.goods_id=a.goods_id WHERE  `a`.`bom_id` = 16  LIMIT 1"
    }
}

结果只是没有连表条件的另外一个字段(因为 select * 的原因)。是有数据结果的。

XueSiLf commented 3 years ago

不是主键去关联的时候就获取不到,如果用主键的时候就获取的到,我的主键是id image image

我怀疑是你使用的问题。我这边测试时有数据。

用到的 sql 文件

CREATE TABLE `yl_component`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bom_id` int(11) NOT NULL DEFAULT 0,
  `goods_id` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 24 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `yl_component` VALUES (16, 16, 16);
INSERT INTO `yl_component` VALUES (17, 17, 17);

CREATE TABLE `yl_unit`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_id` int(11) NOT NULL DEFAULT 0,
  `name` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `yl_unit` VALUES (1, 16, 'unit1');
INSERT INTO `yl_unit` VALUES (2, 16, 'unit2');
INSERT INTO `yl_unit` VALUES (3, 17, 'unit3');
INSERT INTO `yl_unit` VALUES (4, 17, 'unit4');

注册 orm 链接使用的是上面的

相关模型定义

GoodsUnitModel:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link https://www.easyswoole.com
 * @document https://www.easyswoole.com
 * @contact https://www.easyswoole.com/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\Model;

use EasySwoole\ORM\AbstractModel;

class GoodsUnitModel extends AbstractModel
{
    protected $tableName = 'yl_unit';
}

BomComponentModel:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link https://www.easyswoole.com
 * @document https://www.easyswoole.com
 * @contact https://www.easyswoole.com/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\Model;

use EasySwoole\ORM\AbstractModel;

class BomComponentModel extends AbstractModel
{
    protected $tableName = 'yl_component';

    public function units()
    {
        return $this->hasMany(GoodsUnitModel::class, null, 'goods_id', 'goods_id');
    }
}

在控制器中进行调用

<?php

namespace App\HttpController;

use App\Model\BomComponentModel;
use EasySwoole\Http\AbstractInterface\Controller;

class Index extends Controller
{
    public function index()
    {
        $ret = BomComponentModel::create()->with(['units'])->where('bom_id', 16)->all();
        $res = [];
        if ($ret) {
            // 获取所有数据(包括主表 和 关联表的)
            var_dump($ret->toArray(false, false));
            $unitsData = $ret[0]['units'];
            foreach ($unitsData as $k => $v) {
                $res[] = $v->toArray(false, false);
            }
        }
        // 获取关联表的数据结果
        var_dump($res);
    }
}

调用结果:

image