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

  • Home
  • SEARCH
  • 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 7499575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:52:14+00:00 2026-05-29T19:52:14+00:00

I am using CakePHP 2.0 and I have a many-to-many relationship betweens users and

  • 0

I am using CakePHP 2.0 and I have a many-to-many relationship betweens users and courses.
I have a database table users, courses and courses_users.

The problem is deleting courses which are already associated with a user. I won’t delete them if there is an association to a user in courses_users.

So I wrote in my model Course.php

function beforeDelete() {
    if (??? == 0) {
        return true;
    }
    return false;
}

What I need is a database query written in CakePHP, so that I can determine if a course is not associated to any user (x == 0).
How can I do this? With a 1:n relationship I can write this

if($this->User->find("count", array("conditions" => array("course_id" => $this->id))) == 0)

but how can I do this in my n:m relationship?

Best Regards.

  • 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-29T19:52:16+00:00Added an answer on May 29, 2026 at 7:52 pm

    You can check the same as you would for hasMany, because one Course hasMany User and one User hasMany Course.

    So try this (you won’t need to create the UsersCourse or CoursesUser model)

    In the Course model:

    public $hasAndBelongsToMany = array('User');
    

    //in your method

    if($this->CoursesUser->find("count", array("conditions" => array("course_id" => $this->id))) == 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using CakePHP framework with MySQL database and I have problem in saving
Using cakephp, I have a generic address table, which I want to link to
I have trouble saving my date field into database using CakePHP. Table column name
Using CakePHP: I have a many-to-one relationship, let's pretend it's many Leafs to Trees.
I am using postgres database in my cakephp project. I have a table with
I'm using cakephp 2.1.3 and I have a table Ingredients and a table IngredientAliases
I'm using CakePHP 2.1 I have following fields in database. id views max_views I
I'm developing a site using the CakePHP framework. I have a form which shows
I have a PHP application using CakePHP. I need to present a table showing
I have a problem when using CakePHP HABTM. I have the following models. class

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.