This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Tschallacka
Tschallacka

When you have two databases set up in your database config you might run into that deferred binding doesn't work as it should. Thanks to the help of Daftspunk I was able to solve this.

In my model I have set $connection = 'otherdatabase than default october database';

And I have a formwidget in which i'm trying to save the deffered binding, so it only commits when the actual save action is done.

in the widget I have an ajax method called:

 public function onSaveInBetween() {
 }

it instantiates the needed feelds, and adds them to the field model with the session key

$rule = new PriceRule();
$rule->startdate = Carbon::createFromDate($val->startdate->year, $val->startdate->month, $val->startdate->day);
$rule->enddate = Carbon::creat..... and a lto more heh
$rule->save();
// saving the field as a deferred binding
$this->getRelationObject()->add($rule, $this->sessionKey);

Now this all works fine, and when the form is saved it actually shows the values.

But if you want to push back data after the ajax calls with the new value

   $this->vars['value'] = $this->getRelationObject()
                                ->withDeferred($this->sessionKey)
                                ->orderBy('created_at','desc')
                                ->get();

it will fail to include the deferred values

To achieve this we need to add the following line to the begining of the onSaveInBetween() function

 public function onSaveInBetween() {
       \October\Rain\Database\Models\DeferredBinding::extend(function($model) { $model->setConnection('exitproducts'); });

and it will do the deferred bindings showing properly :-)

ichakesachin3010215
ichakesachin3010215

Hi,

I am using two databases in my project, parallel or simultaneously. I have modified some core files and hard-coded database names in model and updates files, but i want to set this database name dynamically.

problem occurring with Validation, look at following snippets: class User extends UserBase { public $connection = 'plugindb'; /**

  • @var string The database table used by the model. */ protected $table = 'users';

    /**

  • Validation rules */ public $rules = [ 'name' => 'required', 'surname' => 'required', 'email' => 'required|between:3,64|email|unique:db_name.users', 'mobile' => 'numeric', 'password' => 'required:create|between:6,16|confirmed', 'password_confirmation' => 'required_with:password|between:6,16',
    ];

In above snippet, I have added $connection = "plugindb" and to check email as unique, used db_name.users.

I want to know how to set Connection dynamically ? to achieve multiple database implementation.

Thanks, Sachin.

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.