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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:30:16+00:00 2026-05-29T19:30:16+00:00

I have the database something like this == Invoices == id customer_id == Customers

  • 0

I have the database something like this

  == Invoices ==
  id
  customer_id

  == Customers ==
  id
  firstname
  lastname
  membersince

So I made relations in between both models like below.

Relation definition in the Invoices Model:

'customers'=>array(self::HAS_MANY,'Invoices','customer_id'),

Relation definition in the Customers Model

'invoices' => array(self::BELONGS_TO, 'Invoices', 'invoice_id'),

Now I want to know the relation in between both models are correct? Any suggestions are welcome.

  • 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-29T19:30:17+00:00Added an answer on May 29, 2026 at 7:30 pm

    No, the relation between the models is not correct.

    I assume what you want here is that customers can have many invoices, but an invoice can only be owned by a single customer, as this is what your database schema implies.

    Please note that in the code below I distinguish between singular and plural forms of customer and invoice to make the code logical and easy to understand.

    In that case the relation definition in the Invoice model would be like this:

    class Invoice extends CActiveRecord {
        public function relations() {
            return array(
                'customer' => array(self::BELONGS_TO, 'Customer', 'customer_id')
            );
        }
    }
    

    The invoice ‘belongs to’ a customer. 'Customer' indicates that the type of model we refer to is a Customer, and 'customer_id' is the name of the column in the invoices table that refers to the primary key of the customer.

    The Customer model looks a bit trickier:

    class Customer extends CActiveRecord {
        public function relations() {
            return array(
                'invoices' => array(self::HAS_MANY, 'Invoice', 
                                   'customer_id', 'index' => 'id')
            );
        }
    }
    

    Here the relation definition indicates that a Customer can refer to many Invoice models. Notice how the rest of the relation is defined. customer_id is again the column that refers to a Customer, and 'index' => 'id' tells Yii to use the column id in the customers table as key for the relationship.

    You can find more information about the relations method in the Yii documentation.

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

Sidebar

Related Questions

If I have a database-like class, and I want to do something like this:
OK I have a mySQL Database that looks something like this ID - an
I have a database that looks something like this: user_id photo_id 1 1 1
So I have a database that has a structure something like this: [user_table] user_id,
i have database table like this +-------+--------------+----------+ | id | ip | date |
My database is something like this === Group === id(Pk) member_id(Fk) description === Member
I have a database that looks like this: Product: PK(ProductId), ProductName....... Store: PK(StoreId), StoreName......
I have a database with two tables: Users , Items Users looks like this:
In Yii I am doing multimodel.My database is something like this +++++ Group ++++++
I have a relatively simple subset of tables in my database for tracking something

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.