Laravel 4数据库插入错误-preg_replace():参数不匹配,模式是字符串,而替换是数组 [英] Laravel 4 Database insert Error - preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

查看:133
本文介绍了Laravel 4数据库插入错误-preg_replace():参数不匹配,模式是字符串,而替换是数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库播种器中,我只想向表中插入一些硬编码的数据.

In database seeder, I just want to insert some hard-coded data to the table.

$Pro1_id = DB::table('projects')->select('id')->where('projectName', '=', 'Project A')->get();
    $data1_1 = array(
            'id'                =>      1,
            'projectID'         =>      $Pro1_id,
            'attributeID'       =>      1,
            'levelID'           =>      2,
            'percentage'        =>      20,
            'risk_value'        =>      25186.86311,
            'expectation_value' =>      706455.9401,
        );
    $data1_2 = array(
            'projectID'         =>      $Pro1_id,
            'attributeID'       =>      2,
            'levelID'           =>      1,
            'percentage'        =>      60,
            'risk_value'        =>      530351.3397,
            'expectation_value' =>      392207.1248,
        );
    $data1 = [$data1_1, $data1_2];
    DB::table('Mapping')->insert($data1);

但是,我得到了错误:

[ErrorException] preg_replace():参数不匹配,模式是 字符串,而替换是一个数组

[ErrorException] preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

这太不可思议了,因为我对另一个表也做了同样的事情.

DB::table('projects')->insert(array(
                array(
                    'id'            =>  Webpatser\Uuid\Uuid::generate(),
                    'projectName'   =>  'Project A',
                    'creator_id'    =>  $pro1_creatorID,
                    'create_at'     =>  \Carbon\Carbon::now()->toDateString(),
                    'lastEditor_id' =>  $pro1_creatorID,
                    'edit_at'       =>  \Carbon\Carbon::now()->toDateString(),
                    'utility'       =>  1.597119661,
                    'exponential'   =>  4.94,
                    'projectValue'  =>  1225090.39
                ),
                array(
                    'id'              =>  Webpatser\Uuid\Uuid::generate(),
                    'projectName'   =>  'Project B',
                    'creator_id'    =>  $pro2_creatorID,
                    'create_at'     =>  \Carbon\Carbon::create(2014, 12, 12)->toDateString(),
                    'lastEditor_id' =>  $pro2_creatorID,
                    'edit_at'       =>  \Carbon\Carbon::create(2014, 12, 12)->toDateString(),
                    'utility'       =>  1.754989409,
                    'exponential'   =>  5.78,
                    'projectValue'  =>  293760.36
                ),
                array(
                    'id'              =>  Webpatser\Uuid\Uuid::generate(),
                    'projectName'   =>  'Project C',
                    'creator_id'    =>  $pro3_creatorID,
                    'create_at'     =>  \Carbon\Carbon::create(2013, 10, 21)->toDateString(),
                    'lastEditor_id' =>  $pro3_creatorID,
                    'edit_at'       =>  \Carbon\Carbon::create(2013, 10, 21)->toDateString(),
                    'utility'       =>  1.423114267,
                    'exponential'   =>  4.15,
                    'projectValue'  =>  1461924.67

                )
            )
        );

我真的不明白为什么将插入项目表起作用,但是映射表之一不起作用. 它们是完全相同的方法.

I really don't understand why inserting into projects table works, but the one of the mapping table does NOT work. They are exactly the same method.

推荐答案

我认为您的代码是正确的,但是当您将id插入数组时,您做错了方法.

I think your code is correct but when you insert the id in array, you are doing the wrong way.

$Pro1_id = DB::table('projects')->select('id')->where('projectName', '=', 'Project A')->get();

此处,$Pro1_idCollection,其中包含查询的返回值.有时可能是一个,但有时可能是2或3....因此,当您在数组中插入id时,您执行的方式是错误的.因此,使用foreach循环是这样的:

Here, $Pro1_id is Collection that contain value return from your query. Sometimes it might be one, but sometimes it might be 2 or 3.... So , your are doing the wrong way when you are inserting the id in the array. So , use foreach loop like this :

foreach($Pro1_id as $pro){
    DB::table('Mapping')->insert(array(
            'id'                =>      1,
            'projectID'         =>      $pro->id,
            'attributeID'       =>      1,
            'levelID'           =>      2,
            'percentage'        =>      20,
            'risk_value'        =>      25186.86311,
            'expectation_value' =>      706455.9401,
        ));
}

为简单起见,get返回Collection,而应该获取多个rows.

For simple , get returns Collection and is rather supposed to fetch multiple rows.

有关更多信息. 选中此

这篇关于Laravel 4数据库插入错误-preg_replace():参数不匹配,模式是字符串,而替换是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