Extend Model to ActiveRecord

Hello Everyone,

I am developing a app for accommodation Quotations.

I created a Quotation Model for price calculation using the properties like: date, number of nights, number of adults.

Price calculations are made with a controller and the model, without database (class Quotation extends \yii\base\Model).

When the customer accept the quotation and pays thought our payment gateway, I would like to convert the info of the Quotation into a database record "Order" with the same properties of Quotation + additional properties like: status, date of payment.

My first idea was to extend the Quotation class, with database feature like \yii\db\ActiveRecord.

It doesn’t look like it’s possible.

What can I do other than duplicate Quotation (class Order extends \yii\base\ActiveRecord)?

Kind regards

Nico

You could link the quote with the order (foreign key).

Hello Patrick

My Quotation model doesn’t have database. I don’t need a database for Quotations, only after the quotation is paid it is should be stored as an “Order” activerecord.

Oh sorry, I missed that part.

I would assume that order and quotation behave quite differently. Thus despite them sharing a fair couple of attributes, I’d have separate classes for them.

If you don’t want to do that at all, how about making quotation an ActiveRecord (without ever saving it) and extending order from that?

Cant you just transfer you Quotation data to a new Order?




$order = new Order();

$order->attributes = $paidQuotation->getAttributes();

$order->payment_date = 'xxxxxx';

...

$order->save();



Hey slinstj, it’s a nice solution. But I think I would still need to recreate/duplicate the properties on Order. And that’s what I am trying to avoid. I have 30 properties on Quotation.

getAttributes() only gets the values but not the definitions of properties, right?

And Patrick, yes I could store Quotation as ActiveRecord, and not saving it in database. I am just worried about performance. Do Model and ActiveRecord use the same amount of memory? My project calculates a good number of quotation for each day, room, accommodation. It then compares Quotations and calculates the best price/Quotation for a period of time.

[color="#1C2837"][size="2"]

[/size][/color]

[color="#1C2837"] [/color]

[color="#1C2837"] [/color]

[color="#1C2837"] [/color]

You had said you would save "with the same properties of Quotation".

BTW, getAttributes will return a $name => $value array.

Yes the properties in Quotation and Order are almost the same. They have 30 properties in common. And Order have 5 additionnal properties.

They also have rules in common. But I guess I can get Quotation::rules(), the same way I could get parent::rules() if Order was extended from Quotation.

I will try this method and post again later. Thanks.

I definitively would not recommend you "extend Order from Quotation" since they are completely different things (an order is not a quotation). It does more sense to have two separate AR in your case.

The performance you are worried about only would probably be a problem when your system would be working with thousands of concurrents queries.

Thanks for your advice slinstj. You are probably right, “Don’t repeat yourself” has some limits ;)

If you are starting out with your project now, then quote and order will look similar. But half a year down the road, you’ll be very happy you put them into two different and independent classes.