Laravel 5.8调用模型上未定义的关系[sourceClient]

时间:2019-07-12 08:00:45

标签: laravel laravel-5 eloquent

我有一个模型WorkflowStep,它与TransportWorkflowStep和TransformworkflowStep具有多义关系

我的transportWorkflowStep与客户端还有其他关系。

当我在代码中执行此操作时:

    /** @var TransportWorkflowStep $workflowStepEntity */
    $workflowStepEntity = $workflowStep->entity;

    try {
        var_dump($workflowStepEntity->sourceClient);
    } catch (\Exception $exception) {
        dd($exception);
    }

我收到此错误:

  

调用模型[App \ TransformWorkflowStep]上未定义的关系[sourceClient]。

第一个奇怪的事情是我在dd($ workflowstep)时正在使用TransportWorkflowStep,所以我不知道他为什么抱怨TransformWorkflowStep

我的WorkflowStep模型:

    public function entity(): MorphTo
    {
        return $this->morphTo();
    }

我的TransportWorkflowStep模型:

    public function workflowStep(): MorphOne
    {
        return $this->morphOne(WorkflowStep::class, 'entity');
    }

    public function sourceClient(): BelongsTo
    {
        return $this->belongsTo(Client::class, 'source_id', 'id');
    }

    public function destinationClient(): BelongsTo
    {
        return $this->belongsTo(Client::class, 'destination_id', 'id');
    }

我的TransformWorkflowStep模型:

    public function workflowStep(): MorphOne
    {
        return $this->morphOne(WorkflowStep::class, 'entity');
    }

编辑

$ workflowStepEntity的转储:

object(App\TransportWorkflowStep)[439]
  protected 'table' => string 'transport_workflow_steps' (length=24)
  protected 'fillable' => 
    array (size=4)
      0 => string 'source_id' (length=9)
      1 => string 'destination_id' (length=14)
      2 => string 'source_options' (length=14)
      3 => string 'destination_options' (length=19)
  protected 'connection' => string 'mysql' (length=5)
  protected 'primaryKey' => string 'id' (length=2)
  protected 'keyType' => string 'int' (length=3)
  public 'incrementing' => boolean true
  protected 'with' => 
    array (size=0)
      empty
  protected 'withCount' => 
    array (size=0)
      empty
  protected 'perPage' => int 15
  public 'exists' => boolean true
  public 'wasRecentlyCreated' => boolean false
  protected 'attributes' => 
    array (size=8)
      'id' => string '4d32e11c-6453-4f48-9419-7c5cbd647128' (length=36)
      'source_id' => string '8c826f52-a78c-4ef5-acf5-8e6fd59004a0' (length=36)
      'destination_id' => string '921f0d90-2fae-496e-9991-68128e87d6d5' (length=36)
      'source_options' => string 'source options' (length=14)
      'destination_options' => string 'destination options' (length=19)
      'created_at' => string '2019-07-04 11:17:35' (length=19)
      'updated_at' => string '2019-07-12 08:39:58' (length=19)
      'deleted_at' => null
  protected 'original' => 
    array (size=8)
      'id' => string '4d32e11c-6453-4f48-9419-7c5cbd647128' (length=36)
      'source_id' => string '8c826f52-a78c-4ef5-acf5-8e6fd59004a0' (length=36)
      'destination_id' => string '921f0d90-2fae-496e-9991-68128e87d6d5' (length=36)
      'source_options' => string 'source options' (length=14)
      'destination_options' => string 'destination options' (length=19)
      'created_at' => string '2019-07-04 11:17:35' (length=19)
      'updated_at' => string '2019-07-12 08:39:58' (length=19)
      'deleted_at' => null
  protected 'changes' => 
    array (size=0)
      empty
  protected 'casts' => 
    array (size=0)
      empty
  protected 'dates' => 
    array (size=1)
      0 => string 'deleted_at' (length=10)
  protected 'dateFormat' => null
  protected 'appends' => 
    array (size=0)
      empty
  protected 'dispatchesEvents' => 
    array (size=0)
      empty
  protected 'observables' => 
    array (size=0)
      empty
  protected 'relations' => 
    array (size=0)
      empty
  protected 'touches' => 
    array (size=0)
      empty
  public 'timestamps' => boolean true
  protected 'hidden' => 
    array (size=0)
      empty
  protected 'visible' => 
    array (size=0)
      empty
  protected 'guarded' => 
    array (size=1)
      0 => string '*' (length=1)
  protected 'forceDeleting' => boolean false

