[英]How to dynamically build this query - Laravel/Lumen
我有來自用戶的以下輸入:
array (
'id_coretable' => 1,
'Internal_key' => 'UPDATED1',
'extensiontable_itc' =>
array (
'description_itc' => 'UPDATED1',
),
'extensiontable_sysops' =>
array (
'description_sysops' => 'UPDATED1',
),
)
其內容將更新以下模型:
array (
'id_coretable' => 1,
'Internal_key' => 'TESTKEY_1',
'extensiontable_itc' =>
array (
'description_itc' => 'EXTENSION_ITC_1',
),
'extensiontable_sysops' =>
array (
'description_sysops' => 'EXTENSION_SYSOPS_1',
),
)
此模型是使用以下代碼創建的:
$joinAsArray = coretable::with($permittedTables)->find(1);
其中$permittedTables
是一個表名數組,它確定要與 coretable 連接的表。
現在,我花了幾個小時思考如何正確地循環模型,如果不將模型序列化為普通數組或類似的東西,這是不可能的。 但這並沒有真正的幫助,因為我想在這里更新模型,如果我只是將它轉換為數組,那么我就會失去與 DB/Model 的連接。
所以我現在使用不同的方法。 我正在循環用戶輸入,它總是與模型具有相同的結構和索引。 然后我使用來自非常好循環的 userinputarray 的鍵和數組形式的舊模型的副本,以確定模型的屬性,該屬性應使用相應的輸入數據進行更新。 這是我的代碼目前的樣子:
foreach($input as $key => $value){
foreach($modelAsArray as $keyOld => $valueOld){
//$keyOld is ALWAYS the same key as the "new" one.
//$keyOld was only chosen to both distinguish from the outer loops $key and to
//maintain its relationship to $valueOld, which is indeed the old value! ;)
coretable::with($permittedTables)->where($key, $valueOld)->update([$key => $value]);
}
}
現在,這段代碼非常適合用於外部數組。 我確實可以通過這種方式更新“Internal_key”字段。 然而,一旦我們達到了以“嵌套數組”的形式表示連接表的點,事情就會向南。 我收到以下錯誤消息:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'extensiontable_itc' in 'where clause' (SQL: update `coretable` set `extensiontable_itc` = {"description_itc":"UPDATED1"}, `coretable`.`updated_at` = 2020-02-06 16:07:06 where `extensiontable_itc` = UPDATED1)
我可以看出這是從哪里來的。 它試圖在 coretable 上找到一個列extensiontable_itc
,它顯然不存在,因為它是它自己的關系,僅通過 FK 連接到 coretable。
我想到處理這個問題的第一件事是通過 $key 動態插入其名稱來調用另一個模型。 然后我將不得不遍歷嵌套數組的鍵和值,基本上做與我對外部數組所做的相同的事情。
這可能會消耗相當多的資源,但該軟件僅供內部使用,我們的數據庫服務器可能能夠處理這種負載。 它也很hacky,我認為^^
那么,有沒有人可以給我另一個比我的更優雅、更少資源、更少黑客的解決方案?
編輯:根據評論中的要求,這是我的遷移:
核心表
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCoretable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('coretable', function (Blueprint $table) {
$table->bigIncrements('id_coretable');
$table->string('Internal_key')->nullable(false)->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('coretable');
}
}
擴展表_itc
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateExtensiontableItc extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('extensiontable_itc', function (Blueprint $table) {
$table->bigIncrements('id_extensiontable_itc');
$table->bigInteger('coretable_id')->unsigned()->unique()->nullable(false);
$table->foreign('coretable_id', 'fk_extensiontable_itc_coretable')->references('id_coretable')->on('coretable');
$table->string('description_itc')->nullable(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('extensiontable_itc');
}
}
extensiontable_sysops
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ExtensiontableSysops extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('extensiontable_sysops', function (Blueprint $table) {
$table->bigIncrements('id_extensiontable_sysops');
$table->bigInteger('coretable_id')->unsigned()->nullable(false)->unique();
$table->foreign('coretable_id', 'fk_extensiontable_sysops_coretable')->references('id_coretable')->on('coretable');
$table->string('description_sysops')->nullable(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('extensiontable_sysops');
}
}
SQLSTATE [42S22]:未發現柱:1054未知列'extensiontable_itc'在'where子句'(SQL:更新
coretable
組extensiontable_itc
= { “description_itc”: “UPDATED1”},coretable
。updated_at
= 2020年2月6日16時07分:06 其中extensiontable_itc
= UPDATED1)
您不能通過with('relation')->update()
更新關系數據。
我將根據您的遷移文件制作一個示例來實現這一點。
核
namespace App;
use App\ExtensiontableItc;
use App\ExtensiontableSysops;
use Illuminate\Database\Eloquent\Model;
class Core extends Model
{
protected $table = 'coretable';
protected $primaryKey = 'id_coretable';
protected $fillable = [
'id_coretable',
'Internal_key',
];
public function extensiontable_itc()
{
return $this->hasOne(ExtensiontableItc::class, 'coretable_id', 'id_coretable');
}
public function extensiontable_sysops()
{
return $this->hasOne(ExtensiontableSysops::class, 'coretable_id', 'id_coretable');
}
}
擴展表
namespace App;
use Illuminate\Database\Eloquent\Model;
class ExtensiontableItc extends Model
{
protected $table = 'extensiontable_itc';
protected $primaryKey = 'id_extensiontable_itc';
protected $fillable = [
'coretable_id',
'description_itc',
];
}
擴展表管理員
namespace App;
use Illuminate\Database\Eloquent\Model;
class ExtensiontableSysops extends Model
{
protected $table = 'extensiontable_sysops';
protected $primaryKey = 'id_extensiontable_sysops';
protected $fillable = [
'coretable_id',
'description_sysops',
];
}
$permittedTables = ['extensiontable_itc', 'extensiontable_sysops'];
$core = Core::with($permittedTables)->find(1);
你會得到(根據你的例子)
array:6 [
"id_coretable" => 1
"Internal_key" => "TESTKEY_1"
"created_at" => "2020-02-07 18:05:50"
"updated_at" => "2020-02-07 18:05:50"
"extensiontable_itc" => array:5 [
"id_extensiontable_itc" => 1
"coretable_id" => 1
"description_itc" => "UPDATED1"
"created_at" => "2020-02-07 18:17:08"
"updated_at" => "2020-02-07 11:32:44"
]
"extensiontable_sysops" => array:5 [
"id_extensiontable_sysops" => 1
"coretable_id" => 1
"description_sysops" => "UPDATED1"
"created_at" => "2020-02-07 18:17:21"
"updated_at" => "2020-02-07 11:32:44"
]
]
要更新您的關系,您需要通過Core
模型調用extensiontable_itc
和extensiontable_sysops
預先加載。
$input = [
'id_coretable' => 1,
'Internal_key' => 'TESTKEY_1',
'extensiontable_itc' => [
'description_itc' => 'EXTENSION_ITC_1',
],
'extensiontable_sysops' => [
'description_sysops' => 'EXTENSION_SYSOPS_1',
],
];
$permittedTables = ['extensiontable_itc', 'extensiontable_sysops'];
$core = Core::with($permittedTables)->find(1);
// Update extensiontable_itc
$core->extensiontable_itc->update($input['extensiontable_itc']);
// Update extensiontable_sysops
$core->extensiontable_sysops->update($input['extensiontable_sysops']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.