如何插入多条记录

时间:2013-02-12 11:19:21

标签: php sql cakephp-2.0

我是一个新的蛋糕php开发者。我正在尝试插入多个记录,但我不能。我的表结构如下: 表名: 剂

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

控制器代码:

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());    
            }
    }

型号代码是:

<?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
    )),



    );

 }

&GT;

无法使用此代码插入记录。我该怎么办?

3 个答案:

答案 0 :(得分:3)

在您的查询中尝试使用saveall(),除了保存,希望这有帮助

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

让我知道它是否有效。

答案 1 :(得分:3)

让我解释一切。

首先,您的html表单必须如下所示。

<?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(); ?>

正如您可以看到使用cakephp同时保存许多记录,您必须将其定义为上面它将输入字段解析为数组 cakephp convention

我的意思是User.0.address for first array element

User.1.address for second array element

User.2.address for third array element等等。

现在

在控制器文件中。

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

是的,在这里你完成了同时保存多条记录。

我刚刚给了你cakephp的工作方式,你需要做的就是根据你的需要设置上面提示。

祝您好运......

干杯...

随意问......:)

答案 2 :(得分:1)

我想这个

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

应替换为

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

并使用saveall()代替save。

相关问题