ybr-nx / laravel-mariadb

Add MariaDB JSON select support to Laravel
95 stars 7 forks source link

save() doesn't appear to use the correct connection #4

Closed Ancyker closed 6 years ago

Ancyker commented 6 years ago

I have a model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class RecolorItem extends Model
{
    protected $table = 'recolor_db';
    protected $primaryKey = 'nameid';
    protected $connection = 'cserver';
    public $timestamps = false;
}
?>

cserver is a dynamic connection set up in various controllers by middleware

    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('setserver');
    }

Here I'm using that model in a controller (with the above-mentioned middleware):

        $recolor = RecolorItem::findOrFail($id);
        $recolor->name = $input['name'];
        $recolor->price_points = $input['price_points'];
        $recolor->price_zeny = $input['price_zeny'];
        $recolor->save();

For some reason save() fails claiming the table doesn't exist because it's using the default connection. I find that odd because findOrFail appears to have loaded it just fine... switching to the mysql driver fixes the issue, so the problem must be in laravel-mariadb.

ybr-nx commented 6 years ago

Can you describe how you set up dynamic connection?

glennacheson commented 6 years ago

I'm experiencing the same issue.

Laravel 5.5 /config/database.php

'public_site' => [
            'driver' => 'mariadb',
            'host' => env('PUBSITE_DB_HOST'),
            'port' => env('PUBSITE_DB_PORT', 3306),
            'database' => env('PUBSITE_DB_DATABASE'),
            'username' => env('PUBSITE_DB_USERNAME'),
            'password' => env('PUBSITE_DB_PASSWORD'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
            'options'   => [
                \PDO::ATTR_EMULATE_PREPARES => true
            ],
        ],

Model

<?php

namespace App\Models\Cms;

use Illuminate\Database\Eloquent\Model;

class Content extends Model
{
    protected $connection = 'public_site';
    protected $table = 'cms_content';
    public $timestamps = false;
    protected $primaryKey = 'cmsid';

    protected $dates = [
        'updatedatetime', 
        'disabledatetime'
    ];
}

Steps to reproduce:

php artisan tinker
>>> $block = Content::find(101413)

App\Models\Cms\Content {#1158
     cmsid: "101413",
     webid: "1043",
     cbtitle: "test3",
     cbmemo: "<p>test2</p>\r\n",
     disable: "0",
     disabledatetime: null,
     updateuser: null,
     updatedatetime: "2018-07-31 14:41:03",
   }

>>> $block->cbtitle = 'test2'
=> "test2"
>>> $block->save()
Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sis.cms_content' doesn't exist (SQL: update `cms_content` set `cbtitle` = test2 where `cmsid` = 101413)'

The sis schema referenced is the default schema for the project. If I switch the driver to mysql in the /config/database.php the save() method works as expected.

composer.lock info

 {
            "name": "ybr-nx/laravel-mariadb",
            "version": "1.0.14",
            "source": {
                "type": "git",
                "url": "https://github.com/ybr-nx/laravel-mariadb.git",
                "reference": "4e9f28a52ae6e5b382a999fe95c803114065a60a"
 },
ybr-nx commented 6 years ago

@glennacheson thanks for detailed info. It should be fixed now.