Cakephp 3 - Model relationship problem

Asked

Viewed 327 times

-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.

  • Not yet. I will test an option that a stackoverflow user suggested in English.

  • Can you tell me the suggestion? So I try something too.

  • He asked me to return: debug($work->works_image).

  • 1

    Display the return of the variable $work in looping pr($work)

  • Ever tried an echo $work->works_image->work_id ?

Show 1 more comment

1 answer

-1

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

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, because I don’t see the return in the template?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.