我收到用户的以下输入:
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联接的表。
现在,我花了多个小时来思考如何正确地遍历一个模型,而且如果不将该模型序列化为一个普通数组等,这根本不可能。但这并没有真正的帮助,因为我想在这里更新模型,如果我只是将其转换为数组,那么我将失去与数据库/模型的连接。
所以我现在使用另一种方法。我正在循环用户输入,该用户输入将始终具有与模型相同的结构和索引。然后,我使用来自可很好循环的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动态插入其名称来调用另一个模型。然后,我将不得不遍历嵌套数组的键和值,基本上执行与外部数组相同的操作。
这可能会消耗相对可怕的资源,但是该软件仅用于内部目的,我们的数据库服务器可能将能够处理此负载。我认为它也很骇人^^
所以,还有其他人能给我比我更优雅,更少资源,更少麻烦的解决方案吗?
编辑:根据评论中的请求,这是我的迁移:
核心表
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');
}
}
extensiontable_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“ where子句”中的未知列“ extensiontable_itc”(SQL:更新
coretable
setextensiontable_itc
= {“ description_itc”:“ UPDATED1”},coretable
。[C0 ] = 2020-02-06 16:07:06其中updated_at
= UPDATED1)
您无法通过extensiontable_itc
更新关联数据。
我将根据您的迁移文件为实现该示例提供示例。
Core
with('relation')->update()
ExtensiontableItc
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');
}
}
ExtensiontableSysops
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"
]
]
模型调用extensiontable_itc
和extensiontable_sysops
紧急加载。
Core