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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:30:51+00:00 2026-06-10T04:30:51+00:00

I have a nodes table ( Node model). I’d like it to be associated

  • 0

I have a nodes table (Node model). I’d like it to be associated to different data types, but only if one if it’s field is set to 1.

Example:

My nodes table has a data_article field (tinyint 1). I only want the Node to $hasMany Article IF that field is a 1.

I tried this:

public $hasMany = array(
    'Article' => array(
        'conditions' => array('Node.data_articles' => '1')
    ),
);

But I get an error:

Column not found: 1054 Unknown column ‘Node.data_articles’ in ‘where
clause’

Because the association is doing the Article find in it’s own query:

SELECT `Article`.`id`, `Article`.`title`, `Article`.`node_id`, ...more fields...
FROM `mydatabase`.`articles` AS `Article`
WHERE `Node`.`data_artiles` = '1'
AND `Article`.`node_id` = ('501991c2-ae30-404a-ae03-2ca44314735d')

Obviously that doesn’t work, since the Node table isn’t being Joined at all in this query.

TLDR:

Is it possible to have associations or not based on a field in the main model? If not, how else can I keep different data types in multiple tables, and not have to query them all every time?

  • 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-10T04:30:52+00:00Added an answer on June 10, 2026 at 4:30 am

    I don’t think that there’s a standard way to do this with CakePHP (at least I can’t imagine a way). What definitely is possible would be binding associated models dynamically.

    So you might query your model without associations by passing the recursive parameter as -1 to the find() method and based on the result unbind the associated models dynamically. Afterwards you would have to query again, for sure. You might build a behavior out of this to make it reusable.

    A quite elegant solution would be possible, if Cake could make a two-step query with a callback after the main model was queried, but before associated models are queried, but this isn’t possible at the moment.

    Edit: There might be a non-Cake way to archieve this more performantly with a custom Query including IF-statements, but I’m not the SQL expert here.

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

Sidebar

Related Questions

So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
I have a database that has node & nodetype tables. Nodes table NodeID ParentNodeID
I have a table which contains data about which node has been visited. It
I have hierarchical data in a nested set model (table:projects): My table (projects): id,
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
Consider I have a table with notes which could be associated with zero or
I have the following model in django: class Node(models.Model): name = models.CharField(max_length=255) And this
I have a data-structure (in plist) that looks something like this: What i have
I have a table 'node' wich has a column 'nid'. This id corresponds to
I have a very simple set-up. Table Node has a nullable foreign key ObjectId.

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.