简体   繁体   中英

How to insert multiple records

I am a new cake php developer.I am trying to insert multiple record but i cant.My table Structure is given below: Table Name : agents

==============================================  
 id | Contact |  Name  |  email_add  | Address
==============================================

Controller Code :

public function send_money()
    {
         $this->layout='agent';
         $this->Agent->create();
         $this->Agent->set($this->data);

         if(empty($this->data) == false)
            {
                    if($this->Agent->save($this->data))
                    {
                        $this->Session->setFlash('Information Added Successfully.');
                        $this->redirect('send_money');
                    }

            }
            else
            {
                $this->set('errors', $this->Agent->invalidFields());    
            }
    }

Model Code Is :

<?php
App::uses('AppModel', 'Model');
/**
 * Admin Login Model
 *
 */
 class Agent extends AppModel
 {
    public $name='Agent';
    public $usetables='agents';

    public $validate = array(

    'contact' => array(
    'contact_not_empty' => array(
        'rule' => 'notEmpty',
        'message' => 'Please Give Contact No',
        'last' => true
        ),
    ),

      'name' =>array(
      'rule' => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...)
      'allowEmpty' => false,
      'message'    => 'Please Enter Name.'
    ),

    'email_add' => array(
            'email_add' => array(
            'rule' => 'email',
            'allowEmpty' => true,
            'message' => 'Please Enter Valid Email',
            'last' => true
    )),



    );

 }

?>

Its not possible to insert records with this code.What should I do?

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

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

Let me know if it work.

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... :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM