CakePHP 3 - Problem of relationship between models

0

My problem is basically the following:

I have related 2 models following the official documentation of CakePHP 3 and I can not return the values of 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 can not return any value from the WorksImagesTable model. Debugging I realize that the tables are related, in addition, the cake does not return any error in the view.

I can not understand what's wrong.

Thanks in advance for any help.

Thank you.

    
asked by anonymous 15.05.2015 / 01:15

1 answer

0

And I have sent the following code from one of the foreach loop results:

object(App\Model\Entity\WorksImage) {

    'new' => false,
    'accessible' => [
        'photo' => true,
        'photo_dir' => true,
        'work_id' => true,
        'work' => true
    ],
    'properties' => [
        'id' => (int) 1,
        'photo' => 'arteviva.jpg',
        'photo_dir' => 'a4cd522c-b7b9-437a-99fc-0eb15827944f',
        'work_id' => (int) 1,
        'created' => object(Cake\I18n\Time) {

            'time' => '2015-05-08T20:25:07+0000',
            'timezone' => 'UTC',
            'fixedNowTime' => false

        },
        'modified' => null
    ],
    'dirty' => [],
    'original' => [],
    'virtual' => [],
    'errors' => [],
    'repository' => 'WorksImages'

}

So the relationship exists, why do not I see the return in the template?

    
19.05.2015 / 02:06