Hello, please tell me how to set up the report in such a way that the relation mapping would work with the like operator, and not where, the thing is this:

CategoryModel:

public function getProducts() { return $this->hasMany(ProductModel::className(), ['category_id' => 'id']); } public function getBrands() { return $this->hasMany(BrandModel::className(), ['id' => 'brand_id'])->via('products'); } 

So we get a definite list of the BrandModel model, but, not quite complete, CategoryModel has data with the field path = 3.10, as well as a list in ProductModel in which the value is path = 3.10.13, and I need to do this like this:

CategoryModel:

 public function getProducts() { return $this->hasMany(ProductModel::className(), ['path' => 'path']); } 

And at the same time, I want to get not category_model.path = product_model.path, but category_model.path like product_model.path. Tell me please, is there a solution in this case? I would be very grateful for your attention.

The query should be: SELECT * FROM TABLE category JOIN TABLE product ON product.path LIKE '%' + category.path + '%'

  • so that the relation mapping would work with the operator like, and not where Do not dub warm with soft. WHERE is the sentence in the query. LIKE is an operator. - Akina
  • I understand, it was difficult to explain, in other words, how to build a relation in activerecords yii2 to perform such a query: SELECT * FROM TABLE a JOIN TABLE b ON b.column LIKE '%' + a.column + '%' - dogmar
  • I think you need to leave only the tag yii2 (well, maybe more php ) and now add this very addition to the description of the task. Meanwhile, delve into the parameters of hasMany and similar functions ... - Akina
  • thanks and did. - dogmar

1 answer 1

This connection will not work.

Here is the code that is responsible for adding the JOIN to the general query.