The written string has all defaults (null, 0, etc.).
I'm using Laravel 5.1 and trying to insert a string into a remote database that connects through an SSH tunnel and it reads fine. I have a local DB and various remote DBs. This code was used to work, I just cloned my blob on DigitalOcean to point to another remote DB and the problem came up.
I get the primary key id when I create the remote Promotion
, but when I read it and check it in MySQL Workbench, all the fields have default values, mostly NULL
and 0
. Any idea what's going on?
Below is the configuration for the remote DB I am trying to write.
DB configuration
'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,
],
Model
<?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);
}
}
** Creation **
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 and 0 and whatnot.
NOTE. \Promotion::
is a promotion in a local DB, I basically copy it to a remote DB.
source to share
This did the trick:
class Promotion extends Model
{
...
public function __construct($attributes = [])
{
parent::__construct($attributes);
$this->connection = env('APP_ENV');
}
...
}
Since I have a constructor in my class that extends Eloquent Model
, I also need to call the constructor Model
before I do anything.
source to share