This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi, I use Builder, I want to bring out thumbnail, which is loaded in another field. But I get the error:
Call to a member function getPath() on null
Model
public $attachOne = [
'img' => 'System\Models\File'
];
Partial
<img src="<?php echo $model->img->getPath(); ?>" />
Last updated
mjauvin said:
Are you sure there's actually an image attached to this model record?
Yes, File upload shows the name of the file, and the url is valid
It's hard to answer without more details... where does the $model variable comes from in the partial? Show more of your code.
mjauvin said:
It's hard to answer without more details... where does the $model variable comes from in the partial? Show more of your code.
<?php namespace gohhan\Portfolio\Models;
use Model;
/**
* Model
*/
class Book extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
public $timestamps = false;
/**
* @var string The database table used by the model.
*/
public $table = 'vebprovajder_portfolio_kuhni';
/**
* @var array Validation rules
*/
public $rules = [
];
public $attachOne = [
'img' => 'System\Models\File'
];
}
Field Partial it displays thumbnail
<img src="<?php echo $model->img->getPath(); ?>" />
Controllers
<?php namespace gohhan\Portfolio\Controllers;
use Backend\Classes\Controller;
use BackendMenu;
class Book extends Controller
{
public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController', 'Backend\Behaviors\ReorderController' ];
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $reorderConfig = 'config_reorder.yaml';
public function __construct()
{
parent::__construct();
BackendMenu::setContext('gohhan.Portfolio', 'portfolio', 'book');
}
}
Last updated
mjauvin said:
It doesn't show where the $model variable used in your partial is assigned...
I stupidly copied $model variable from the documentation :)
Then how can I create a variable for a downloaded file and use in Partial?
Last updated
And where exactly do you want to show this thumbnail? In a backend form or on a front-end CMS page?
mjauvin said:
What does your
Models/book/fields.yaml
look like?
fields:
title:
label: Name
span: left
type: text
img:
label: 'Image'
mode: file
useCaption: true
thumbOptions:
mode: crop
extension: auto
span: right
type: fileupload
description:
label: Description
size: ''
span: auto
type: richeditor
thumbnail:
span: right
readOnly: 1
path: ~/plugins/gohhan/portfolio/models/img/thumbnail_field.htm
type: partial
Last updated
mjauvin said:
And where exactly do you want to show this thumbnail? In a backend form or on a front-end CMS page?
In a backend form
gohhan said:
mjauvin said:
It doesn't show where the $model variable used in your partial is assigned...
I stupidly copied $model variable from the documentation :)
Then how can I create a variable for a downloaded file and use in Partial?
~You're right, $model
should contains the model instance in your partial... ~
$model contains the FIELD instance for a field... $formModel contains the actual model used by the form.
Last updated
Try using $formModel instead of $model...
Like this:
<img src="<?= $formModel->img->getPath() ?>" />
Last updated
mjauvin said:
gohhan said:
mjauvin said:
It doesn't show where the $model variable used in your partial is assigned...
I stupidly copied $model variable from the documentation :)
Then how can I create a variable for a downloaded file and use in Partial?
~You're right,$model
should contains the model instance in your partial... ~$model contains the FIELD instance for a field... $formModel contains the actual model used by the form.
dump
It seems you were right, img is empty?
Last updated
mjauvin said:
Try using $formModel instead of $model...
Like this:
<img src="<?= $formModel->img->getPath() ?>" />
Call to a member function getPath() on null
It seems you were right, img is empty?
Are you trying to see the thumbnail BEFORE saving the model or after??
mjauvin said:
It seems you were right, img is empty?
Are you trying to see the thumbnail BEFORE saving the model or after??
After.
I think I've set myself a bad task and I'm taking your time.
You're right, if I open the first time the record then I will have a bug that img empty. So you need to check img for an empty value.
In my cases it is easier in fields.yaml to change mode: file
on mode: image
Then I'll see thumbnail. It's a pity that you couldn't learn how to display a uploaded image where you need to :)
I found a problem! Looked at the database, in the plugin table, the img column is empty. The image is uploaded but not recorded in the database. Why don't I know.
Last updated