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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:07:21+00:00 2026-06-02T06:07:21+00:00

I am looking at YII for the first day, and i’m having some problems

  • 0

I am looking at YII for the first day, and i’m having some problems trying to work out the relations between some tables.

my table structure is as follows:

Pets:
pet_id
pet_name
….

Pet_Owner_Link
pet_id
owner_id

Owner:
owner_id
owner_name

How would I go about getting all of the pets that belong to an owner? Really struggling to get my head around the AR relations at the moment.

  • 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-02T06:07:23+00:00Added an answer on June 2, 2026 at 6:07 am

    Per this comment by DD.Jarod on the Yii AR documentation page: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#c970

    “If you declare a many to many relationship, the order of keys inside the jointable declaration must be ‘my_id, other_id’:

    class Post extends CActiveRecord
    {
      public function relations()
      {
        return array(
            'categories'=>array(self::MANY_MANY, 'Category',
                'tbl_post_category(post_id, category_id)'),
        );
      }
    }
    class Category extends CActiveRecord
    {
      public function relations()
      {
        return array(
            'Posts'=>array(self::MANY_MANY, 'Post',
                'tbl_post_category(category_id, post_id)'),
        );
      }
    }
    

    So your code would look like:

    class Owner extends CActiveRecord
    {
      public function relations()
      {
        return array(
            'pets'=>array(self::MANY_MANY, 'Pet',
                'tbl_post_category(pet_id, owner_id)'),
        );
      }
    }
    class Pet extends CActiveRecord
    {
      public function relations()
      {
        return array(
            'owners'=>array(self::MANY_MANY, 'Post',
                'tbl_post_category(owner_id, pet_id)'),
        );
      }
    }
    

    Your problem may be that your primary keys for Pet and Owner by default should be id (not pet_id / owner_id). Yii may be getting confused if you don’t clarify that your primary keys don’t match the default naming convention / aren’t setup as primary keys in the database. You can specify what your primary key is in a model like this:

    public function primaryKey()
    {
        return 'owner_id';
    }
    

    Finally, you would retrive the information like this:

    $owner = Owner::model()->findByPk((int)$id);
    foreach($owner->pets as $pet)
    {
        print $pet->name;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking through the Django docs and trying to figure out the use of the
Looking to move data from a table A to history table B every X
Looking at adding some data graphing to a new iPhone app in development (ala
I am trying to write a validation rule in Yii but can't get the
Looking for a C++ async HTTP library so I can download some zip files
Looking at all the examples of Operational Transformation Frameworks out there, they all seem
As I am studying Yii I keep for looking this class of Yii:app()-> user
Looking to create my first MVC application. Coming from WebForms I'm pretty excited to
Looking through some java code and this just does not seem right. To me,
I'm looking at the source files for Yii 1.1.5, and I don't see a

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.