atk4 / data

Data Access PHP Framework for SQL & high-latency databases
https://atk4-data.readthedocs.io
MIT License
273 stars 46 forks source link

hasMany referencing a table in another db generates ciritcal error from lack of fallback field #998

Closed samsouthardjr closed 2 years ago

samsouthardjr commented 2 years ago

With two classes:

class Parent extends Model
{
   public $table = 'db1.parent';

   protected function init(): void
   {
      parent::init();
      $this->addField('name');
      $this->hasMany('children', ['model' => [Child::class]]);
    }
}

class Child extends Model
{
   public $table = 'db2.child';

   protected function init(): void
   {
      parent::init();
      $this->addField('name');
      $this->hasOne('parent_id', ['model' => [Parent::class]]);
   }
}

With those classes, the code:

$p = new Parent($db);
$c = $p->ref('children');

will generate an exception "Atk4\Data\Exception: Their model does not contain fallback field" as it will attempt to reference a their_fallback_field of 'db1.parent_id' (it should be simply 'parent_id').

Adding "their_field' => 'parent_id' to the hasMany definition resolves this, but shouldn't be necessary