Cakephp 3 - Inner Join

Asked

Viewed 1,166 times

0

How do I link an article table to a table of favorite articles using cakephp 3.

bookmark table:

CREATE TABLE IF NOT EXISTS `favorites` (
`id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `article_id` int(11) NOT NULL,
  `favorited` datetime NOT NULL
) 

article table:

CREATE TABLE IF NOT EXISTS `articles` (
`id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `content` text NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL
)
  • 1

    cakephp is not a database is a PHP framework, I believe that actually your doubt is about mysql, I’m sure?

  • In vdd is about the Cakephp Associations >> http://book.cakephp.org/3.0/en/orm/associations.html But when I try to make use of it it returns all the data in the article table when in vdd I should return only the articles associated with the bookmark table

  • Are your tables in innoDB (or similar)? Do you use a foreign key? Post the actual structure of both tables (including foreign keys if you have it and if it is n:m the table in the middle of the two... IF IT’S N:M), I believe it is possible to do it. : ) I wait

  • the foreing key would be made by the id of the Articles table and in the favorites table by the column 'article_id', it would be an association n:n. because every favorite article is a row in the favorites table

  • You still don’t have the tables is this?

  • I have, but the problem is in the cake code where I can’t 'cross' these tables

  • Then post what I asked in the comment, all tables that will relate, type of table and which Fields do the foreign key in the question please.

  • I updated the information in the 2 table

Show 3 more comments

1 answer

1


Cakephp 3.x is like this!

Example:

$this->Favorites
    ->find('all')
    ->leftJoin('articles', 'articles.id = Favorites.article_id');

Documentation:

leftJoin(), rightJoin(), innerJoin() and manually Join()

Browser other questions tagged

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