Saturday, 19 January 2019

RedbeanPHP Many-To-Many Relation - Odd behaviour

I'm using RedbeanPHP for my DB Layer and I have a many to many relationship between 2 tables.

The tables are:

  • kioskmodels
  • components

Where kioskmodels can have multiple components in the 1 model. Currently, I don't allow RedbeansPHP to add/remove db tables fluidly. I like to have manual control over that.

So I have a table called: kioskmodels_components with 2 columns; 1. kioskmodels_id 2. components_id

The problem, when I'm adding a new record into kioskmodels, using the dispense function as below:

if(!$this->isEdit()) $kiosk_model = R::dispense('kioskmodels');

$kiosk_model->import($this->getValidation()->data['kioskmodels']);

if(!empty($this->getValidation()->data['components'])) {
    $kiosk_model->sharedComponentsList = $this->getValidation()->data['components'];
}

R::begin();

try {
    R::store($kiosk_model);
    R::commit();
} catch(\Exception $e) {
    R::rollback();
    throw $e;
}

What happens is when I'm adding a new record, I dispense a new kioskmodel bean. If it is editing an existing record, the R::load is used earlier on.

However, when inserting a new kioskmodels and associating it with components, it wants to use a table called "components_kioskmodels". When this happened, I went along with it and changed the db table to match it.

However when I tested the edit setting, it wanted to use "kioskmodels_components" as I originally had it (Which I'd prefer).

Further, inside the "$this->getValidation()->data['components']" as an array of Beans using the R::loadAll('components', $value); and value comes from the user.

Unfortunately, the documentation has not been helpful in terms of getting this resolved.



from RedbeanPHP Many-To-Many Relation - Odd behaviour

No comments:

Post a Comment