samedi 26 décembre 2015

Laravel MongoDB relationships break when deployed

I have a Laravel 5.1 project that uses a hybrid of MySQL and Mongo Databases that drill down and up using Foreign Keys and Eloquent Relationships. Specifically, I use MySQL for the Users table and a metadata 'lists' table with foreign key 'user_id', then 2 MySQL tables for with list values from specific sources and 2 Mongo Collections for list values from different sources - all the source tables have the lists_id foreign key. This allows me to do things like:

$user = \projectName\User()::find(X);
$list = $user->lists()->where('user_id',Y);
$source = $list->source.'_values';
$values = $list->$source;

Now, this is working fine on my local envirnoment, but when I deployed it to the server only the MySQL relationships are working - the Mongo relationships are returning empty Laravel Collections.

Example User Model Relationships (MySQL)

use Jenssegers\Eloquent\Model;

...

public function lists() {  //MySQL
    return $this->hasMany('projectName\lists');
}

Example Lists Model (MySQL)

use Jenssegers\Eloquent\Model;

...

public function user() {          //MySQL
    return $this->belongsTo('projectName\User');
}

public function source1_values() { //MySQL
    return $this->hasManyThrough('projectName\source1','projectName\lists');
}

public function source2_values() { //MySQL
    return $this->hasManyThrough('projectName\source2','projectName\lists');
}

public function source3_values() { //MONGODB
    return $this->hasManyThrough('projectName\source3','projectName\lists');
}

Example Source3 Model

use Jenssegers\Mongodb\Model;

... 

 public function lists() { //MySQL
    return $this->belongsTo('projectName\lists');
}

public function user() {   //MySQL
    return $this->belongsTo('projectName\User');
}

Important thing to note is that it all works find on my machine, even when I link up to the remote MySQL and MongoDB's, but when deployed the $lists->source3_values returns and empty collection. The MySQL database in on it's own server, and the mongoDB is on the same server as the Laravel Project. What's also strange is that using

ssh deploymentServer
cd project/path
php artisan tinker
$source3 = projectName\source3::first();
$source3->lists

Works! But

ssh deploymentServer
cd project/path
php artisan tinker
$list = projectName\lists::find(X); //Appropriate X for a source3 list
$list->source3_values

Doesn't...

Any ideas why this would work on my local environment but not once deployed?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire