[[+content_image]]
O
O
Oleg Lysenko2016-04-27 10:16:49
Yii
Oleg Lysenko, 2016-04-27 10:16:49

Can this method be rewritten from Yii1 to Yi2?

Hello, I need help. I need to rewrite a method from Yii1 to Yii2.
The essence is this: the method accepts an array of values, for example

[["name" => "attr_date_end", "jointype" => "left join"]]

Here is the implementation of the method
foreach ($attributes as $item) {

$criteria = new CDbCriteria;

criteria->select = (!is_array($this->getDbCriteria()->select) ? $this->getDbCriteria()->select : '') . ", " . $item['name'] . "." .$item['select'] . " as " . $item['name'];

$criteria->join = " " . $item['jointype'] . " cms_attributes_values " . $item['name'] . " on " . $item['name'] . ".id_obj=t.id AND " . $item['name'] . ".id_attr=" . $item['id_attr'] . " AND " . $item['name'] . ".id_tree=" . $item['id_tree'] . ($item['condition'] ? ' AND ' . str_replace("[id_variant]", $item['name'] . ".id_variant", str_replace("[id_val]", $item['name'] . ".id_val", str_replace("[text]", $item['name'] . ".text", $item['condition']))) : '');

$this->getDbCriteria()->mergeWith($criteria);
}

   return $this;

It is necessary to implement the same, but on Yii2, while doing this:
$query = new Query();

$query->select = (!is_array($query->select) ? $query->select : '') . ", " . $item['name'] . "." . $item['select'] . " as " . $item['name'];

$query->join = " " . $item['jointype'] . " cms_attributes_values " . $item['name'] . " on " . $item['name'] . ".id_obj=t.id AND " . $item['name'] . ".id_attr=" . $item['id_attr'] . " AND " . $item['name'] . ".id_tree=" . $item['id_tree'] . ($item['condition'] ? ' AND ' . str_replace("[id_variant]", $item['name'] . ".id_variant", str_replace("[id_val]", $item['name'] . ".id_val", str_replace("[text]", $item['name'] . ".text", $item['condition']))) : '');

But createCommand() throws an error Invalid argument supplied for foreach()
If anyone can, please help

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2016-04-27
@oleglysenko

"nvalid argument supplied for foreach()" means that your $attributes is not an array. You are looking for the error in the wrong place.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question