Codeigniter 4 MySQL查询构建器从本身获取subdata

发布于 2025-01-18 21:21:17 字数 1725 浏览 0 评论 0 原文

[更新]

错误现在已经消失,但是与PhpMyAdmin中的数据相同,该错误来自Codeigniter 4查询构建器的所有查询构建器都具有相同的 ID 值。而且,如果我使用 $构建器方法现在给出了预期的结果,但是我仍然希望能够在可能的情况下正确使用查询构建器。.


有这样的mysql表

id | nama | hari
-----------------
1  | AAA  | 1
2  | AAA  | 2
3  | AAA  | 3
4  | AAA  | 4
5  | AAA  | 5
6  | BBB  | 1
7  | BBB  | 2
8  | BBB  | 3

我 我得到一个参数 ID 1,我只想获得行号1-5。当我在phpmyadmin中使用此SQL

select m.* 
from m_rute m
join (
    select m1.*
    from m_rute m1
    where m1.id = 1
) mt on mt.nama_rute = m.nama_rute

”

然后我尝试使用codeigniter 4的查询构建器来“翻译”它,但是当我在邮递员上对其进行测试时,它提供了一组数据,其中所有 ID 字段是相同的,与phpmyadmin结果中的字段相同。

控制器

public function show($id = null)
{
    $db = \Config\Database::connect();
    $builder = $db->table('m_rute as m');
    $builder->select('m.*')->where('m.id', $id);
    $subquery = $builder->getCompiledSelect();

    // this works but wouldn't a fully proper query builder seem nicer?
    //////////////
    // $builder->select('m.*')
    //     ->join('('.$subquery.') as m1', 'm1.nama_rute = m.nama_rute')
    //     ;
    // $q = $builder->get();
    // $data = $q->getResultArray();

    $model = new MRuteModel();

    $model->join('('.$subquery.') as t', 't.nama_rute = m_rute.nama_rute');

    $data  = $model->findAll();

    if (!$data) {
        return $this->failNotFound('Data not found');
    }

    return $this->respond($data);
}

任何帮助将不胜感激

[UPDATED]

the error is now gone but the data result from the codeigniter 4 query builder all has the same id value, unlike the one in phpmyadmin. and if I use the $builder method it now gives the expected result, but I'm still hoping to be able to fully use the query builder properly if possible..


I have a mysql table like this

id | nama | hari
-----------------
1  | AAA  | 1
2  | AAA  | 2
3  | AAA  | 3
4  | AAA  | 4
5  | AAA  | 5
6  | BBB  | 1
7  | BBB  | 2
8  | BBB  | 3

when I get a parameter id 1, I want to get the row number 1-5 only. it works when I use this sql in phpmyadmin

select m.* 
from m_rute m
join (
    select m1.*
    from m_rute m1
    where m1.id = 1
) mt on mt.nama_rute = m.nama_rute

result from sql in phpmyadmin

then I try to "translate" it using codeigniter 4's query builder but when I test it on postman, it gives a set of data where all the id field is the same, unlike the one in phpmyadmin result.

controller

public function show($id = null)
{
    $db = \Config\Database::connect();
    $builder = $db->table('m_rute as m');
    $builder->select('m.*')->where('m.id', $id);
    $subquery = $builder->getCompiledSelect();

    // this works but wouldn't a fully proper query builder seem nicer?
    //////////////
    // $builder->select('m.*')
    //     ->join('('.$subquery.') as m1', 'm1.nama_rute = m.nama_rute')
    //     ;
    // $q = $builder->get();
    // $data = $q->getResultArray();

    $model = new MRuteModel();

    $model->join('('.$subquery.') as t', 't.nama_rute = m_rute.nama_rute');

    $data  = $model->findAll();

    if (!$data) {
        return $this->failNotFound('Data not found');
    }

    return $this->respond($data);
}

any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮萍、无处依 2025-01-25 21:21:17

解决方案1:使用 raw/常规查询

query()函数返回数据库结果 object
运行“读取”类型查询,您可以使用显示您的
结果

运行“写”类型查询时,只需返回 true false
取决于成功或失败。

<?php

namespace App\Models;

use CodeIgniter\Model;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        $id = $this->db->escape($id);

        return $this->db->query("
            select m.* 
            from m_rute m
                join (
                    select m1.*
                    from m_rute m1
                    where m1.id = {$id}
                ) mt on mt.nama_rute = m.nama_rute")->getResultArray();
    }
}

解决方案2a:使用 nofollow noreferrer“> sql sql join> sql join with raw queries 。

join($ table,$ cond [,$ type =''[,$ eccave = null]])
&nbsp;&nbsp; 参数:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $ table (string) - 表名
加入

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $ cond (字符串) -
在条件下加入

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $ type
(字符串) - 联接类型

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
逃脱值和标识符

&nbsp;&nbsp;&nbsp; 返回: basebuilder 实例(方法)
链接)

&nbsp;&nbsp;&nbsp; 返回类型: basebuilder

加入子句到查询。

<?php

namespace App\Models;

