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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:28:50+00:00 2026-05-25T19:28:50+00:00

I have two tables: internet_access_codes and radacct . The internet_access_codes hasMany radacct records. The

  • 0

I have two tables: internet_access_codes and radacct.
The internet_access_codes hasMany radacct records.
The join is internet_access_codes.code = radacct.username AND internet_access_codes.fk_ship_id = radacct.fk_ship_id

I created 2 models and wanted to use $hasMany and $belongsTo respectively so that the related radacct records would be pulled when getting and internet_access_codes record.

Here’s the code:

class InternetAccessCode extends AppModel{
var $name = 'InternetAccessCode';
var $hasMany = array(
    'Radacct' => array(
        'className' => 'Radacct',
        'foreignKey'=> false,
        'conditions'=> array(
            'InternetAccessCode.code = Radacct.username',
            'InternetAccessCode.fk_ship_id = Radacct.fk_ship_id'
        ),
    )
);

}

class Radacct extends AppModel{
var $name = 'Radacct';
var $useTable = 'radacct';

var $belongsTo = array(
    'InternetAccessCode' => array(
        'className' => 'InternetAccessCode',
        'foreignKey' => false,
        'conditions'=> array(
            'InternetAccessCode.code = Radacct.username',
            'InternetAccessCode.fk_ship_id = Radacct.fk_ship_id'
        )
    ),
);

}

When I find() a record from internet_access_codes I expect it to give me all the relevant radacct records as well. However I got an error because it didnt do the join.

Here’s the outcome and error:

Array
(
    [InternetAccessCode] => Array
        (
            [id] => 1
            [code] => 1344444440
            [bandwidth_allowed] => 20000
            [time_allowed] => 30000
            [expires_at] => 31536000
            [cost_price] => 0.00
            [sell_price] => 0.00
            [enabled] => 1
            [deleted] => 0
            [deleted_date] => 
            [fk_ship_id] => 1
            [downloaded_at] => 2011-09-10 22:18:14
        )

    [Radacct] => Array
        (
        )

)

Error: Warning (512): SQL Error: 1054: Unknown column
‘InternetAccessCode.code’ in ‘where clause’
[CORE/cake/libs/model/datasources/dbo_source.php, line 684]

Query: SELECT Radacct.id, Radacct.fk_ship_id,
Radacct.radacctid, Radacct.acctsessionid,
Radacct.acctuniqueid, Radacct.username, Radacct.groupname,
Radacct.realm, Radacct.nasipaddress, Radacct.nasportid,
Radacct.nasporttype, Radacct.acctstarttime,
Radacct.acctstoptime, Radacct.acctsessiontime,
Radacct.acctauthentic, Radacct.connectinfo_start,
Radacct.connectinfo_stop, Radacct.acctinputoctets,
Radacct.acctoutputoctets, Radacct.calledstationid,
Radacct.callingstationid, Radacct.acctterminatecause,
Radacct.servicetype, Radacct.framedprotocol,
Radacct.framedipaddress, Radacct.acctstartdelay,
Radacct.acctstopdelay, Radacct.xascendsessionsvrkey FROM
radacct AS Radacct WHERE InternetAccessCode.code =
Radacct.username AND InternetAccessCode.fk_ship_id =
Radacct.fk_ship_id AND Radacct.deleted <> 1

In the app_model I also added the containable behaviour just in case but it made no difference.

  • 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-25T19:28:51+00:00Added an answer on May 25, 2026 at 7:28 pm

    Sadly cakephp doesn’t work too well with the associations with foreign key =false and conditions. Conditions in associations are expected to be things like Model.field = 1 or any other constant.

    The has many association first find all the current model results, then it finds all the other model results that have the current model results foreignKey… meaning it does 2 queries. If you put the conditions it will try to do it anyway but since it didn’t do a join your query will not find a column of another table.

    Solution

    use joins instead of contain or association to force the join you can find more here

    an example of how to use join

    $options['joins'] = array(
        array(
            'table' => 'channels',
            'alias' => 'Channel',
            'type' => 'LEFT',
            'conditions' => array(
                'Channel.id = Item.channel_id',
            )
        ));
    $this->Model->find('all', $options);
    

    Possible solution #2

    BelongsTo perform automatic joins (not always) and you could do a find from radaact, the bad thing of this solution, is that it will list all radacct and put its internetAccesCode asociated instead of the internetAccesCode and all the radaact associated…. The join solution will give you something similar though…

    You will need to do a nice foreach that organizes your results :S it won’t be to hard though….

    Hope this solves your problem.

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

Sidebar

Related Questions

I have two tables users(id, username, password) and user_badges(user_id, badge_id, badge_slot) I would like
For example, I have created two pages and two MySQL tables. Index.php & citys.php
Have two tables with a linking table between them. USERS +-------+---------+ | userID| Username|
I have two tables that are joined together. A has many B Normally you
I have two tables, Book and Tag , and books are tagged using the
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables, both with start time and end time fields. I need
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two tables in my database, called ratings and movies . Ratings: |
I have two tables, one that contains volunteers, and one that contains venues. Volunteers

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.