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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:07:36+00:00 2026-06-14T18:07:36+00:00

Sorry if this is obvious – I’m trying to do a find() on a

  • 0

Sorry if this is obvious – I’m trying to do a find() on a join table and failing, whats the correct syntax to do this?

Basically I have expenseCode table with a HABTM releationship with the expense table.

In my expenseCode model I have:

public function beforeDelete($cascade  = false) {

    $count = $this->Expense->find("count", array(
        'conditions' => array('expense_code_id' => $this->id)
    ));

    if ($count == 0) {
        return true;
    } else {
        //$this->Session->setFlash('Record cannot be deleted as it has ' . $count . 'number of expenses attached to it');
        return false;
    }

}

If I uncomment the setFlash() I get the error.

Where am I going wrong? Thanks in advance.

  • 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-14T18:07:37+00:00Added an answer on June 14, 2026 at 6:07 pm

    Unfortunately it’s not possible to perform such a query on the HABTM join table from one of the parent models without some extra work. I’m assuming that Expense is the parent model and ExpensesCode the join model?

    A common way is to modelise the HABTM join table. Say you have an expenses and codes table which are joined by expenses_codes:

    $this->Expense->bindModel(array('hasOne' => array('ExpensesCode')));
    $this->Expense->find('all', array(
      'fields' => array('Expense.*'),
      'conditions' => array('ExpensesCode.expense_code_id' => $this->id)
    ));
    

    However, Cake also auto-iniatlises a model for the join table when a HABTM association is defined (see the manual, and the “with” key in the list of possible keys).

    So this syntax would let you directly query the join table:

    $this->Expense->ExpensesCode->find('all', array(
      'conditions' => array('ExpensesCode.expense_code_id' => $this->id)
    ));
    

    The query above will net you with an array containing only the results from the join table, as it doesn’t perform a join like the first procedure. So you would have to perform a second find() on the Expense model to find the expenses related to the expense_code_id from ExpensesCode.

    EDIT:
    It’s a framework convention that HABTM join tables should be underscored and alphabetically ordered. So if the HABTM join table is called codes_expenses, it’s auto-modelised as CodesExpense.

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

Sidebar

Related Questions

Sorry this may be blatantly obvious but I have spent all morning trying to
Sorry if this is obvious but have looked around and can't get this working:
sorry if the answer to this is obvious but I couldn't find it. How
I'm sorry if this is too obvious but I can't find any proper answer
Sorry if this is extremely obvious, but I have looked and looked for a
Sorry if this sounds obvious but i'm getting confused. I'm trying to build a
Sorry if this has an obvious answer, I can't seem to find anything describing
I'm new to Haskell, so am sorry if this is incredibly obvious... I have
Sorry if this is obvious but i cannot find a straight forward answer on
Im new to perl, so sorry if this is obvious, but i looked up

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.