eloquence-mappable icon indicating copy to clipboard operation
eloquence-mappable copied to clipboard

Mapping ignored if db connection is not hardcoded as protected $connection = 'string'

Open Anecho opened this issue 2 years ago • 0 comments

Hello!

I've found out that mapping is not working if database connection is set within models __construct() method by calling $this->setConnection( config('custom.connection') )

namespace App\Models\ERP;

use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Mappable;

abstract class AbstractBaseModel extends Model
{
    use Eloquence, Mappable;

    public $timestamps = false;

If I hardcode connection name as string like this - everything works well (1st screenshot)

    protected $connection = 'ERP_1';
}

But if it is set within constructor - all mapped keys are returning null. Original keys return correct values though. (2nd screenshot)
(Last element of array: originalModel->getAttributes(), difference is in the case of first letter)

    // protected $connection = 'ERP_1';

    public function __construct()
    {
        $connection = config('erp.connection');
        $this->setConnection($connection);
        $this->connection = $connection;
        return $this;
    }

image image

Tested on: Laravel 5.8
PHP 7.2.34 (cli)
"sofa/eloquence-mappable": "^7.0"

Sorry if I am missing something. Any help will be much appreciated, this package is really awesome, helps to standardise different ERP db structure queries!

Thanks!

Anecho avatar May 13 '22 20:05 Anecho