-1
My problem is basically this:
I’ve linked 2 models to the official Cakephp 3 documentation and I can’t return one of them in the view (now Template in Cake 3).
The Code:
Work - Entity
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Work extends Entity
{
protected $_accessible = [
'project' => true,
'client' => true,
'filter' => true,
'tech_1' => true,
'tech_2' => true,
'tech_3' => true,
'tech_4' => true,
'job' => true,
'status' => true,
'link' => true,
];
}
Worksimage - Entity
namespace App\Model\Entity;
use Cake\ORM\Entity;
class WorksImage extends Entity
{
protected $_accessible = [
'photo' => true,
'photo_dir' => true,
'work_id' => true,
'work' => true,
];
}
Pagescontroller - Controller:
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
class PagesController extends AppController
{
public function portfolio()
{
$this->loadModel('Works');
$this->loadModel('WorksImages');
$works = $this->Works->find('all',['contain' => ['WorksImages'],'limit' => 10, 'order' => ['Works.created' => 'DESC']]);
$this->set(compact('works'));
}
}
Workstable - Table:
namespace App\Model\Table;
use App\Model\Entity\Work;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class WorksTable extends Table
{
public function initialize(array $config)
{
$this->table('works');
$this->displayField('project');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasOne('WorksImages', [
'foreignKey' => 'work_id'
]);
}
Worksimagestable - Table
namespace App\Model\Table;
use App\Model\Entity\WorksImage;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class WorksImagesTable extends Table
{
public function initialize(array $config)
{
$this->table('works_images');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Works', [
'foreignKey' => 'work_id',
'joinType' => 'INNER'
]);
}
Portfolio - View (Template)
<div class="container">
<div class="span12">
<h1>Portfólio</h1>
<div>
<?php foreach ($works as $work): ?>
<div>
<p><?= 'Conteúdo da tabela Works = ' . $work->project ?></p>
<p><?= 'Conteúdo da tabela WorksImages = ' . $work->work_id ?></p>
</div>
<?php endforeach ?>
</div>
</div>
</div>
I cannot return any value from the Worksimagestable model. Debugging realize that the tables are related, moreover, cake does not return any error in the view.
I can’t understand what’s wrong.
Thank you in advance for any help.
Grateful.
Got something? I have a similar problem and can’t find a solution.
– Marcelo de Andrade
Not yet. I will test an option that a stackoverflow user suggested in English.
– Downbeat
Can you tell me the suggestion? So I try something too.
– Marcelo de Andrade
He asked me to return: debug($work->works_image).
– Downbeat
Display the return of the variable
$work
in loopingpr($work)
– Jeferson Assis
Ever tried an echo $work->works_image->work_id ?
– Marcos Xavier