国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Laravel 9 Eloquent 模型 - 模型中保留的列名稱
P粉309989673
P粉309989673 2023-09-03 14:36:02
0
1
730
<p>我有一個(gè) postgres 資料庫,其中有一個(gè)表,其中有一列名為「attributes」。 </p> <p>屬性列是 jsonb 類型,所以我使用 Eloquent 轉(zhuǎn)換:</p> <pre class="lang-php prettyprint-override"><code>protected $casts = [ 'attributes' => AsArrayObject::class, ]; </code></pre> <p>這似乎會(huì)導(dǎo)致問題,因?yàn)椤笇傩浴挂呀?jīng)是 Eloquent 模型屬性,似乎沒有任何別名列名稱的規(guī)定。 </p> <p>所以這一行:</p> <pre class="lang-php prettyprint-override"><code>$this->attributes['a_property_of_the_attributes_jsonb_field'] = 'hello word!'; </code></pre> <p>似乎正在訪問內(nèi)部 Eloquent 模型屬性 - 而不是我的資料庫表中的“attributes”字段,從而導(dǎo)致以下錯(cuò)誤:</p> <pre class="brush:php;toolbar:false;">SQLSTATE[42703]: Undefined column: 7 ERROR: column "a_property_of_the_attributes_jsonb_field" of relation "); LINE 1: update "mytable" set "a_property_of_the_attributes_jsonb_field" = $1 where "mypk" = ...</pre> <p>我無法重新命名該列,因?yàn)槠渌?PHP 專案正在使用該資料庫。 </p> <p>如何將模型中的「屬性」欄位作為 ArrayObject 存??? </p>
P粉309989673
P粉309989673

全部回覆(1)
P粉034571623

簡(jiǎn)單的解決方法是使用 ArrayObject 存取器方法:

$this['attributes'] = ['x' => 'y'];
$stuff = $this['attributes'];

注意:仍然需要定義「attributes」轉(zhuǎn)換才能以陣列形式存取底層 jsonb 欄位:

protected $casts = [
    'attributes' => AsArrayObject::class
];
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板