Fetching the file path attached to a model through a parent model relation?

- 1 answer

Ad

I have a Model called application which can fetch the relation of another model called resume. In resume model is further relation which has a $attachOne relation of a file.

public $attachOne = [
    'resume' => ['System\Models\File','public' => false, 'delete' => true]
    ];

The application has a resume attached to it though $belongsTo.

public $belongsTo = [
    'user' => ['RainLab\User\Models\User', 'table' => 'users'],
    'resume' => ['Acme\Job\Models\Resume']
    ];

I can fetch all the fields in the application model for resume, such as user_id, display_name etc but when I try to access the file attached to resume it says column not found. Here is what I have in the application model that accesses the Resume data.

resume:
    label: Resume File
    type: relation
    disabled: true
    select: display_name

I can select all fields in that table but can't access the file attached to the resume table. Is there any way I can access the attached system file in the application model?

Ad

Answer

Ad

You can use a partial to access to your related model attachment, something like this.

Step 1.

Setup your fields.yaml

resume:
  label: Resume File
  type: partial
  path: $/acme/jobs/models/resume/_resume.htm
  1. Create a partial inside of the resume's model directory to print out the download link to the file

  2. Inside of the partial you have access to $model variable that holds the current instance of the model if exists, for instance:

/acme/jobs/models/resume/_resume.htm

<?php if ($model->resume->resume): ?>
 <div class="form-group  text-field span-full form-pad" data-field- name="resume" id="Form-field-resume-group">
    <div>
        <a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="<?= $model->resume->resume->getPath(); ?>">Download   resume</a>
    </div>
</div>
<?php endif; ?>
Ad
source: stackoverflow.com
Ad