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 8705957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:29:38+00:00 2026-06-13T03:29:38+00:00

I have a model called Game, which has a hasMany relationship with a Planet

  • 0

I have a model called Game, which has a hasMany relationship with a Planet model (which in turn has a belongsTo defined back to the Game model).

Upon creating my first Game record, I use the value of its starting_num_planets field to try to create that many associated Planet records. The GamesController does this by calling a function in the PlanetsController which, each time called, returns an array that matches the Planet model, and should be saveable, which is the next thing the GamesController tries to do.

The Game record is created just fine, but I am somehow getting an invalid SQL syntax error apparently at the point at which I’m trying to access the second controller’s function. Which is weird, because I’m not trying to save until a couple of lines later.

Here’s the code from the GamesController:

public function newgame() {
        if ($this->request->is('post')) {
            $this->Game->create();
            if ($this->Game->save($this->request->data)) {
                // Add the 'id' element to the Game array and put the whole game record on the session
                $this->request->data['Game']['id'] = $this->Game->getLastInsertID();
                $this->Session->write('Game', $this->request->data);

                // Create the number of planet records indicated by start_num_planets
                $count = 0;
                $planets = array();
                while ($count < $this->request->data['Game']['starting_num_planets']) {
                    $planetData = $this->Game->Planet->generate_planet_data($this->request->data['Game']['id']);
                    $planets[$count] = $planetData;
                    $this->Game->Planet->create();
                    $this->Game->Planet->save($planetData);
                    $count++;
                }

                $this->redirect(array('action' => 'main'));
            } else {
              $this->Session->setFlash('There was a problem creating the game.');
            }
        }
    }

At the line number for this line:

$planetData = $this->Game->Planet->generate_planet_data($this->request->data[‘Game’][‘id’]);

I am getting:

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'generate_planet_data' at line 1

SQL Query: generate_planet_data

And I have no idea why, I’m afraid.

Here’s the function being called in the PlanetsController:

public function generate_planet_data($gameId) {
    $planetNames = array(1 => 'name1', 2 => 'name2', 3 => 'name3', 4 => 'name4');
    $planetImages = array(1 => '01', 2 => '02', 3 => '03', 4 => '04');
    $planetData = array();
    // game_id is the foreign key, do I have to explicitly state that in the model?
    $planetData['Planet']['game_id'] = $gameId;
    $planetData['Planet']['planet_name'] = $planetNames[rand(1,10)];
    $planetData['Planet']['planet_image_file'] = $planetImages[rand(1,4)];
    return $planetData;
}

The model association in Game.php is simply:

var $hasMany = array(
    'Planet' => array(
        'className' => 'Planet'),
    'Player' => array(
        'className' => 'Player'
    )
);

Can anyone tell what’s going wrong? This is my first attempt at using associated models, and it’s gone a bit awry! 🙂

  • 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-06-13T03:29:39+00:00Added an answer on June 13, 2026 at 3:29 am

    The problem is that you’re calling a missing function here:

    $planetData = $this->Game->Planet->generate_planet_data($this->request->data['Game']['id']);
    

    As you said generate_planet_data is on the controller. So you should call it thusly:

    $planetData = $this->generate_planet_data($this->request->data['Game']['id']);
    

    As stated in the comments, this logic probably belongs in the Planet model. If you moved it there you would then be able to access it via your original way.

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

Sidebar

Related Questions

I have a model called Answer which has a ForeignKey relationship to another model
I have model called User with a has_many relationship to UserFilter . UserFilter has
I have a model called Event and another called Product. An event has many
I have a model called Contact which has_one guest like so. class Contact <
I have a game app that has the ability to go fullscreen and back
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have a activerecord model called game with an paperclip attachment called image. I
I have a data model called Game . In the Game model, I have
I have a model called Post, with a column called vote, and it has
I have a model called SimplePage in which I have this line: category =

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.