1

I can't seem to find a proper way to cast a parent record object to a child record object.

My models look like this:

ModelA:
  columns:
    col_a:integer

ModelB:
  inheritance:
    type:             concrete
    extends:          ModelA
  columns:
    col_b:integer

I'd like to do something like this:

$instanceB = (ModelB) $instanceA->copy();
$instanceB->setColB('whatever');
$instanceB->save();

Basically I need to copy all fields and relations in from instance A to instance B without having to hardcode the fields and relations.

Is it possible?

j0k
  • 22,600
  • 28
  • 79
  • 90
sanya
  • 860
  • 1
  • 8
  • 20

1 Answers1

2

IIRC there is no object to object casting in php (there are some hacky solutions though). You can copy all properties of a model object to an another e.g. like this:

$b = new ModelB();
$b->fromArray($instanceA->toArray());
$b->setColB('yepp')->save();
Community
  • 1
  • 1
1ed
  • 3,668
  • 15
  • 25