0

I have model for

  • user(id,name)
  • section(id,name)
  • section_users(id,user_id,section_id)

The admin adds all the users and sections separately. Once theses are added I want the admin to selects the section and add all the users in it in section_users


I have a select input with multiple set to true. How do i save this data the cakephp way along with validation.

<?php echo $this->Form->input("section_id"); ?>
<?php echo $this->Form->input("user_id", array('multiple'=>'checkbox')); ?>

This generates

Array
(
    [section_id] => 1
    [user_id] => Array
        (
            [0] => 3
            [1] => 4
        )

)

I know i can loop and convert to this and use saveAll or saveMany but what is the cakephp way/right way to do it.

Array
(
    [0] => Array
        (
            [section_id] => 1
            [user_id] => 3
        )
    [1] => Array
        (
            [section_id] => 1
            [user_id] => 4
        )

)
2
  • 1
    Umm, how about starting with reading the manual and the existing questions about saving data with CakePHP, and then check back when you have a specific problem? ps. please always mention your exact CakePHP version! Also it wouldn't hurt to know about the associations. Commented Oct 19, 2014 at 20:18
  • cakephp version mentioned Commented Oct 19, 2014 at 20:21

3 Answers 3

2

As already mentioned, this is exaplained in the docs, please read them, and in case you don't understand them (which would be understandable as the HABTM section is a little confusing and requires some trial & error), tell us what exactly you are having problems with.

http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm

Based on the examples shown, the format for saving multiple X to Y should be

Array
    (
        [Section] => Array
            (
                [id] => 1
            )
        [User] => Array
            (
                [User] => Array(3, 4)
            )
    )

The corresponding form could look like this:

<?php echo $this->Form->create('User'); ?>
    <?php echo $this->Form->input('Section.id'); ?>
    <?php echo $this->Form->input('User', array('multiple' => 'checkbox')); ?>
<?php echo $this->Form->end('Add Users'); ?>

And the data would be saved via the Section model, that way its modified column is being updated properly.

public function addUsersToSection($id) {
    // ...
    if($this->request->is('post')) {
        if($this->Section->save($this->request->data)) {
            // ...
        } else {
            // ...
        }
    } else {
        $options = array(
            'conditions' => array(
                'Section.' . $this->Section->primaryKey => $id
            )
        );
        $this->request->data = $this->Section->find('first', $options);
    }

    $users = $this->Section->User->find('list');
    $this->set(compact('users'));
}

Another way would be to restructure the array as shown by Vinay Aggarwal, that works fine, the only difference is that it requires saving through the join model, and consequently it doesn't update the Section models modified column.

Sign up to request clarification or add additional context in comments.

2 Comments

It wish to do it in habtm table and not user or section table
And I wish you'd at least try it @aWebDeveloper , because if you would, you'd see that it does exactly what you're asking for.
0

CakePHP has the saveMany function, as mentioned in the documentation:

Model::saveMany(array $data = null, array $options = array())¶

3 Comments

i want for above case
We're going to need your existing code for the model and controller your using for this. You have only given the view.
Do you have a model for the sections table yet?
0

This is HABTM relation, first of all you need have relation setup in SectionUser model. Then you can use saveAll() method to save all records at once.

Array
(
    [0] => Array
    (
        [user_id] => 33
        [section_id] => 9
    )

    [1] => Array
    (
        [user_id] => 33
        [section_id] => 10
    )
)

Make sure your data array is in above given format.

$this->SectionUser->saveAll($data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.