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();
$workCats = WorkCat::find()->with(['workNames' => function(ActiveQuery $q){
return $q->with(['workPrices']);
}])->all();
foreach ($workCats as $workCat){
echo '<h2>'.$workCat->name.'</h2>';
foreach ($workCat->workNames as $workName) {
foreach ($workName->workPrices as $workPrice) {
echo '<p>'.$workName->name.' ['.$workPrice->amount.']</p>';
}
}
}
echo form->field($model, '['.$workCat->id.']['.$workName->id.'][]')->textInput();
class AuthorController extends Controller {
public function actionIndex() {
$authors = Author::find()->select(['author.id', 'author.name', 'books' => 'count(*)'])
->leftJoin('book', 'author.id = book.author_id')
->groupBy('author.id')->asArray()->all();
return $this->render('index', ['authors' => $authors]);
}
}
foreach($authors as $author){
echo '<h2>'.$author['id'].') '.$author['name'].'</h2>'; // 1) Иван И.И.
echo '<p>Книг: '.$author['books'].'</p>'; // Книг: 17
}
class MyActiveController extends yii\rest\ActiveController {
public function actions(){
return ArrayHelper::merge(parent::actions(), [
'index' => [
'class' => 'common\components\IndexAction'
]
]);
}
}
class IndexAction extends yii\rest\IndexAction {
/**
* Prepares the data provider that should return the requested collection of the models.
* @return ActiveDataProvider
*/
protected function prepareDataProvider()
{
$requestParams = Yii::$app->getRequest()->getBodyParams();
if (empty($requestParams)) {
$requestParams = Yii::$app->getRequest()->getQueryParams();
}
$filter = null;
if ($this->dataFilter !== null) {
$this->dataFilter = Yii::createObject($this->dataFilter);
if ($this->dataFilter->load($requestParams)) {
$filter = $this->dataFilter->build();
if ($filter === false) {
return $this->dataFilter;
}
}
}
if ($this->prepareDataProvider !== null) {
return call_user_func($this->prepareDataProvider, $this, $filter);
}
/* @var $modelClass \yii\db\BaseActiveRecord */
$modelClass = $this->modelClass;
// чтобы переделать это
//$query = $modelClass::find();
$query = $modelClass::find()->where(['user_id' => Yii::$app->user->id]);
if (!empty($filter)) {
$query->andWhere($filter);
}
return Yii::createObject([
'class' => ActiveDataProvider::className(),
'query' => $query,
'pagination' => [
'params' => $requestParams,
],
'sort' => [
'params' => $requestParams,
],
]);
}
}
$token = Yii::$app->security->hashData(time().'|'.$id, 'secret-key');
$data = Yii::$app->security->validateData($token, 'secret-key');
try {
list($time, $id) = explode('|', $data);
$model = MyModel::findOne($id);
if (null === $model || time() > $time + 2 * 3600) {
throw new Exception();
}
}
catch (Exception $e) {
echo 'Token invalid';
}
RewriteRule ^.htaccess$ - [F]
RewriteRule ^favicon.ico$ _img/favicon.ico [L]
RewriteRule ^[a-z]{2}/files/.*$ index.php [L]
RewriteRule ^_img/number\.png.*$ index.php [L]
RewriteCond %{REQUEST_URI} !\.txt$ [NC]
RewriteRule ^.*$ index.php [L]
RewriteCond %{REQUEST_URI} !\.(html|txt)$ [NC]
$model->relatedRecords
, если "все возможные связи" - то, возможно, вам нужно рассмотреть возможность именования названия всех связей так:public function getRelatedClient(){}
public function getRelatedUser(){}
public function getRelatedBooks(){}
$model->relatedClient;
$model->relatedUser;
$model->relatedBooks;
getRelated
.class Social extends ActiveRecord {
public function getPivotSocialUser(){
return $this->hasMany(SocialUser::class, ['social_id' => 'id');
}
public function getUsers(){
return $this->hasMany(User::class, ['id' => 'user_id')->via('pivotSocialUser');
}
}
class User extends ActiveRecord {
public function getPivotSocialUser(){
return $this->hasMany(SocialUser::class, ['user_id' => 'id');
}
public function getSocials(){
return $this->hasMany(Social::class, ['id' => 'social_id')->via('pivotSocialUser');
}
}
$social = Social::findOne(['name' => 'facebook']);
var_dump($social->users); // все пользователи фейсбука
$user = User::findOne(['name' => 'admin']);
var_dump($user->socials); // все социальные сети админа