第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

由于條件關(guān)系而對(duì) null 調(diào)用成員函數(shù) addEagerConstraints()

由于條件關(guān)系而對(duì) null 調(diào)用成員函數(shù) addEagerConstraints()

PHP
慕萊塢森 2024-01-19 15:03:19
我必須處理一個(gè) Laravel 7 應(yīng)用程序,該應(yīng)用程序的數(shù)據(jù)庫設(shè)計(jì)不理想,導(dǎo)致標(biāo)題中提到的錯(cuò)誤。數(shù)據(jù)庫看起來像這樣:mains- id- sub_typesubs_a- id- main_idsubs_b- id- main_id然后我有一個(gè)Main帶有方法的類sub:public function sub(){    switch($this->sub_type) {        case 'a':            return $this->hasOne('SubTypeA');            break;        case 'b':            return $this->hasOne('SubTypeB');            break;        default:            return null;    }}此代碼適用于 99% 的情況,但 Laravel 有時(shí)會(huì)加載 的空實(shí)例Main,然后嘗試加載關(guān)系。這是行不通的,因?yàn)?method 的默認(rèn)值sub是null.重組數(shù)據(jù)庫已列入待辦事項(xiàng)列表,但目前沒有任何幫助。我需要什么選項(xiàng)來阻止 Laravel 嘗試sub在空對(duì)象上加載關(guān)系?
查看完整描述

3 回答

?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

我知道這是某種預(yù)期,但嘗試返回一個(gè)空關(guān)系?


public function sub()

{

? ? switch($this->sub_type) {

? ? ? ? case 'a':

? ? ? ? ? ? return $this->hasOne('SubTypeA');

? ? ? ? ? ? break;

? ? ? ? case 'b':

? ? ? ? ? ? return $this->hasOne('SubTypeB');

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? return $this->newQuery();? // or newQueryWithoutScopes()

? ? }

}

它應(yīng)該防止 addEagerConstraints() 對(duì) null 的錯(cuò)誤。



查看完整回答
反對(duì) 回復(fù) 2024-01-19
?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

我想指出的幾件事是,您的關(guān)系不應(yīng)該是有條件的,在將默認(rèn)關(guān)系設(shè)置為您想要的默認(rèn)關(guān)系之后,您可以定義范圍并管理內(nèi)容或使用withDefault在失敗情況下返回某些內(nèi)容。

關(guān)于在 null 上調(diào)用成員函數(shù)的錯(cuò)誤:- 下面是另一個(gè)示例

<?php

class MyClass{

? ? function bar(){

? ? ? ? echo "something";

? ? }

}


class MyAnotherClass{

? ? function foo(){

? ? ? ? if (1>2) {

? ? ? ? ? ? $obj = new MyClass();

? ? ? ? ? ? $obj->x = $x;

? ? ? ? ? ? $obj->y = $y;

? ? ? ? ? ? $obj->save();

? ? ? ? ? ? return $obj;

? ? ? ? } else {

? ? ? ? ? ? return null;

? ? ? ? }

? ? }

}


$myAnotherObj = new MyAnotherClass();

$myClass = $myAnotherObj->foo();

$myClass->bar()

?>

與其這樣做,我更愿意拋出異常并處理它,這樣我就能得到失敗的具體原因,在 Laravel救援輔助函數(shù)中你可以選擇使用。


<?php

class MyClass{

? ? function bar(){

? ? ? ? echo "something";

? ? }

}


class MyAnotherClass{

? ? function foo(){

? ? ? ? if (1>2) {

? ? ? ? ? ? $obj = new MyClass();

? ? ? ? ? ? $obj->x = $x;

? ? ? ? ? ? $obj->y = $y;

? ? ? ? ? ? $obj->save();

? ? ? ? ? ? return $obj;

? ? ? ? } else {

? ? ? ? ? ? throw new Exception("could not create my class object", 100); // Better to create custom exception class here

? ? ? ? }

? ? }

}


$myAnotherObj = new MyAnotherClass();

try {

? ? $myClass = $myAnotherObj->foo();

? ? $myClass->bar();

} catch(Exception $e) {

? ? echo $e->getMessage();

}

?>

如果對(duì)我來說數(shù)據(jù)不是那么重要我會(huì)考慮創(chuàng)建一個(gè)空對(duì)象


<?php

? ? class MyClass{

? ? ? ? function bar(){

? ? ? ? ? ? echo "something";

? ? ? ? }

? ? }

? ??

? ? class MyAnotherClass{

? ? ? ? function foo(){

? ? ? ? ? ? $obj = new MyClass();

? ? ? ? ? ? if (1>2) {

? ? ? ? ? ? ? ? $obj->x = $x;

? ? ? ? ? ? ? ? $obj->y = $y;

? ? ? ? ? ? ? ? $obj->save();??

? ? ? ? ? ? }?

? ? ? ? ? ? return $obj;

? ? ? ? }

? ? }

? ??

? ? $myAnotherObj = new MyAnotherClass();

? ? $myClass = $myAnotherObj->foo();

? ? $myClass->bar()

? ?>

但是如果您正在使用該對(duì)象屬性進(jìn)行操作,那么屬性將為 null 而不是對(duì)象,因此根據(jù)您在使用它時(shí)的紀(jì)律,您可以做出決定。


我想如何處理你的情況?


異常類


<?php


namespace App\Exceptions;


use Exception;


class SubTypeNotFound extends Exception {

? ? public function report()

? ? {

? ? ? ? \Log::debug('Could not find this subtype');

? ? }

}

?>


模型類


<?php

class Mains extends Model

{

? ? public function subA()

? ? {

? ? ? ? return $this->hasOne(SubTypeA::class);

? ? }? ??


? ? public function subB()

? ? {

? ? ? ? return $this->hasOne(SubTypeB::class);

? ? }


? ? public function scopeSub($query, $type)

? ? {

? ? ? ? return $query

? ? ? ? ? ->when($type === 'a',function($q){

? ? ? ? ? ? ? return $q->with('subA');

? ? ? ? ?})

? ? ? ? ?->when($type === 'b',function($q){

? ? ? ? ? ? ? return $q->with('subB');

? ? ? ? ?}),function($q){

? ? ? ? ? ? ?throw SubTypeNotFound();

? ? ? ? ?});

? ? }

}

?>

檢索它時(shí)


try {

? ? $sub = Mains::sub('a')->get();

} catch(SubTypeNotFound $e) {

? ? return $e->getMessage();

}

如果有,$this->sub_type您可以避免使用該type參數(shù)。


查看完整回答
反對(duì) 回復(fù) 2024-01-19
?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以簡單地將 if 條件放在 switch case 之前。


public function sub()

{

    if($this->sub_type){

        switch($this->sub_type) {

            case 'a':

                return $this->hasOne('SubTypeA');

                break;

            case 'b':

                return $this->hasOne('SubTypeB');

                break;

            default:

                return null;

        }

    }else{

        return null

    }       

}


查看完整回答
反對(duì) 回復(fù) 2024-01-19
  • 3 回答
  • 0 關(guān)注
  • 177 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)