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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:59:29+00:00 2026-05-29T22:59:29+00:00

I have the database just like this ==== Invoices ==== id customer_id description ====

  • 0

I have the database just like this

==== Invoices ====
id
customer_id
description

==== Customers ===
id
firstname
lastname

Now I have made the relation in between models just like this. In Invoices models the relation is as like this

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

In customer model the relation is just like this

public function relations()
{
    return array(
      'invoice' => array(self::HAS_MANY, 'Invoices','customer_id')
    );
}

Now as my relation is defined one customer has many invoices and the invoice is belongs to the customer.

Now I made multimodel and loaded the Customer model into Invoice model just like this.

public function actionCreate()
{
    $model = new Invoices;
    $customers = new Customers;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if (isset($_POST['Invoices'],$_POST['Customers']))
    {
      $model->attributes = $_POST['Invoices'];
      $customers->attributes = $_POST['Customers'];
      $valid = $model->validate();
      $valid = $customers->validate();
      if($valid)
      {
        $model->save(false);
        $customers->id = $model->customer_id;
        $customers->save(false);
        $this->redirect(array('view','id'=>$model->id));
      }
    }

    $this->render('create',array(
      'model'=>$model,
      'customers'=>$customers,
    ));
}

Here everything is okay. I can insert the data for both models easily. But my problem comes here in the way that when I am inserting data from Invoice multimodel the foreign key id is not changing. It is showing zero every time. Can some one tell me where I am wrong.

Any help and suggestions will be highly appreciated.

  • 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-29T22:59:31+00:00Added an answer on May 29, 2026 at 10:59 pm

    My guess is that you are overriding the customer’s primary key with the invoice’s foreign key. I do not say that’s not correct that way (maybe in your scenario it makes sense).

    Let me explain what you are doing in that code:

    • First, you create new instances of two models, Invoices and Customers. Yii understands that as “they wish to insert new items in the database”.

    • Then, you check if there are the items coming from an ajax form. If true, then,

    • You populate Invoices (defined as $model. I’d change it to $invoice, in case you need to edit and understand it further).
    • You also popupulate the customer’s information, overriding the $valid value (so, you don’t know if invoice is actually valid).
    • If valid (remember you’re only validating customer’s information), do,
    • Save the invoice
    • Override customer’s id with invoice’s foreing key to customer.
    • Save the customer, and redirect.

    Now, what I got from that:

    • $valid doesn’t work as expected: I’d change that to an incremental assignment.
    • You may not be passing a customer_id coming from the ajax form. Foreing keys are integers, and so if not defined within a model, it becomes 0 or NULL.
    • You are always passing id = 0 / NULL to Customer’s model, so it would probably warn you when validating. However, you are using save(false), which means it doesn’t pre-validate on save, so you never know it doesn’t work.

    So, according to this:

      public function actionCreate()
      {
        $invoice = new Invoices;
        $customers = new Customers;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($invoice);
    
        if (isset($_POST['Invoices'],$_POST['Customers']))
        {
          $invoice->attributes = $_POST['Invoices'];
          $customers->attributes = $_POST['Customers'];
          $valid = true; /* expect it is always valid */
          $valid &= $invoice->validate(); /* if $invoice is not valid, $valid will be false (true&false = false) */
          $valid &= $customers->validate(); /* same as the above line */
          if($valid)
          {
            $customers->save(); /* First save customers. It's the Foreign item */
            $invoice->customer_id = $customers->getPrimaryKey(); /* new instances use getPrimaryKey() to get its id */
            $invoice->save(); /* Save invoice AFTER getting customer's primary key */
            $this->redirect(array('view','id'=>$invoice->id));
          }
        }
    
        $this->render('create',array(
          'invoice'=>$invoice,
          'customers'=>$customers,
        ));
      }
    

    I 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 a database like this: table pepak, table category, table subcategory I just
i have rows of text like this in database: row1 -> text text!? sometext..!
I have a little bit of code that looks just like this: function StrippedExample(i1,
I have a database that looks like this: ID parent ticket category _record_status _log_user
This is just an out of curiosity question. Let's say you have a database
I have a database that has four columns like this level_1, level_2, level_3, level_4
I have a database that contains a table that looks a bit like this:
I have a table (in an Oracle database) just like below: department ---------- Finance
So I have a database that has a structure something like this: [user_table] user_id,
I have several database tables that just contain a single column and very few

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.