use CodeIgniter\Model;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        $id = $this->db->escape($id);

        return $this->db->table('m_rute m')
            ->select("m.*")
            ->join("
            (select m1.*
                from m_rute m1
                    where m1.id = {$id}
            ) mt", 'mt.nama_rute = m.nama_rute')
            ->get()->getResultArray();
    }
}

解决方案2b:使用

编译选择语句并将其返回为字符串。

<?php

namespace App\Models;

use CodeIgniter\Model;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        $subQuery = $this->db->table('m_rute m1')
            ->select("m1.*")
            ->where("m1.id", $id)
            ->getCompiledSelect();

        return $this->db->table('m_rute m')
            ->select("m.*")
            ->join("($subQuery) mt", 'mt.nama_rute = m.nama_rute')
            ->get()
            ->getResultArray();
    }
}

解决方案3a:使用a 关闭。

whye($ key [,$ value = null [,$ evave = null]])
&nbsp;&nbsp; 参数:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $ key (混合) - 比较的字段名称或关联阵列

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $ value (混合) - 与此值相比,如果是一个键

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $逃逸
(布尔) - 是否逃脱值和标识符

&nbsp;&nbsp;&nbsp; 返回: basebuilder 实例(方法)
链接)

&nbsp;&nbsp;&nbsp; 返回类型: basebuilder

生成查询的部分部分。用将多个调用分开。

<?php

namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\BaseBuilder;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        // With a closure.
        return $this->db->table('m_rute')
            ->where('nama_rute =', function (BaseBuilder $builder) use ($id) {
                return $builder->select("nama_rute")->from("m_rute")
                    ->where("id", $id);
            })
            ->get()
            ->getResultArray();
    }
}

解决方案3b:使用a 和一个建筑商。


<?php

namespace App\Models;

use CodeIgniter\Model;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        $subQuery = $this->db->table('m_rute m1')
            ->select("m1.nama_rute")
            ->where("m1.id", $id)
            ->getCompiledSelect();

        // With a builder.
        return $this->db->table('m_rute')
            ->where("nama_rute =($subQuery)")
            ->get()
            ->getResultArray();
    }
}

Solution 1: Using Raw/Regular Queries.

The query() function returns a database result object when
“read” type queries are run which you can use to show your
results
.
When “write” type queries are run it simply returns true or false
depending on success or failure.

<?php

namespace App\Models;

use CodeIgniter\Model;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        $id = $this->db->escape($id);

        return $this->db->query("
            select m.* 
            from m_rute m
                join (
                    select m1.*
                    from m_rute m1
                    where m1.id = {$id}
                ) mt on mt.nama_rute = m.nama_rute")->getResultArray();
    }
}

Solution 2A: Using SQL Joins With Raw Queries.

join($table, $cond[, $type = ''[, $escape = null]])
   Parameters:

      $table (string) – Table name
to join

      $cond (string) – The
JOIN ON condition

      $type
(string) – The JOIN type

      $escape (bool) – Whether to
escape values and identifiers

   Returns: BaseBuilder instance (method
chaining)

   Return type: BaseBuilder

Adds a JOIN clause to a query.

<?php

namespace App\Models;

use CodeIgniter\Model;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        $id = $this->db->escape($id);

        return $this->db->table('m_rute m')
            ->select("m.*")
            ->join("
            (select m1.*
                from m_rute m1
                    where m1.id = {$id}
            ) mt", 'mt.nama_rute = m.nama_rute')
            ->get()->getResultArray();
    }
}

Solution 2B: Using SQL Joins With A Query Builder.

getCompiledSelect()

Compiles a SELECT statement and returns it as a string.

<?php

namespace App\Models;

use CodeIgniter\Model;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        $subQuery = $this->db->table('m_rute m1')
            ->select("m1.*")
            ->where("m1.id", $id)
            ->getCompiledSelect();

        return $this->db->table('m_rute m')
            ->select("m.*")
            ->join("($subQuery) mt", 'mt.nama_rute = m.nama_rute')
            ->get()
            ->getResultArray();
    }
}

Solution 3A: Using A where Clause With A Closure.

where($key[, $value = null[, $escape = null]])
   Parameters:

      $key (mixed) – Name of field to compare, or associative array

      $value (mixed) – If a single key, compared to this value

      $escape
(bool) – Whether to escape values and identifiers

   Returns: BaseBuilder instance (method
chaining)

   Return type: BaseBuilder

Generates the WHERE portion of the query. Separates multiple calls with AND.

<?php

namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\BaseBuilder;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        // With a closure.
        return $this->db->table('m_rute')
            ->where('nama_rute =', function (BaseBuilder $builder) use ($id) {
                return $builder->select("nama_rute")->from("m_rute")
                    ->where("id", $id);
            })
            ->get()
            ->getResultArray();
    }
}

Solution 3B: Using A where Clause With A Builder.


<?php

namespace App\Models;

use CodeIgniter\Model;

class MRuteModel extends Model
{
    public function findById(int $id)
    {
        $subQuery = $this->db->table('m_rute m1')
            ->select("m1.nama_rute")
            ->where("m1.id", $id)
            ->getCompiledSelect();

        // With a builder.
        return $this->db->table('m_rute')
            ->where("nama_rute =($subQuery)")
            ->get()
            ->getResultArray();
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文