odoo-jsonrpc
odoo-jsonrpc copied to clipboard
Model definition: HasMany/BelongsTo not being hydrated
I was trying to define a model for Sales Order, with a relation for its Lines.
I saw that the PurchaseOrder model in the test folders had a ::read method to grab with an array of id's. And given there are Relation Attribute types like HasMany
, BelongsTo
, etc I thought it would be easy to get Lines etc.
But.. :D
Situation 1:
Using Service Provider directly Works,
order_line
is filled and can be used.
#[Model('sale.order.line')]
class SaleOrderLine extends OdooModel {}
public function debug(Odoo $odoo, int $id)
{
$order = $odoo->find('sale.order', $id);
$lines = SaleOrderLine::read($order->order_line) // :array
}
Situation 2:
Using Model Error:
App\Odoo\SaleOrder::$orderLine must not be accessed before initialization
#[Model('sale.order.line')]
class SaleOrderLine extends OdooModel {}
#[Model('sale.order')]
class SaleOrder extends OdooModel
{
// #[Field('order_line')] // this one works
#[HasMany(SaleOrderLine::class, 'order_line')]
public array $orderLine;
public function orderLines(): array
{
return SaleOrderLine::read($this->orderLine); // *
}
}
public function debug(int $id)
{
$order = SaleOrder::find($id);
// then, where it fails:
$lines = $order->orderLine; // error: see above
$lines = $order->orderLines(); // error: see above
}
(kept it "orderLine" for clarity, could've used lines
of course, order_line
has the same result)
I'm guessing the relations aren't fully working yet, while filling up the model? ( OdooModel::hydrate
, which is on the HasFields
trait)?
There's currently only a test to check for saving a relation (PurchaseOrder). So I'm not quite sure how to go about this.
- Extend the relation classes eg
HasMany
- Extend the
hydrate
method to fill the property on the model properly, based on the given relation? - Use another definition, to just keep the ID array instead:
#[Field('order_line')]
Which does work in this case, but would stop me from using the Model to save the data and force me to grab the IDs in an array.) - ...