编辑完整代码

WorkflowJob

<?php

namespace App\Jobs;

use App\Workflow;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class WorkflowJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    /**
     * @var Workflow
     */
    protected $workflowId;

    /**
     * Create a new job instance.
     *
     * @return void
     * @param string $workflowId
     */
    public function __construct(string $workflowId)
    {
        $this->workflowId = $workflowId;
    }

    /**
     * Execute the job.
     *
     * @return \Illuminate\Foundation\Bus\PendingDispatch
     * @throws \Exception
     */
    public function handle()
    {
        try {
            $this->execute(Workflow::find($this->workflowId));
        } catch (\Exception $exception) {
            throw $exception;
        }
    }

    /**
     * Execute the WorkflowJob.
     *
     * @param Workflow $workflow
     */
    private function execute(Workflow $workflow)
    {
        // start a new workflow instance
        $workflowInstance = $this->startWorkflowInstance($workflow);

        // start the first step
        StepJob::dispatch($workflowInstance, $workflow->workflowSteps->first()->order);
    }

    /**
     * Start a new WorflowInstance for a Workflow.
     *
     * @param Workflow $workflow
     * @return \Illuminate\Database\Eloquent\Model
     */
    private function startWorkflowInstance(Workflow $workflow)
    {
        return $workflow->workflowInstances()->create([
            'started_at' => Carbon::now(),
        ]);
    }
}

StepJob:

<?php

namespace App\Jobs;

use App\Client;
use App\File;
use App\TransportWorkflowStep;
use App\WorkflowInstance;
use App\WorkflowStep;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class StepJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    const LOG_START = 'start';
    const LOG_END_SUCCESS = 'end';
    const LOG_END_FAIL = 'fail';

    /**
     * @var WorkflowInstance
     */
    private $workflowInstance;

    /**
     * @var int
     */
    private $stepCount;

    /**
     * Create a new job instance.
     *
     * @param WorkflowInstance $workflowInstance
     * @param int $order
     */
    public function __construct(WorkflowInstance $workflowInstance, int $order)
    {
        $this->workflowInstance = $workflowInstance;
        $this->stepCount = $order;
    }

    /**
     * Execute the job.
     */
    public function handle()
    {
        if ($this->stepCount <= $this->workflowInstance->workflow->workflowSteps->count()) {
            /** @var WorkflowStep $workflowstep */
            $workflowstep = $this->workflowInstance->workflow->workflowSteps->where('order', $this->stepCount)->first();

            $workflowStepEntity = $workflowstep->entity;

            if ($workflowStepEntity instanceof TransportWorkflowStep) {
                /* @var TransportWorkflowStep $workflowStepEntity */
                try {
                    var_dump($workflowStepEntity->sourceClient);
                } catch (\Exception $exception) {
                    dd($exception);
                }
                var_dump($workflowstep->entity->sourceClient);
            } else {
                $workflowstep->entity->transformer
            }

            StepJob::dispatch($this->workflowInstance, $this->stepCount + 1);
        } else {
            return;
        }
    }
}

workflow_steps

transport_workflow_steps

transform_workflow_steps

1 个答案:

答案 0 :(得分:0)

您发消息说:Call to undefined relationship [sourceClient] on model [App\TransformWorkflowStep].

您的模型定义(TransformWorkflowStep):

public function workflowStep(): MorphOne
{
    return $this->morphOne(WorkflowStep::class, 'entity');
}

错误消息正确,在TransformWorkflowStep模型上没有sourceClient关系。您应该定义它或停止使用它;)

编辑: 您可以尝试这样做:

foreach($workflowStepCollection as $workflowStep)
{
    dump($workflowStep->entity);
    try
    {
        $x = $workflowStep->sourceClient;
    }
    catch(\Exception $ex)
    {
        dd($ex->getMessage());
    }
}

当它失败(dd)时,dd之前的最后一个结果应该确切地告诉您哪一个正在变坏。