【问题标题】:Laravel 4 deleting multiple models related by foreign keysLaravel 4删除外键相关的多个模型
【发布时间】:2014-03-04 07:40:34
【问题描述】:

所以我有三个模型,当其中一个 dAgency 被删除时 delete-() 我希望所有三个模型都被删除。问题是其中两个正在被删除,而最高父级 DemDataSet 没有被删除。另外,当我打电话时:

echo "<pre>", dd(dAgency::find(21)->DemographicReport()->DemDataSet()->get()), "</pre>";

我收到此错误:Call to undefined method Illuminate\Database\Query\Builder::DemDataSet() 但是当我尝试时:

echo "<pre>", dd(dAgency::find(21)->DemographicReport()->get()), "</pre>";

它有效。所以我知道问题是我的 DemDataSet 模型之间的关系。下面是我的模型:

<?php

class DemDataSet extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'DEMDataSet';
    protected $primaryKey = 'pk_DEMDataSet';

    public function DemographicReport(){
        return $this->hasOne('DemographicReport','fk_DEMDataSet','pk_DEMDataSet');
    }


}

class DemographicReport extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'DemographicReport';
    protected $primaryKey = 'pk_DemographicReport';

    public function DemDataSet (){
        return $this->belongsTo('DemDataSet','fk_DEMDataSet','pk_DEMDataSet');
    }

    public function dAgency(){
        return $this->hasOne('dAgency','fk_DemographicReport','pk_DemographicReport');
    }

    public function delete(){
        parent::delete();
        return $this->DemDataSet()->delete();
    }

}

class dAgency extends Eloquent {

    public $timestamps = false;
    protected $connection = 'epcr_dem_data';
    protected $table = 'dAgency';
    protected $primaryKey = 'pk_dAgency';

    public function DemographicReport(){
        return $this->belongsTo('DemographicReport','fk_DemographicReport','pk_DemographicReport');
    }

    public function dAgency_10(){
        return $this->hasMany('dAgency_10','fk_dAgency','pk_dAgency');
    }

    public function delete(){
        parent::delete();
        return $this->DemographicReport->delete();
    }

}

?>

我已经和这个摔跤了两天了!非常感谢您抽出宝贵时间查看此内容。

【问题讨论】:

    标签: php model laravel-4 foreign-keys


    【解决方案1】:

    用关系查询laravel模型的正确方法是这样的:

    //This is to pull all DemDataSet from DemographicReport
    dAgency::find(21)->DemographicReport()->first()->DemDataSet;
    
    //If you need further query, then you call DemDataSet as a method
    dAgency::find(21)->DemographicReport()->first()->DemDataSet()->where('your condition here')->get();
    

    【讨论】:

    • 没用。当我尝试第一行时,我收到此错误:Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$DemDataSet 但这有效:DemographicReport::find(18)-&gt;DemDataSet-&gt;get() 所以我无法将 dAgency 与 DemDataSet 建立关系。我在您提供的第二行代码中收到未定义的方法错误。
    • 好吧,编辑了解决方案,虽然它可能不是您想要的,所以基本上这将从属于 dAgency 的第一个 DemographicReport 返回 DemDataSet,id=21
    • 您的新更新修复了错误,但它会导致额外的查询(两个连接表中的每一个查询)。我应该更具体地回答我的问题,但是我将如何使用 Eager 来做到这一点?我可以做到dAgency::with('DemographicReport')-&gt;find($id),但是当我尝试将 DEMDataSet 放入with() 时,它会抛出与以前相同的错误。我猜这是因为我的 dAgency 类中没有 DemDataSet() 方法,但是由于这些表之间没有直接关系(它们通过另一个表相关),我该怎么做?
    • 我想知道是否可以稍微改变你的表结构,假设 DemDataSet 只属于 1 个 DemographicReport 而 DomographicReport 只属于 1 个 dAgency,如果你只是使用 dAgency_id 将 FK 添加到 DemoDataSet 表中,这样您就可以在 2 之间建立直接关系。
    • 不,我不能真正改变结构,但好消息是它正在工作!我想我只是在围绕模型类本身时遇到问题。你是最后一次更新让我走上了正确的道路,所以我会把它标记为答案。感谢您的帮助 - 获得 L4 帮助的好地方并不多,所以似乎没有大型 L$ 社区。​​span>
    猜你喜欢
    • 2018-04-22
    • 2015-02-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多