错误创建和获取模型

时间:2017-04-20 05:17:24

标签: php laravel orm

此模型表现不正常。它不会填充其属性,也不能在其上调用find()get()或任何类型的getter。

这是create_matches_table:

public function up()
{
    Schema::create('matches', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('p1');
        $table->integer('p2');
        $table->integer('u1');
        $table->integer('u2');
        $table->string('p1_action')->nullable();
        $table->string('p2_action')->nullable();
        $table->bigInteger('clock')->unsigned();
        $table->smallinteger('round')->unsigned()->default('1');
        $table->integer('victor')->nullable();
        $table->boolean('murder');
        $table->integer('wager')->unsigned()->nullable();
        $table->integer('notoriety')->nullable();
        $table->integer('stalemate')->default('0');
        $table->timestamps();
        $table->datetime('finished_at')->nullable();
    });
}

使用Match::create($data)其中$data是与所需内容完美对齐的属性数组,不会填充它们。返回的对象永远不会有上面所需的clock属性。

我不情愿地使用以下方式绕过这些问题:

public static function setMatch($data)
{

    $match = new Match;
    $match->fill($data);
    $match->clock = time() + 6;
    $match->save();

    return $match;
}

即使这样做,尝试通过$match = Match::find([$id])->first();获取此对象的特定记录也会产生此错误:

  

SQLSTATE [HY000]:常规错误:2031(SQL:select * from matches   Connection.php第729行中的matchesid位于(?)中

at
> Connection->runQueryCallback('select * from `matches` where
> `matches`.`id` in (?)', array(), object(Closure)) in Connection.php
> line 685 at Connection->run('select * from `matches` where
> `matches`.`id` in (?)', array(), object(Closure)) in Connection.php
> line 349 at Connection->select('select * from `matches` where
> `matches`.`id` in (?)', array(), true) in Builder.php line 1610

我尝试过硬编码值,将其作为数组传递,甚至只需要Match::first(),并且都返回此错误。

我所需要的就是让它成为一个普通的OL模型。

2 个答案:

答案 0 :(得分:2)

first()之后find()find(),因为Match::find($id); 会返回一个对象:

$fillable

另外,请确保clock具有正确的属性,并create() public function setClockAttribute($value) { $this->attributes['clock'] = time() + 6; } dataArray = [[response valueForKey:@"data"]mutableCopy]; NSPredicate *departmentPredicate = [NSPredicate predicateWithFormat:@"department == %@",selectedDepartment]; doctorsArray =[[dataArray filteredArrayUsingPredicate:departmentPredicate]mutableCopy]; doctorsNameArray = [[doctorsArray valueForKey:@"doctors"]mutableCopy]; doctorsListArray = [[doctorsNameArray valueForKey:@"name"]mutableCopy]; 属性使<__NSArrayM 0x7ca81f10>( { department = Endodontics; doctors = ( { id = 11; name = "Dr Alex"; } ); id = 7; }, { department = "Operative Dentistry"; doctors = ( { id = 9; name = "Dr Amal K Ramachandran"; }, { id = 6; name = "Dr Rahman"; }, { id = 10; name = "Dr varsha"; } ); id = 8; }, { department = Othondontics; doctors = ( { id = 8; name = "Dr. Rashid KC"; }, { id = 5; name = Prasad; } ); id = 5; }, { department = Periodontics; doctors = ( { id = 7; name = "Dr Sreya Vishwanath"; } ); id = 6; } ) 有效:

<__NSArrayM 0x7c9553e0>(
{
department = Othondontics;
doctors =     (
            {
        id = 8;
        name = "Dr. Rashid KC";
    },
            {
        id = 5;
        name = Prasad;
    }
);
id = 5;
}
)

答案 1 :(得分:0)

我认为您的代码需要更新如下:

Match::find([$id])->first();

Match::find($id);

或者

Match::where('id',$id)->first();

希望这对你有用!