ardent icon indicating copy to clipboard operation
ardent copied to clipboard

Unique calidation + multiple connections

Open orzilca opened this issue 9 years ago • 4 comments

When trying to use _unique_ with multiple db connections, ardent tries to fetch from the wrong connection

protected $connection   = 'mysql_platform';
public static $rules    = array(
        'email'       => 'required|email|unique:users,email',
    );

when validating, getting the following error

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'WRONG_DATABASE_NAME.users' doesn't exist (SQL: select count(*) as aggregate from `users` where `email` = ***)

Any ideas?

orzilca avatar Nov 29 '14 16:11 orzilca

I have the same issue.

LastRide avatar Dec 18 '14 15:12 LastRide

Hi @orzilca and @LastRide were you able to resolve this issue?

mrgrt avatar Sep 16 '15 11:09 mrgrt

@mrgrt nope :(

LastRide avatar Sep 16 '15 16:09 LastRide

@LastRide I'm using Ardent outside the Laravel Framework and had to use the configureAsExternal method.

I modified the code to set the $db variable as a static property for the class. I'm not sure if this is the best approach but just now I have done the following:

public static function testMethod(){
        self::$db = new DatabaseCapsule;
    }
    public static function anotherMethod(array $connection, $lang = 'en', $name){
        self::$db->addConnection($connection,$name);
        self::$db->setEventDispatcher(new Dispatcher(new Container));
        // Make this Capsule instance available globally via static methods
        self::$db->setAsGlobal();

        self::$db->bootEloquent();

        $translator = new Translator($lang);
        $translator->addLoader('file_loader', new PhpFileLoader());
        $translator->addResource('file_loader',
            dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$lang.
            DIRECTORY_SEPARATOR.'validation.php', $lang);

        self::$externalValidator = true;
        self::$validationFactory = new ValidationFactory($translator);
        self::$validationFactory->setPresenceVerifier(new DatabasePresenceVerifier(self::$db->getDatabaseManager()));

    }

//Instantiate the datbase capsule
LaravelBook\Ardent\Ardent::testMethod();

LaravelBook\Ardent\Ardent::anotherMethod(array(
  'driver'    => 'mysql',
  'host'      => '127.0.0.1',
  'port'      => 3306,
  'database'  => 'password',
  'username'  => 'user',
  'password'  => 'pass',
  'charset'   => 'utf8',
  'collation' => 'utf8_unicode_ci'
), 'en', 'db1'); //English is the default messages language, may be left empty

LaravelBook\Ardent\Ardent::anotherMethod(array(
  'driver'    => 'mysql',
  'host'      => '127.0.0.1',
  'port'      => 3306,
  'database'  => 'dbname',
  'username'  => 'dbuser',
  'password'  => 'pass',
  'charset'   => 'utf8',
  'collation' => 'utf8_unicode_ci'
), 'en', 'db2'); //English is the default messages language, may be left empty
<?php


use Illuminate\Database\Capsule\Manager as DB;

class User extends LaravelBook\Ardent\Ardent
{


    protected $connection = 'db1';

}

?>

mrgrt avatar Sep 17 '15 09:09 mrgrt