Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:55:47+00:00 2026-05-25T18:55:47+00:00

I’m currently using CakePHP 2.0-RC1. Being pretty nifty and all, I’ve only come across

  • 0

I’m currently using CakePHP 2.0-RC1. Being pretty nifty and all, I’ve only come across one challenge that I can’t wrap my mind around. I’ve read the documentation on linking models together (http://www.cakedocs.com/models/associations-linking-models-together.html), but how to I tell CakePHP to search trough a relation table and find the values I’m after? Let me explain.

I have a database structure similar to the following (it has been simplified for the question. PK = Primary Key. FK = Foreign Key )

game

 - INT(11) id (PK)
 - VARCHAR(100) name
 - VARCHAR(40) year
 - VARCHAR(10) age

category

 - INT(11) id (PK)
 - VARCHAR(50) name

game_category

 - INT(11) game_id (FK)
 - INT(11) category_id (FK)

Explanation to the relationship between these:

A game can have one or more category. This relation is defined in “game_category” table. I want CakePHP to not only find what category id a game has, I want the category name as well when I do $this->Game->find(‘first’) for instance. I would guess CakePHP needs to be told to “continue through the game_category table and onto the category table” and find what the name of each category actually is.

I have these Models

Game.php

<?php
class Game extends AppModel {
    var $useTable = 'game';

    var $hasMany = array(
    'GameCategory' => array(
        'fields' => '*',
        'className' => 'GameCategory',
        )
    );

    var $hasOne = 'Review';  
}
?>

GameCategory.php

<?php
class GameCategory extends AppModel {
    var $useTable = 'game_category';

    var $belongsTo = 'category';

    var $hasMany = array(
    'Category' => array(
        'fields' => '*',
        'foreignKey' => 'category_id',
        'className' => 'Category',
        'conditions' => array('GameCategory.game_id' => 'Game.id', 'GameCategory.category_id' => 'Category.id'),
    )
    );
}
?>

Category.php

<?php
class Category extends AppModel {
    var $useTable = 'category';   
    var $belongsTo = 'GameCategory';
}
?>

By the relations defined above, I get results like this:

Array
(
    [Game] => Array
        (
            [id] => 2
            [name] => Colonization
            [year] => 1995
            [age] => 7
        )

    [GameCategory] => Array
        (
            [0] => Array
                (
                    [game_id] => 2
                    [category_id] => 19
                )

            [1] => Array
                (
                    [game_id] => 2
                    [category_id] => 16
                )

        )
)

The only thing remaining is just getting the actual name of each category. I hope some of this made any sense.

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:55:47+00:00Added an answer on May 25, 2026 at 6:55 pm

    You should see hasAndBelongsToMany (HABTM) in book online and should change table name game_category to games_categories for correct conventions.

    Game.php

    <?php
    class Game extends AppModel {
        var $name = 'Game';
        var $hasAndBelongsToMany = array(
        'Category' =>
            array(
                'className'              => 'Category',
                'joinTable'              => 'game_category',
                'foreignKey'             => 'game_id',
                'associationForeignKey'  => 'category_id',
            )
    );
    

    }
    ?>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.