How to insert multiple records

 ̄綄美尐妖づ 提交于 2019-12-06 10:34:21

Let me explain everything.

First of all your html form must be looks like following.

<?php echo $this->Form->create('User'); ?>
<tr>
    <td>
        <?php
            echo $this->Form->input('User.0.address',array
            (
                'div'   => null,
                'label' => false,
                'class' => 'span required'
            ));
        ?>

        <?php
            echo $this->Form->input('User.1.address',array
            (
                'div'   => null,
                'label' => false,
                'class' => 'span required'
            ));
        ?>

        <?php
            echo $this->Form->input('User.2.address',array
            (
                'div'   => null,
                'label' => false,
                'class' => 'span required'
            ));
        ?>
    </td>
    <td>
        <input type="submit" value="Add" >
    </td>
</tr>
<?php echo $this->Form->end(); ?>

As you can see to save many record at same time using cakephp you must define it as above it will parse input fields as array this is cakephp convention.

I mean User.0.address for first array element

User.1.address for second array element

User.2.address for third array element and so on.

Now.

In Controller file.

<?php
    function add()
    {
        $this->User->saveAll($this->data['User']);
    }

And yes here you are done saving multiple record at same time.

I just gave you how cakephp works all you need to do is set above hint as per your need.

Best of luck...

Cheers...

Feel Free to ask... :)

Try this saveall() in your query except save, hope this helps

 if($this->Agent->saveall($this->data))

Let me know if it work.

I think this

php echo $this->Form->create('Agents', array('action' => 'send_money'));?>

should be replaced with

php echo $this->Form->create('Agent', array('action' => 'send_money'));?>

and use saveall() in place of save.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!