0
votes

I am trying to save data directly into the HABTM table offerings_students

I am doing this because there is a field in this table that I need to manually set in my controller.

My $this->data array is currently looking like this, after setting the 'owing' field.

Array
(
[OfferingsStudent] => Array
    (
        [OfferingsStudent] => Array
            (
                [0] => Array
                    (
                        [offering_id] => 35
                        [owing] => 209.00
                        [student_id] => 31
                    )

            )

    )

)

The form allows for multiple selection, therefore I must be able to save more than one record into the HABTM table.

My save statement: $this->Student->OfferingsStudent->save($this->data)

The problem is that the records are not being added to the table. I must be able to add multiple records into the table with a single form submission.

I must add that the record is being created, but all the fields end up having zeros.

EDIT:

Changed the array to try submitted solution but still no luck.

$this->data array:

Array
(
[OfferingStudent] => Array
    (
        [0] => Array
            (
                [offering_id] => 35
                [owing] => 209.00
                [student_id] => 31
            )

    )

)
2

2 Answers

1
votes

You haven't asked a question. Is your problem that your attempts to save do not work? Are you trying to insert a new record or update a new one?

If simply inserting, try this instead:

$data = array(
    [offering_id] => 35
    [owing] => 209.00
    [student_id] => 31
);

$this->OfferingsStudent->save($data);

That will save directly to the table. You do not need to create a model for this to work.

EDIT

To add more than one:

$data = array(
    'OfferingStudent' => array(
        array(
            [offering_id] => 35
            [owing] => 209.00
            [student_id] => 31
        ),
        array(
            [offering_id] => 32
            [owing] => 100
            [student_id] => 30
        )
    )
);

$this->OfferingsStudent->saveAll($data);
0
votes

The array that Scoot Harwell generates in the Edit is good to make the saveAll, but the code to do the action must be:

$this->OfferingsStudent->saveAll($data['OfferingStudent']);

I have the same problem and finally find that after many time.