PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Users is not associated with LastBookmarks

 January 23, 2022     cakephp, cakephp-3.0     No comments   

Issue

Im following Lynda tutorial on cakephp 3 basics and i have encountered on problem. The task was that when you view one user, this users entity must also contain information about his last added bookmark.

So what the guy on tutorial did was:

on bookmarks table i have added one relation hasOne

class BookmarksTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('bookmarks');
        $this->displayField('title');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id'
        ]);
        $this->belongsToMany('Tags', [
            'foreignKey' => 'bookmark_id',
            'targetForeignKey' => 'tag_id',
            'joinTable' => 'bookmarks_tags'
        ]);

        // i have added this line
        $this->hasOne('LastBookmarks', [
            'className' => 'Bookmarks',
            'foreignKey' => 'user_id'
        ]);
    }

And my usersController i have added LastBookmarks in contain array

    public function view($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => ['Bookmarks', 'LastBookmarks']
        ]);

        $this->set('user', $user);
        $this->set('_serialize', ['user']);
    }

And the error i'm getting is Users is not associated with LastBookmarks This is a print screen of error which i'm getting when i press view on one user enter image description here

How can i overcome my problem? If you need any additional informations, please let me know and provide. Thank you


Solution

You are trying to do your find on the Users table, so you are missing the association on that table.

class UsersTable extends Table
{

    public function initialize(array $config)
    {
        $this->hasOne('LastBookmarks');
    }
}

This might not be exactly correct for you, but basically the error is telling you that the Users table doesn't know what LastBookmark is, because it's not associated with it.

https://book.cakephp.org/3.0/en/orm/associations.html

Add the correction association that UsersTable needs to relate to LastBookmarksTable.



Answered By - BadHorsie
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing