Answer the question
In order to leave comments, you need to log in
How to merge a sequence of tables in yii2?
Hello!
I started to study yii2, according to the lesson I created a category tree in a component in which an associative array is formed.
The problem is that the urls for the categories are in a separate table.
The call itself in the component:
$this->data = Category::find()->indexBy('id')->asArray()->all();
class Category extends ActiveRecord
{
public static function tableName()
{
return 'categories';
}
public function getProducts()
{
return $this->hasMany(Product::className(), ['category_id' => 'id']);
}
}
class Product extends ActiveRecord
{
public static function tableName()
{
return 'products';
}
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
}
class CategoriesUrl extends ActiveRecord
{
public static function tableName()
{
return 'categories_url';
}
}
[А3] => Array
(
[id] => А3
[name] => DVD и CD диски
[description] =>
[meta_keywords] =>
[meta_description] =>
[parent_id] =>
[enabled] => 1
[updated] => 2017-07-31 00:07:59
[categoriesUrl] => Array
(
[category_id] => А3
[url] => a3
[url_title] => dvd-i-cd-diski
[new] => 0
[description] =>
[meta_keywords] =>
[meta_description] =>
[product_name_template] => %name% - %sku%
)
)
$this->data = Category::find()->indexBy('id')->joinWith('categoriesUrl')->asArray()->all();
public function getCategoriesUrl()
{
return $this->hasOne(CategoriesUrl::className(), ['category_id' => 'id']);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question