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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:44:44+00:00 2026-06-12T15:44:44+00:00

I have a site develop in cakephp 2.0. I have a relation HABTM to

  • 0

I have a site develop in cakephp 2.0.
I have a relation HABTM to the same model like this:

class Product extends AppModel {
    public $name = 'Product';
    public $useTable = 'products';
    public $belongsTo = 'User';
    public $actsAs = array('Containable');
        public $hasAndBelongsToMany = array(
        'Product' => array(
            'className' => 'Product',
            'joinTable' => 'ingredients_products',
            'foreignKey' => 'product_id',
            'associationForeignKey' => 'ingredient_id', 
            'unique' => false
        )
    );

}

I want to save a record into my view with a simple form like this:

echo $this->Form->create('IngredientProduct', array ('class' => 'form', 'type' => 'file'));

        foreach ($product as $prod) {
            echo '<div>'.$prod['ProductAlias']['alias'].'</div>';  
            echo $this->Form->input('IngredientProduct.product_id', array ('type'=>'text', 'value'=>  $prod['ProductAlias']['id'], 'label'=> false, 'id' => 'id'));
        }

        $select = '<select  name="data[IngredientProduct][ingredient_id]" id="[IngredientProductIngredientId">';
        foreach ($other_product as $prod2) {
            $select .= '<option value="'.$prod2['ProductAlias']['id'].'">'.$prod2['ProductAlias']['alias'].'</option>';
        }
        $select .= '</select><br>';
        echo($select);

        echo $this->Form->submit('Collega', array('id'=>'link_product'));
        echo $this->Form->end();

Into my controller I save in this mode:

if ($this->Product->saveAll($this->request->data)){
                $this->Session->write('flash_element','success');
                $this->Session->setFlash ('Prodotto collegato con successo.');
                //$this->redirect(array('action'=>'edit',$alias));
            }
            else{
                $this->Session->write('flash_element','error');
                $this->Session->setFlash('Errore di salvataggio activity');
            }

When I’m going to see into the database I see that ingredient:id is setting well but product_id is 0.
I have debugged my request->data and this is the array:

array(
    'IngredientProduct' => array(
        'product_id' => '1',
        'ingredient_id' => '2'
    )
)

I have print the sql query created by cakephp:

INSERT INTO `db`.`ingredients_products` (`product_id`, `ingredient_id`, `modified`, `created`) VALUES ('', 2, '2012-10-09 23:19:22', '2012-10-09 23:19:22')

Why product_id is null instead of 1?
Can someone help me?
Thanks

  • 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-12T15:44:45+00:00Added an answer on June 12, 2026 at 3:44 pm

    I think this line is wrong:

    $this->Product->saveAll($this->request->data);
    

    Try:

    $this->IngredientProduct->saveAll($this->request->data);
    

    as your form seems to ask data for a relationship, not a new product.

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

Sidebar

Related Questions

I have a site develop in cakephp 2.0. I have a table Product related
I have a site develop in cakephp 2.0 I want to make a HABTM
We have a web site with domain http://209.59.154.150/~phnxaudi/ ,. To develop this website we
I have a simple site to develop and would like to learn PHP as
Hi I have a site develop in cakephp. I want to connect my cakephp
I have to develop a site like paypal which supports online transaction where i
I have to develop a site which has to accomodate around 2000 users a
I need to develop a site on Drupal 7. I have some content types
I'm currently trying to develop a site which queries text information. I already have
I'm trying to develop a site using Zend Framework, but i have a problem:

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.