Thinkphp5 爬過的坑

記錄使用過程中遇到的坑

  1. * validate 驗證器中的date規則 * 
    如下validate :
class PartTime extends Validate { protected $rule = [ 'birtyday' => 'require|date' ]; protected $message = [ 'birtyday.require' => '生日必填', 'birtyday' => '生日格式錯誤', ]; } // 調用驗證 $result = $this->validate($data, 'PartTime'); if (true !== $result) { return $this->outMessage([], '-2', $result); }

問題(坑): 
當data裏的birtyday 值爲:‘x’一位字符串時,驗證不會檢測到錯誤,這明顯不是時間格式字符串。 
但是’xx’ 一位以上的字符串,驗證能檢測到錯誤。 
我們看看它的源碼:

case 'date':
   // 是否是一個有效日期
   $result = false !== strtotime($value);
   break;

問題就在這裏了,我們可以測試以下:

if (false !== strtotime('s')) {
    echo 'ssss';
}

輸出 : ssss 
所以這個驗證方法是有問題的,稍微改下:

case 'date':
  // 是否是一個有效日期
  $result = (false !== strtotime($value) && strlen($value) > 1);
  break;

—————————————–完美分割線————————————————– 
2. * 模型with方法 * 
這個坑在模型關聯“懶加載”的方法with上,錯誤寫法:

public function getDataWithRelation($xxId)
{
    return $this->where('xx_id', $xxId)->with('post, industry')->select();
}
public function post()
{
    return $this->beLongsTo('Post', 'post_id', 'id')->field('id, name');
}

public function industry()
{
    return $this->beLongsTo('Industry', 'industry_id', 'id')->field('id, name');
}
// 注意上面的with裏面第二個關聯模型xxz之前有個空格

這樣寫法,測試會報錯: 
這裏寫圖片描述
我的編碼習慣,都是會在’,’後面跟個空格,在這裏就會報錯。 
看他源碼:它是直接把with裏面的參數,直接用explode(‘,’, $with),然後拼接字符串,這樣空格就存在問題了,優化的話,可以explode後,用trim去除空格,或者拼接之前trim。 
正確寫法,把那個空格去掉就可以了。

public function getDataWithRelation($xxId)
{
    return $this->where('xx_id', $xxId)->with('post,industry')->select();
}

—————————————–完美分割線————————————————– 
3. model 的create 和save 問題: 
問題處在allowField()方法,只能save用,create 不行。就是新增時如果想使用allowField()方法過濾非數據庫表字段,不能使用

$this::allowField(true)->create($data);

只能使用:

$this::allowField(true)->save($data);

我需要保存後返回新增的對象給我,所以要用到create,save只返回新增的ID。 
看源碼發現:create 方法提供第二個參數,field,可以設置,但是沒有allowField那麼方便。 
解決我的這個問題,辦法就是使用:

$this::allowField(true)->create($data, 'name, xxx,xx,xxxx');

—————————————–完美分割線————————————————– 
4. 模型關係配置問題 
在配置關聯關係時,如果要過濾字段,字段裏必須要有關聯關係的id,否則會報錯

return $this->hasMany('app\job\model\Company', 'userid', 'id')->field('id, name');

會報錯: 
這裏寫圖片描述
必須加上:

return $this->hasMany('app\job\model\Company', 'userid', 'id')->field('id, name, userid');

而在配置beLongsTo的時候就得跟上id。 
—————————————–完美分割線————————————————– 
5. 模型關聯查詢的巨坑 
配置好關聯關係後,如果是層級查詢時,同一個層級的多個子模型查詢會被最後一個覆蓋,直接見代碼:

$this->where('id', $id)->field('id, title, userid, post_id, salary_range_id, welfare_id, num, qualifications_id, job_year_id, address, linkman, tele, describe, update_time')->with('post,salaryRange,welfare,member.company.companyScale,member.company.companyNature,member.company.industry')->find();

這裏想要查詢出member.company 的companyNature,industry,companyScale,機會有大問題:

"member": {
    "id": 28,
    "company": {
        "id": 1,
        "name": "雲紫峯",
        "userid": 28,
        "company_scale_id": 2,
        "company_nature_id": 3,
        "industry_id": 2,
        "industry": {
            "id": 2,
            "name": "會計/金融/銀行/保險"
        }
    }
}

最後一個配置的是industry 會覆蓋之前的companyNature,companyScale 
* 問題已經找到,是with裏面寫法問題 * 
正確寫法:

$this->where('id', $id)->field('id, title, userid, post_id, salary_range_id, welfare_id, num, qualifications_id, job_year_id, address, linkman, tele, describe, update_time')->with(['post','salaryRange','welfare','member.company' => ['companyScale','companyNature','industry']])->find();

預加載同一個模型的多個子模型要使用數組方式傳入。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章