class Service extends ActiveRecord
{
public function getServiceSpec()
{
return $this->hasMany(ServiceSpec::className(), ['service_id' => 'id']);
}
}
class ServiceSpec extends ActiveRecord
{
public function getService()
{
return $this->hasOne(Service::className(), ['id' => 'service_id']);
}
public function getServices()
{
return $this->hasMany(Service::className(), ['id' => 'service_id']);
}
}
$service = Service::findOne(1);
$serviceSpec = $service->serviceSpec;
$serviceSpec = ServiceSpec::findOne(1);
$services = $serviceSpec->services;
$service = Service::find()->where('id' => 1)
->joinWith(['serviceSpec' => function (ActiveQuery $query) {
$query->andWhere(['specialist_id' => 2]);
}])
->all();
$serviceSpec = $service->serviceSpec;
class Объявления {
private $продвижениеИд;
private $продвижениеВремя;
....
public function получитьПродвижение(): ?Продвижение
{
return new Продвижение($this->продвижениеИд, $this->Продвижение время);
}
public function этоПродвигаемое(): bool
{
return $this->получитьПродвижение() ? $this—>получитьПродвижение()->продвигаемое() : false
}
}
public function getServiceSpec()
{
return $this->hasMany(ServiceSpec::className(), ['service_id' => 'id']);
}
class Advert {
private $promotionId;
private $promotionTime;
....
public function getPromotion(): ?Promotion
{
return new Promotion($this->promotionId, $this->promotionTime);
}
public function isPromoted(): bool
{
return $this->getPromotion() ? $this—>getPromotion()->isPromoted() : false
}
}
class Promotion
{
private $id;
private $date;
public function __construct(int $id, DateTimeImmutable $date)
{
$this->id = $id;
$this->date = $date;
}
public function isPromoted(): bool
{ //Проверка
return $result;
}
}
/**
* @var TblItemQuery $query
*/
$query = TblItem::find()
->alias('tbl')
->select([
'obj.id AS objId',
'obj.name AS objName',
'obj.slug AS objSlug',
'obj.text_price AS objTextPrice',
'town.slug AS townSlug',
'type.slug AS typeSlug',
'room.id AS roomId',
'room.name AS roomName',
'room.osn AS roomOsn',
'room.dop AS roomDop',
'roomcat.name AS roomCatName',
'tbl.id AS tblId',
'tbl.kf AS tblAdultKf', // коэффициент взрослых
'tbl.text AS tblTextPrice', // текст под таблицей
'txt.text AS tblTextTplPrice', // текст под таблицей
'l.name AS lechenieName',
'p.name AS pitanieName',
'spoS.spoStandartTurist',
'spoS.spoStandartAgent',
'spoS.spoStandartMalutki',
'spoA.spoAverageTurist',
'spoA.spoAverageAgent',
'spoA.spoAverageMalutki',
'spoA.spoDays',
'colAge.minAge',
'colAge.maxAgeFree',
'col.is_free AS isFree',
new Expression("$this->totalDays AS totalDays"),
new Expression("$kfSum AS kfSum"),
new Expression("$this->kidAge AS kidAge"),
new Expression('MIN(`row`.`date_from`) AS `date_min`'),
new Expression('MAX(`row`.`date_to`) AS `date_max`'),
$this->sumExpression('osnTblPriceSpo', $kfSum, $sum === self::AS_OSN),
$this->sumExpression('osnTblPriceNetto', $kfSum, $sum === self::AS_OSN),
$this->sumExpression('osnTblPriceFreeSpo', $kfSum, $sum === self::AS_FREE),
$this->sumExpression('osnTblPriceFreeNetto', $kfSum, $sum === self::AS_FREE),
$this->sumExpression('dopTblPriceSpo', $kfSum, $sum === self::AS_DOP),
$this->sumExpression('dopTblPriceNetto', $kfSum, $sum === self::AS_DOP),
$this->sumExpression('numTblPriceSpo', $kfSum, $sum === self::AS_MIX),
])
->innerJoinWith([
'rows row' => static function (TblRowQuery $query) {
$query->innerJoinWith([
'data d' => static function (TblDataQuery $query) {
$query->innerJoinWith([
'col col' => static function (TblColQuery $query) {
$query->innerJoinWith([
'type coltype',
])->published();
},
]);
},
])->published();
},
])
->innerJoinWith([
'room room' => static function ($query) {
/** @var ActiveQuery $query */
$query
->innerJoinWith([
'item obj' => static function ($query) {
/** @var ActiveQuery $query */
$query->innerJoinWith(['town town', 'type type']);
},
])
->innerJoinWith(['category roomcat'])
->published();
},
])
->joinWith(['lechenie l', 'pitanie p', 'textRelation txt'])
->leftJoin(['colAge' => $this->getMinChildAgeQuery()], 'colAge.tblId = tbl.id')
->leftJoin(['spoS' => $this->getSpoStandartQuery()], 'spoS.tblId = tbl.id')
->leftJoin(['spoA' => $this->getSpoAverageQuery()], 'spoA.tblId = tbl.id')
->where(['in', 'col.id', $colIds])
->andWhere(
[
'OR',
[
'AND',
['=', 'col.is_free', TblCol::IS_FREE],
['>=', 'd.data', 0],
],
['>', 'd.data', 0],
]
)
->andWhere(
[
'OR',
['tbl.kf' => $this->adults], // убираем таблицы с коэффициентом не для расчета
['tbl.kf' => null],
]
)
->andWhere(
[
'OR',
[
'AND',
['>=', 'row.date_to', $this->dateFrom],
['<=', 'row.date_from', $this->dateTo],
],
[
'AND',
['>=', 'row.date_from', $this->dateFrom],
['<=', 'row.date_to', $this->dateTo],
],
]
)
->andWhere(['obj.raschet_online' => 1])
->groupBy('row.tbl_id')
->having(['>=', 'date_max', $this->dateTo])
->andHaving(['<=', 'date_min', $this->dateFrom])
->andFilterWhere(['room.object_id' => $this->objId])
->andFilterWhere(['room.id' => $this->roomId])
->andFilterWhere(['tbl.id' => $this->tblId])
->byMinMaxPeriod($this->totalDays)
->published();
//print_r($query->createCommand()->rawSql);
$this->setUnion($query);
<?php
$js = <<< JS
alert(123);
JS;
$this->registerJs( $js, $position = yii\web\View::POS_READY, $key = null );
?>