【问题标题】:Populating parameters with default value using relationships in Laravel 5.2使用 Laravel 5.2 中的关系使用默认值填充参数
【发布时间】:2017-02-24 08:13:11
【问题描述】:

好的,我有一个问题。我正在编写一个非常复杂的报告,界面使用 Laravel 5.2。现在的问题是,根据某些条件,用户并不总是需要填写所有参数。但是,为了简单起见,我这样做是为了让报告始终接收完整的参数集。所以我有三张桌子:

tblReportParam
ID
ParamName
DefaultValue

tblReportParamValue
ParamID
ReportID
Value

tblReport
ID
UserName

现在,我有一个可行的解决方案,但出于某种原因,我感觉我应该能够更好地利用模型和关系。我基本上只有我的模型和控制器,并使用 SQL 解决了整个问题。

感觉有点接近this,但不完全一样。所以基本上,您需要始终加载/保存所有参数。如果参数 x 实际上是由用户定义的,那么您使用他的定义,否则您将使用 tblReportParam 中定义的默认值。任何人都知道如何做到这一点?

编辑:

好的,所以我检查了 Eddy 的答案并尝试在我们的系统中使用它,但我的另一位同事开始在 tblReport 和 tblReportParam 表之间实现多对多关系,其中 tblReportParamValue 作为枢轴所以我'我在为我们的系统调整这个解决方案时遇到了一些困难。这是两个模型:

class ReportParam extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'tblReportParam';

    protected $primaryKey = 'ID';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['ID', 'NomParam', 'DefaultValue'];

    public function renourapports()
    {
        return $this->belongsToMany('App\Report');
    }

}

class Report extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'tblReport';

    protected $primaryKey = 'ID';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['ID', 'NoEmploye', 'NoClient', 'NoPolice', 'DateCreation', 'DateModification', 'runable', 'DernierEditeur'];

    public $timestamps = false;

    public function params()
    {
        return $this->belongsToMany('App\ReportParam ', 'tblReportParamValue', 'ReportID', 'ParamID')->withPivot('Valeur');
    }

}

现在这实际上是一个非常简洁的解决方案,但它仅在参数实际位于数据透视表中时才有效(即关系实际存在)。我们想要的是对于不在数据透视表中的参数,我们只需要它们的默认值。在这种情况下,Eddy 的解决方案可以工作吗?

【问题讨论】:

    标签: php laravel relationships


    【解决方案1】:

    使用 Eloquent 模型

    class ReportParam extends Model
    {
        public function paramValue() {
            return $this->hasOne('App\ReportParamValue', 'ParamID');
        }
    
        public function getDefaultValueAttribute($value) {
            if ( $this->paramValue ) return $this->paramValue->Value; //relationship exists
            return $this->DefaultValue;
        }
    }
    
    $reportParam->value; // return the relationship value or the default value;
    

    更新

    现在tblReportParamValue 是一个数据透视表,您应该重新定义您的关系。在ReportParam模型中添加

    public function reports() {
        return $this->belongsToMany('App\Report', 'tblReportParamValue', 'ParamID', 'ReportID')->withPivot('Value');
    }
    

    而在Report 模型中,定义相反

    public function params() {
        return $this->belongsToMany('App\ReportParam', 'tblReportParamValue', 'ReportID', 'ParamID')->withPivot('Value');
    }
    

    现在从 ReportParam 获取默认值变得太复杂了,因为一个 ReportParam 会有很多报告。因此,$reportParam->reports() 将带回在数据透视表中使用该 paramID 的每个报告。因此,寻找一个值意味着要浏览所有报告。我们可以通过改变函数定义来避免这种情况。

    public function getDefaultValue($reportID) {
        $reportValue = $this->reports()->wherePivot('ReportID', $reportID)->first();
        return $reportValue ? $this->reportValue->Value : $this->DefaultValue;
    }
    
    //In Controller 
    $report = Report::find(1);
    $reportParam = ReportParam::find(1);
    $reportParam->getDefaultValue($report->ID);
    

    好的,我认为这可能有效。如果没有,我真的很抱歉,我不知道更好。

    【讨论】:

    • 您的解决方案可以构建成多对多的关系吗?
    • 当然可以,但是您必须更改第二个功能才能检查您的所有收藏。
    • 我很难想象你的意思。你能发布一些代码吗?在此先感谢:)。
    • 好吧,如果您在顶部检查我的表定义,在 ParamValue 中有 ReportID 和 ParamID,因此从技术上讲,ParamID 可以在此表中出现多次。所以不应该反过来吗?一个 ParamValue 只能关联一个 ReportParam,因为 ReportParam 是实际定义默认值的地方?
    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2019-02-09
    相关资源
    最近更新 更多