为什么foreach($ array为$ value)会导致Indeterminate数据类型

时间:2015-05-12 14:38:24

标签: php postgresql laravel

我做了以下方法,这里没什么特别但我得到一个令人困惑的错误:

SQLSTATE[42P18]: Indeterminate datatype: 7 FEHLER: konnte Datentyp von Parameter $2 nicht ermitteln (SQL: select * from "roles" where ("name" = Admin) limit 1)
     

(“无法确定参数$ 2的类型”)

好像我们不知道Line

中的哪个数据类型$ value
$role = Role::firstOrNew(array('name' => $value));
实际上是。

我很快将其更改为

$role = Role::firstOrNew(array('name' => '$value'));
一切都很好。事实上,我使用几乎相同的程序上面的几行让我想知道为什么php不知道返回的数据类型。

也许有人可以向我解释幕后的魔力:)

<?php
namespace App\Modules\AddressManagement\Seeds;

use Carbon\Carbon;
use Eloquent, Permission, DB, Voucher, Role, Log;

class DatabaseSeeder extends \Illuminate\Database\Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Eloquent::unguard();
        // Add Permissions
        $permissions = [
            'am_read_global_address' => ['Bundesweit Adressen lesen' => ['Admin', 'Vertriebsleiter']],
            'am_write_global_address' => ['Bundesweit Adressen hinzufügen' => ['Admin', 'Vertriebsleiter']],
            'am_read_region_address' => ['Regional Adressen lesen' => ['Regionalleiter']],
            'am_write_region_address' => ['Regional Adressen schreiben' => ['Regionalleiter']],
            'am_read_branches_address' => ['Branchnweit Adressen lesen' => ['Branchenmanager']],
            'am_write_branches_address' => ['Branchenweit Adressen schreiben' => ['Branchenmanager']]
        ];


        // Deploy permission to database
        // Update if permission already exists
        foreach ($permissions as $pname => $roles) {
            $permission = Permission::firstOrNew(array('name' => $pname));
            $permission->display_name = key($roles);
            $permission->save();

            foreach ($roles as $value) {
                $role = Role::firstOrNew(array('name' => $value)); // Without enclosed ' we get indeterminate datatype, php PLS 
                $role->save();
                $role->perms()->attach($permission->id);
            }
        }
    }
}

修改 像下面的答案一样,$ value实际上是一个数组。这不是故意的。我在解决方案之前在我的解决方案中有一个array_pop,但由于某些原因我将其删除并得到了错误。没有想到这一点。完整的代码在这里(它的工作原理,所以一切都很好!)。单引号将变量变为纯字符串的事实对我来说是新的,所以对于它来说就是这样。

public function run()
{
    Eloquent::unguard();
    // Add Permissions
    $permissions = [
        'am_read_global_address' => ['Bundesweit Adressen lesen' => ['Admin', 'Vertriebsleiter']],
        'am_write_global_address' => ['Bundesweit Adressen hinzufügen' => ['Admin', 'Vertriebsleiter']],
        'am_read_region_address' => ['Regional Adressen lesen' => ['Regionalleiter']],
        'am_write_region_address' => ['Regional Adressen schreiben' => ['Regionalleiter']],
        'am_read_branches_address' => ['Branchnweit Adressen lesen' => ['Branchenmanager']],
        'am_write_branches_address' => ['Branchenweit Adressen schreiben' => ['Branchenmanager']]
    ];


    // Deploy permission to database
    // Update if permission already exists
    foreach ($permissions as $pname => $perm) {
        $permission = Permission::firstOrNew(array('name' => $pname));
        $permission->display_name = key($perm);
        $permission->save();
        $roles = array_pop($perm);

        foreach ($roles as $value) {
            $role = Role::firstOrCreate(array('name' => $value));
            try {
                $role->perms()->attach($permission->id);
            } catch(\Exception $e) {
                // Nothing but duplicate entry, ignore
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

似乎Role::firstOrNew期待一个结构不同的第一个论点并抱怨它无法处理你传递的任何内容(我可以在网上找到几个例子,但没有实际的Laravel文档来支持这个主张)。与您提供的参数中的name键关联的值很可能是一个字符串。当你这样做时:

'$value' 

您最终得到一个字符串,因为PHP中的单引号字符串不会尝试转换变量。由于key($roles)的值在您的示例中始终是一个字符串,并且$value是一个数组,因此我发现与name键关联的值可能是字符串而不是数组。也许是这样的?

 $role = Role::firstOrNew(array('name' => $value[0]));