Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the connection that was set on the model instance #15

Open
reno1979 opened this issue Feb 15, 2017 · 2 comments
Open

Use the connection that was set on the model instance #15

reno1979 opened this issue Feb 15, 2017 · 2 comments

Comments

@reno1979
Copy link

reno1979 commented Feb 15, 2017

Copied from: #7

@yadakhov Thank you for creating this plugin.

Too bad the non default connection is not read from the instance of the model.

\App\Model::on('otherconnection')->getModel()->insertOnDuplicateKey([[....]]); // will still use the default connection

when rewriting the function to a non static, the instance settings are available.

public function insertOnDuplicateKey(array $data, array $updateColumns = null)
    {
        if (empty($data)) {
            return false;
        }

        // Case where $data is not an array of arrays.
        if (!isset($data[0])) {
            $data = [$data];
        }

        $sql = self::buildInsertOnDuplicateSql($data, $updateColumns);

        $data = self::inLineArray($data);

        return $this->getConnection()->affectingStatement($sql, $data);
    }
@yadakhov
Copy link
Owner

yadakhov commented Nov 3, 2017

Hey,

I looked into fixing this issue a few months ago. It wasn't as easy as I thought due to the design of the current code using public static functions.

Have you tried setting the connections in the model file? Just create a new Model for the second connection.

class YouModel extends Model 
{
    protected $connection= 'second_db_connection';
}

@dsandber
Copy link

dsandber commented Feb 7, 2019

I'm having the same issue -- I can't create a new model because the table name is determined dynamically at runtime. (I'm bulk inserting into a temporary table).

The issues seems to be here:

  public static function getTableName()
   {
       $class = get_called_class();

       return (new $class())->getTable();
   }

Instead we want to call on the instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants