PHPmodel层查询数据库

在model层里的PHP

 $sqlGetAllCode=sprintf("select distinct code from %s",$tableName);
        $codeArr=DB::select($sqlGetAllCode);
        $A_data = [];
        $B_data = [];
        foreach ($codeArr as $a => $b) { //a是角标  b是对应的code编号
            $sqlGetData = sprintf("select cumulative,create_time from ".$tableName." where code= '".$b->code."'");
            $codeData = DB::select($sqlGetData);
            $A_data[$b->code]=[]; 
            foreach ($codeData as $m => $n) { //x 角标 y->variation 是值
                array_push($A_data[$b->code], $n->cumulative); 
        }
        foreach ($codeData as $x => $y) { //x 角标 y->variation 是值
                array_push($A_data[$b->code], $y->create_time); 
        }
         }  
        dd($A_data);
          
        return $A_data;
 

查询出的数据

array:4 [▼
  "BX6-01" => array:12 [▼
    0 => "9.4"
    1 => "8.66"
    2 => "7.55"
    3 => "9.35"
    4 => "10.23"
    5 => "11.44"
    6 => "2018-08-01 08:50:24"
    7 => "2018-08-02 08:50:24"
    8 => "2018-08-03 08:50:24"
    9 => "2018-08-04 08:50:24"
    10 => "2018-08-05 08:50:24"
    11 => "2018-08-06 08:50:24"
  ]
  "BX6-02" => array:12 [▼
    0 => "9.4"
    1 => "8.66"
    2 => "7.55"
    3 => "9.35"
    4 => "10.23"
    5 => "11.44"
    6 => "2018-08-01 08:50:24"
    7 => "2018-08-02 08:50:24"
    8 => "2018-08-03 08:50:24"
    9 => "2018-08-04 08:50:24"
    10 => "2018-08-05 08:50:24"
    11 => "2018-08-06 08:50:24"
  ]
  "BX4-01" => array:12 [▼
    0 => "9.4"
    1 => "8.66"
    2 => "7.55"
    3 => "9.35"
    4 => "10.23"
    5 => "11.44"
    6 => "2018-08-01 08:50:24"
    7 => "2018-08-02 08:50:24"
    8 => "2018-08-03 08:50:24"
    9 => "2018-08-04 08:50:24"
    10 => "2018-08-05 08:50:24"
    11 => "2018-08-06 08:50:24"
  ]
  "BX4-02" => array:12 [▼
    0 => "9.4"
    1 => "8.66"
    2 => "7.55"
    3 => "9.35"
    4 => "10.23"
    5 => "11.44"
    6 => "2018-08-01 08:50:24"
    7 => "2018-08-02 08:50:24"
    8 => "2018-08-03 08:50:24"
    9 => "2018-08-04 08:50:24"
    10 => "2018-08-05 08:50:24"
    11 => "2018-08-06 08:50:24"
  ]
]

将第一cumulative变成数组,再将create_time放进cumulative的数组内,应该怎么在model层内写?

【以下回答由 GPT 生成】

在PHP的model层使用数据库查询语句查询数据可以通过以下步骤进行:

  1. 首先,在model文件中建立一个与数据库表对应的类。
class YourModel extends Model
{
    protected $table = 'your_table_name';
}
  1. 使用该model类进行查询。
$codeArr = YourModel::select('code')->distinct()->get();
  1. 循环遍历查询结果,并根据code字段查询相应的数据。
$A_data = [];
foreach ($codeArr as $b) {
    $codeData = YourModel::where('code', $b->code)->select('cumulative', 'create_time')->get();
    $A_data[$b->code] = $codeData->pluck('cumulative')->toArray();
    $A_data[$b->code][] = $codeData->pluck('create_time')->toArray();
}

这样,$A_data就会按照特定格式进行处理,包含了查询结果的cumulative和create_time字段的值。最后返回$A_data即可。

完整代码如下:

class YourModel extends Model
{
    protected $table = 'your_table_name';
}

public function yourQueryFunction()
{
    $codeArr = YourModel::select('code')->distinct()->get();

    $A_data = [];
    foreach ($codeArr as $b) {
        $codeData = YourModel::where('code', $b->code)->select('cumulative', 'create_time')->get();
        $A_data[$b->code] = $codeData->pluck('cumulative')->toArray();
        $A_data[$b->code][] = $codeData->pluck('create_time')->toArray();
    }

    return $A_data;
}

以上就是在PHP的model层中使用数据库查询语句查询数据,并将查询结果按照特定格式进行处理的解决方案。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^