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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:24:23+00:00 2026-06-17T14:24:23+00:00

I have an add action form ProductsController which has many prices depending on the

  • 0

I have an add action form ProductsController which has many prices depending on the size. I am trying to save the product first and then within foreach loop the prices.

Some how saveAll or saveAssociated did not work.

public function add() {
        if ($this->request->is('post')) {
            $this->Product->create();
            $product = $this->Product->save($this->request->data);

            if (!empty($product)) {
                $product_id = $this->Product->getInsertID();
                $prices = $this->request->data['Product']['price'];

                foreach ($prices as $price) {
                    $price['product_id'] = $product_id;
                    $this->Product->Price->save($price);
                     $this->Product->Price->id = false;
                }

                $this->Session->setFlash('The product has been saved.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add the product.');
            }
        }
    }

The models are look like

class Product extends AppModel {        
   ...
   public $hasMany = array(
      'Price' => array(
              'className' => 'Price',           
                 )
   );


class Price extends AppModel {

    public $belongsTo = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'product_id', 
        )
    );
}
  • 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-17T14:24:24+00:00Added an answer on June 17, 2026 at 2:24 pm

    You can use saveAssociated() as per documentation

    But your view should look something like this:

    echo $this->Form->create('Product', array('action' => 'add'));
        echo $this->Form->input('Product.name', array('label' => 'Name'));
        echo $this->Form->input('Product.description', array('label' => 'Description'));
    
        echo $this->Form->input('Price.0.amount', array('label' => 'Amount'));
        echo $this->Form->input('Price.0.price', array('label' => 'Price'));
    echo $this->Form->end('Add');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So in struts I have an action form which has 5 properties. Each property
I have a form which has a input textbox and submit button. On submission
I have the following form in cakephp: <div class=quickcontactDisplay> <?php echo $form->create('Enquiry',array('action'=>'add','class'=>'quickcontact')); echo $form->hidden('visitor_id',
I have and add/edit form that adds/edits depending on whether there is an id
Im using cakePHP 2.2.1 I have a GoalsController that has an add action in
I have one custom action (add) and two custom objects (favorites list) and (tv
I have to add a textbox inside a form that is composed of a
I have to add certificate to IIS 6.0. But there are many other websites
I have 2 actions. First to display form and another to process form. If
im trying to achieve the following, in php i have a form like this:

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.