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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:27:44+00:00 2026-06-16T18:27:44+00:00

I’m using php.activerecord , and I am trying to link tables together. I’m not

  • 0

I’m using php.activerecord, and I am trying to link tables together. I’m not using their structure, but php.activerecord assumes I am, so it doesn’t always work. I’m trying to use it on an already made app, so I can’t change the database.

I learned from my previous question – Model association with custom table and key names – that I need to be as explicit as possible with the primary_key and foreign_key fields.

I’m having issues now using has_many through. I keep getting NULL, and I have no idea why.

So, here’s a scenario: I have 3 tables, contacts, contactPrefs, and preferences. Those tables are as follows

contacts
--------
contactID
name
status

contactPrefs
------------
contactID
prefID
prefValue

preferences
-----------
prefID
name
description

Each contact has multiple contactPrefs. Each contactPrefs has one preferences. I tried to use has_many to get this working, but it’s not. Here are my models:

Contacts.php:

<?php
class Contact extends ActiveRecord\Model {
    static $primary_key = 'contactID';

    static $has_many = array(
        array(
            'prefs',
            'foreign_key' => 'contactid',
            'primary_key' => 'contactid',
            'class_name' => 'ContactPref'
        ),
        array(
            'preferences',
            'foreign_key' => 'prefid',
            'primary_key' => 'prefid',
            'through' => 'prefs',
            'class_name' => 'Preference'
        )
    );
}

ContactPref.php:

<?php
class ContactPref extends ActiveRecord\Model {
    static $table_name = 'contactPrefs';

    static $belongs_to = array(
        array(
            'contact',
            'foreign_key' => 'contactid',
            'primary_key' => 'contactid'
        ),
        array(
            'preference',
            'foreign_key' => 'prefid',
            'primary_key' => 'prefid'
        )
    );
}

Preference.php:

<?php
class Preference extends ActiveRecord\Model {
    static $primary_key = 'prefID';

    static $has_many = array(
        array(
            'prefs',
            'foreign_key' => 'prefid',
            'primary_key' => 'prefid',
            'class_name' => 'ContactPref'
        )
    );
}

According to the docs, I now should be able to the following:

<?php
var_dump(Contact::find(1234)->preference);

I cannot. I get NULL. Oddly, I can do this:

<?php
var_dump(Contact::find(1234)->prefs[0]->preference);

That works correctly. But, shouldn’t I be able to access the preference object directly through the contact object? Am I misunderstanding the docs (they aren’t the greatest, in my opinion)? Am I doing something 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-16T18:27:45+00:00Added an answer on June 16, 2026 at 6:27 pm

    First you are reading the docs with a small flaw. In the docs you are shown:

    $order = Order::first();
    # direct access to users
    print_r($order->users); # will print an array of User object
    

    Which you are already doing via Contact::find(1234)->prefs. Let me boil it down a bit

    $contact = Contact::find(1234);
    # direct access to prefs
    print_r($contact->prefs); # will print an array of ContactPref object
    

    Second, what you actually want is undefined. What should Contact::find(1234)->preference actually do? Return the preference of the first ContactPref? Return an array of Preference objects?

    I feel like offering both:

    <?php
    class Contact extends ActiveRecord\Model {
        static $primary_key = 'contactID';
    
        static $has_many = array(
            array(
                'prefs',
                'foreign_key' => 'contactid',
                'primary_key' => 'contactid',
                'class_name' => 'ContactPref'
            ),
            array(
                'preferences',
                'foreign_key' => 'prefid',
                'primary_key' => 'prefid',
                'through' => 'prefs',
                'class_name' => 'Preference'
            )
        );
    
        public function get_preference() {
            return isset($this->prefs[0])
                ? $this->prefs[0]->preference
                : null
            ;
        }
        public function get_preferences() {
            $preference=array();
            foreach($this->prefs as $pref) {
                $preference[]=$pref;
            }
            return $preference;
        }
    }
    

    Let me explain a little bit what I have done. The ActiveRecord\Model class has a __get($name) function that looks for another function called get_$name, where $name in your case is preference (for the first result) and preference (for the entire collection). This means you can do Contact::find(1234)->preference which would be the same as doing Contact::find(1234)->prefs[0]->preference (but safer, due to the check) and Contact::find(1234)->preferences to get the entire collection of preferences.

    This can be made better or optimized in numerous ways, so please don’t take it as it is, but do try and adapt it to your specific situation.
    For example you can either use the id of the preference as an index in the array or either not force a load of more data from ContactPrefs than the ones you are going to use and try a more intricate query to get the preference objects that you specifically need.

    If I find a better implementation by getting through to work in the relationship definition, I’ll return. But seeing the Unit Tests for active record, I’m skeptical.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is

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.