Answer the question
In order to leave comments, you need to log in
How to competently implement the functionality of individual prices in Yii2?
Task: to implement individual prices for services for some users. Not every service can have its own individual price (that is, if there is no individual price, display the standard one).
Simplified table schema:
Answer the question
In order to leave comments, you need to log in
SELECT `services`.*, `prices`.`amount` FROM `services`
LEFT JOIN `prices` ON `prices`.`service_id` = `services`.`id` AND `prices`.`user_id` = 100
Services::find()->select(['services.*', 'prices.amount'])->leftJoin('prices', ['AND',
'services.id = prices.amount',
['prices.user_id' => Yii::$app->user->id],
])->asArray()->all();
The actual price must be a method in the services. And in this method already implement all the magic. I think this will be a connection with individual_price, checking if there is one and if not setting the base one.
Further on afterFind: what prevents using mass saving through for example:
https://www.yiiframework.com/doc/api/2.0/yii-db-ac...
and I don’t quite understand why and how you added AttributeBehavior here by sampling from the database, everything was correctly suggested by Dmitry Kim
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question