写入的行具有所有默认值(null,0等)

时间:2015-07-20 16:49:22

标签: php mysql laravel eloquent

我正在使用Laravel 5.1并尝试将一行插入到通过SSH隧道连接的远程数据库中,并且它正在读取。我有一个本地数据库和各种远程数据库。这个代码曾经工作过,我只是在DigitalOcean中克隆了我的Droplet,这样它就会指向另一个远程数据库并出现问题。

当我创建远程Promotion时,我得到了一个主键ID,但是当我读回它并在MySQL Workbench上检查时,所有字段都有默认值,主要是NULL和{{1} }。知道发生了什么事吗?

以下配置适用于我正在尝试写入的远程数据库。

数据库配置

0

模型

'prod' => [
    'read'      => [
        'options' => [
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET time_zone = \'-04:00\''
            ]
    ],
    'write'     => [],
    'driver'    => 'mysql',
    'host'      => env('PROD_DB_HOST', 'localhost'),
    'port'      => env('PROD_DB_PORT', '3306'),
    'database'  => env('PROD_DB_DATABASE', 'forge'),
    'username'  => env('PROD_DB_USERNAME', 'forge'),
    'password'  => env('PROD_DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

**创作**

<?php

namespace App\Models\Remote;

use Illuminate\Database\Eloquent\Model;

class Promotion extends Model
{
    protected $connection = null;
    protected $table = 'Tournament';
    protected $primaryKey = 'tournamentId';
    protected $guarded = ['tournamentId'];
    public $timestamps = false;

    public function __construct()
    {
        $this->connection = env('APP_ENV');
    }

    public function gameplayLogs()
    {
        return $this->hasMany('GamePlayLog', 'tournamentId');
    }

    public function scopeActive($query)
    {
        return $query->where('status', 3);
    }
}

注意:public function deployPromotionMain($id) { $post = json_decode(file_get_contents("php://input"), true); $today = date('Y-m-d H:i:s'); $promo = \Promotion::with(['assets'])->find($id); $data = [ 'name' => $promo->name, 'description' => $promo->description, 'gameId' => intval($promo->gameId), 'categoryId' => intval($promo->categoryId), 'subCategoryId' => intval($promo->subCategoryId), 'advertiserId' => $promo->advertiserId, 'advertiserCost' => 0, 'gamePlays' => $promo->gamePlays, 'promoStartDate' => '2015-01-01 00:00:00', 'promoEndDate' => '2015-01-02 00:00:00', 'createdDate' => $today, 'updatedDate' => $today, 'promoFileDuration' => 0, 'eventStartDate' => $promo->promoStartDate, 'eventEndDate' => $promo->promoEndDate, 'eventStartTime' => '0:00 AM', 'eventEndTime' => '11:00 PM', 'minPlays' => 1, 'maxPlays' => 0, 'minAge' => 18, 'maxAge' => 0, 'scoringId' => 1, 'minLevel' => 1, 'maxLevel' => 0, 'tournamentDifficultyId' => 1, 'timeId' => 1, 'timeOut' => 15, 'defaultPrice' => 0, 'limitGames' => 1, 'distributorId' => 0, 'stateId' => 0, 'locationTypeId' => 0, 'questionSetId' => 0, 'status' => 3, 'sendEmail' => 1, 'kioskModelId' => 2, 'triviaCategoryId' => 1, 'duration' => 0 ]; foreach ($promo->assets as $a) { switch($a['assetTypeId']) { case 1: $data['eventFilePath'] = 'uploadfiles/tournament/event/'.$a['assetName']; $data['promoFilePath'] = 'uploadfiles/tournament/promo/'.$a['assetName']; break; case 2: $data['imageFilePath'] = 'uploadfiles/tournament/image/'.$a['assetName']; $data['thumbnailFilePath'] = 'uploadfiles/tournament/image/'.$a['assetName']; break; case 3: $data['iconFilePath'] = 'uploadfiles/tournament/icon/'.$a['assetName']; break; case 7: $data['ruleFilePath'] = 'uploadfiles/tournament/rule/'.$a['assetName']; break; default: break; } } foreach ($post['env'] as $env) { if ($env == 'dev') { $dev_promo = \PromotionRemote::create($data); ... //At this point I dumped $data and it had the right values but the entry had all null's and 0's and whatnot. 是本地数据库中的促销,我基本上将其复制到远程数据库。

1 个答案:

答案 0 :(得分:0)

这就是诀窍:

function myfunc() {
  alert ('working ..!');
}
$('.classname').on('click', myfunc);
$(document).on('load', myfunc);

因为我的类上有一个构造函数,它扩展了Eloquent的class Promotion extends Model { ... public function __construct($attributes = []) { parent::__construct($attributes); $this->connection = env('APP_ENV'); } ... } ,所以我需要在做任何事之前调用Model的构造函数。