Question

I'm trying to save data to two models (a model and related hasMany) simultaneously, and have had some success in a number of places in my app, however this approach doesn't seem to be working with tables/models which have underscored names, i.e.

//View Code
echo $this->Form->create('Product');
  echo $this->Form->input('name',array('between' => '<br />'));
  echo $this->Form->input('Component.0.text',array('between' => '<br />'));
echo $this->Form->end(__('Submit'));

//Controller Code
if ($this->Product->saveAssociated($this->request->data)) {
    $this->Session->setFlash(__('The product has been saved'));
    $this->redirect(array('action' => 'index'));
} else {
    $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}

The above works fine, however I have a model ProductComponent (db table product_components)

//View Code
echo $this->Form->create('Product');
  echo $this->Form->input('name',array('between' => '<br />'));
  echo $this->Form->input('ProductComponent.0.text',array('between' => '<br />'));
echo $this->Form->end(__('Submit'));

//Controller Code
if ($this->Product->saveAssociated($this->request->data)) {
    $this->Session->setFlash(__('The product has been saved'));
    $this->redirect(array('action' => 'index'));
} else {
    $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}

The form is picking this up correctly as it's showing a Textarea rather than a standard input, however when saveAssociated is run, I get the response:

Database table product__components for model ProductComponent was not found.

Why is cake looking for a table with a double underscored name?

Any ideas?

Update for Issem Danny:

public $hasMany = array(
    'ProductComponent' => array(
        'className' => 'Product_Component',
        'foreignKey' => 'product_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
          );
Was it helpful?

Solution

A few things:

  • If you have these three models: Product, Component, ProductComponent, it seems to me you're trying to setup hasAndBelongsToMany relations. Check out the cakephp docs.

  • Using Component as or in a model name seems confusing to me..

  • If you have a model for a table product_components: your model should be named "ProductComponent" without an underscore (cake will add that automatically). If you dont follow cake's conventions you can declare the "useTable" property. Your model should look like this:

Code:

// model

class ProductComponent extends AppModel {
    public $name = "ProductComponent";

    // Only needed when not following cake's conventions
    public $useTable = "product_components"; 

    // rest of model code  ...
}

// And the hasmany relation

public $hasMany = array(
    'ProductComponent' => array(
        'className' => 'ProductComponent',
        'foreignKey' => 'product_id'
     )
);

Hope that helps, good luck.

OTHER TIPS

Have you tried to use

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

Try this link SaveAll

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top