Activerecord, Activequery And Ml Db

Hello there.

Let me first tell you, that I do not have any experience with Yii2 or Yii1 so be little more patient :)

I do not get how the ActiveRecord must be used when we deal with multilingual DB. Up to now I do write

every single SQL by hand using joins, sub queries and whatever is needed and everything was just fine. However, now with ActiveRecords the

things get messy.

Let’s assume that I have following tables -


+----------------------------------+

| products                         |

|----------------------------------+

| id  int not null auto_increment  |

| active tinyint(0) default 0      |

| sku varchar(...)..               |

|.....                             |

+----------------------------------+


+----------------------------------+

| products_texts                   |

|----------------------------------+

| id  int not null auto_increment  |

| product_id FK to products        |  

| language_id FK to language tbl   |

| caption varchar(...)..           |

|.....                             |

+----------------------------------+

I have to make list of some products, for example some 20 items. If I write query for this, it would look like this one -


SELECT products.id, products.sku, products_texts.caption FROM products

  INNER JOIN products_texts ON products_texts.product_id = products.id AND language.id = products_texts.language_id

WHERE

	products.active = 1

LIMIT 20

However, when I use ActiveRecord, from what I learnt from the guide 2.0 I must do -


class Products extends \yii\db\ActiveRecord {

.....

    /**

     * @return \yii\db\ActiveQuery

     */

    public function getTexts()

    {

        return $this->hasOne(ProductsTexts::className(), ['product_id' => 'id'])->onCondition(['language_id' => 'bg']);

    }

.....

}

Thus will allow me to access multilingual texts via "$product->text" propertie.

I can do Product::find()->joinWith(‘texts’)… and continue to use the returned ActiveQuery, but generated SQL is useless.

It will make actual join, but result is useless - SELECT only select “products.*” without any option (or I can’t figure how) to select

any field from joined table.

So, finally, my question is - How should I use ActiveRecord for ML DB? Keep in mind that this single join is just for example. In the

reality there will be at least 4.

My main concern here is - do I will execute 21 queries (1 for table pages and 20 more for result texts) to fetch 20 products and their texts or I miss something essential with AR?

Thanks for your time.

Can’t you just use your same join with a SqlDataProvider?

http://www.yiiframework.com/doc-2.0/guide-data-providers.html#sql-data-provider

I can, but this is not a solution for me. If this is the one and only one way to make it - it doesn’t worth to use AR and other DB abstraction stuff at all.

I think you are missing something.

I’m no expert in Yii2 but I think if you can have the right results with join than you just have to use


$products = Product::find()->joinWith('texts');

$texts = $products->texts;

Hope it helps.

If you read my entry first post, you will know that I’m aware of this way and something is not right from my view point.

I’m good to use this for product view for example, but it doesn’t look good when you make list of products.

Put your debugger ON and show 20 products. Look how many queries was generated - at least 41.

As I said before, what will happens if I have 20 products, which has some properties in 4 additional tables? Does AR will execute 20 * 4 + 1 queries?

Is this the right way?