2

I have one form that saves a person with many parts and many colors but it's not saving the parts and the colors; only saving the person(main model).

On my list view, part of script:

echo $this->Form->input('Person.person_name', array('required' => true, 'placeholder' => 'Name Your Person', 'label' => false) );
echo $this->Form->input('Part.0.part_name', array('required' => true, 'placeholder' => 'Add Part', 'label' => false) ); 
echo $this->Form->input('Color.0.color_name', array('required' => true, 'placeholder' => 'Enter a Color', 'label' => false) ); 

On my list controller:

if ($this->request->is('post')) {
            $created = $this->Person->saveAll($this->request->data, array('validate'=>'first'));
            if (!empty($created)) {
                $this->Session->setFlash(__('The person and his/her parts and colors have been saved.'));
                return $this->redirect(array('action' => 'index'));                 
            } else {
                $this->Session->setFlash(__('The person could not be saved. Please, try again.'));
            }
        }

On my Person Model:

public $hasMany = array(
    'PersonParts' => array(
        'className' => 'Part',
        'foreignKey' => 'part_person_id'
    ),
    'PersonColors' => array(
        'className' => 'Color',
        'foreignKey' => 'color_person_id'
    )
);

Tried replacing saveAll with saveAssociated but it's the same. What am I doing wrong here? Any help is greatly appreciated.

tereško
  • 58,060
  • 25
  • 98
  • 150
Leah
  • 225
  • 2
  • 10
  • 24
  • do you have three models right ? and values should store in different tables – Manish Patel Dec 23 '13 at 10:27
  • 1
    It would be a good idea to maintain your model aliases as singular `PersonParts -> PersonPart` - otherwise certain aspects of your app will be confusing (such as needing form fields containing the name `PersonParts`) – AD7six Dec 23 '13 at 10:42

5 Answers5

3

You must add original name of alias in form (alias like in hasMany variable):

echo $this->Form->input('Person.person_name');
echo $this->Form->input('PersonParts.0.part_name'); 
echo $this->Form->input('PersonColors.0.color_name');
Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
kicaj
  • 2,881
  • 5
  • 42
  • 68
1

Update your form to

echo $this->Form->input('Part.part_name',...)
echo $this->Form->input('Color.color_name',...)

And in your controller, use

**saveAssociated**
Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
XuDing
  • 1,982
  • 18
  • 27
  • `SaveAll` is in context an alias for `SaveAssociated`. The form structure you propose isn't appropriate for a has-many relationship. – AD7six Dec 23 '13 at 10:41
1

Try this

echo $this->Form->input('Person.person_name', array('required' => true, 'placeholder' => 'Name Your Person', 'label' => false) );
echo $this->Form->input('PersonParts.0.part_name', array('required' => true, 'placeholder' => 'Add Part', 'label' => false) ); 
echo $this->Form->input('PersonColors.0.color_name', array('required' => true, 'placeholder' => 'Enter a Color', 'label' => false) );
Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
0

I think you need to save everything independently.

What do you think of this possibility?

if yes then go through.

$this->Person->Part->save();
$this->Person->Color->save();
Carl0s1z
  • 4,683
  • 7
  • 32
  • 47
Badru
  • 41
  • 2
0

To save you could use something like:

$this->Model->saveMany($data, array('deep'=>TRUE));

Note that the "deep" option requires CakePHP 2.1. Without it the associated Comment records would not be saved.

All this is documented in http://book.cakephp.org/2.0/en/models/saving-your-data.html

Also correct it

echo $this->Form->input('Person.person_name', array('required' => true, 'placeholder' => 'Name Your Person', 'label' => false) );
echo $this->Form->input('Part.part_name', array('required' => true, 'placeholder' => 'Add Part', 'label' => false) ); 
echo $this->Form->input('Color.color_name', array('required' => true, 'placeholder' => 'Enter a Color', 'label' => false) ); 
Manish Patel
  • 1,877
  • 1
  • 14
  • 15
  • SaveMany is for saving many rows _of the same type_. That's the wrong method for the question. – AD7six Dec 23 '13 at 10:40