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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:34:12+00:00 2026-06-11T02:34:12+00:00

I have got two Models in CakePHP 2.2.1: class Episode extends AppModel { public

  • 0

I have got two Models in CakePHP 2.2.1:

class Episode extends AppModel {
    public $hasAndBelongsToMany = array(
    'Tags' => array('className' => 'Tag')
    );
}

and

class Tag extends AppModel {
    public $hasAndBelongsToMany = array(
        'Episode' => array('className' => 'Episode')
    );
}

And I want to insert an Episode and a Tag at the same time and link them to each other. From the documentation I got that this would be the right code which I execute inside a controller method:

$this->Tag->create();
$this->Tag->save(array(
    'name' => 'Test Tag'
));
$tagid = $this->Tag->getInsertID();

var_dump($tagid); // works

$this->Episode->create();
$this->Episode->save(array(
    'Episode' => array(
        'title' => 'Test with Tag'
    ),
    'Tags' => array(
        'Tag' => array($tagid)
    )
));

I get no error message and both records are created, but there is no row inserted into the episodes_tags table. CakePHP does these queries:

Nr  Query                                                                                                              Err Aff Rws Took (ms)
1   INSERT INTO `pottcast`.`tags` (`name`) VALUES ('Test Tag')                                                              1   1   0
2   INSERT INTO `pottcast`.`episodes` (`title`) VALUES ('Test with Tag')                                                    1   1   0
3   SELECT `EpisodesTag`.`tag_id` FROM `pottcast`.`episodes_tags` AS `EpisodesTag` WHERE `EpisodesTag`.`episode_id` = 9     0   0   0

What did I do wrong?

  • 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-11T02:34:13+00:00Added an answer on June 11, 2026 at 2:34 am

    Try This :

    In Episode Model:

    public $hasAndBelongsToMany = array(
        'Tag' => array(
            'className' => 'Tag',
            'joinTable' => 'episodes_tags',
            'foreignKey' => 'episode_id',
            'associationForeignKey' => 'tag_id',
            'unique' => 'keepExisting',
    
        ));
    

    In your controller:

    $this->Episode->save(array(
        'Episode' => array(
            'title' => 'Test with Tag'
        ),
        'Tag' => array(
            'Tag' => $tagid
        )
    ));
    

    Ask if not work for you.

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

Sidebar

Related Questions

I've got two models: Item and Tag . Both have a name attribute. I
I have got two models, User and Event. I need two kind of relationship
I've got two models Category and Item. Category can have many items and item
I've got two models that look like this class Stage include DataMapper::Resource property :id,
I have got two models, seen below, and am trying to insert one of
I've got two models like these: class Schedule(models.Model): name = models.CharField(_('name'), blank=True, max_length=15) class
I have two models, Client and PaymentOptions . class Client(models.Model): name = models.CharField(max_length=50, null=True,
Got a question. Say I have two models in a many-to-many relationship (Article, Publication).
I have two models class Subscription < ActiveRecord::Base belongs_to :client end class Client <
I got a problem, i've got two models class Department < ActiveRecord::Base belongs_to :school

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.