M
M
Maxim2014-10-18 21:44:52
Yii
Maxim, 2014-10-18 21:44:52

How to get value from pivot table (MANY_MANY) in ActiveRecord Yii model?

The situation is this, there are 2 tables, they are treated as MANY_MANY, but in the intermediate table there are 3 more fields (value) (For example, as here http://www.yiiframework.com/tutorial/image?type=gu... but in the table tbl_post_category there is also a value field)
The problem is that if 2 AR models are related as MANY_MANY, then when receiving data from related tables, this field does not appear in the output (find, findAll, etc.).
Now it works like this:
Let's say there are 2 AR models Posts and Fields, in the adjacent table (posts_fields) the post_id|field_id|value field
in the Posts model made a getter

private $fields;
public function getFields() {
//... вытаскиваем из базы необходимые поля
$this->fields = $result;
return $this->fields;
}

It works, but it works like lazy loading, and I would like to get the result as with eager loading (for example, as with Posts::model()->with('fields')->findAll(); we get everything in 1 object at once related fields).
Is there a prettier solution?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vladlen Grachev, 2014-10-18
@gwer

Maybe I'm wrong somewhere. But it seems there should be no problem if MANY_MANY is interpreted as BELONGS_TO + HAS_MANY to get. Well, for eager loading, use the together=>true parameter.

G
GreatRash, 2016-11-23
@GreatRash

You do not understand what "event delegation" is, which is why you do not understand why, for example, click can be delegated, but submit cannot.
Fill in the gaps.

V
Vladimir, 2016-11-23
@Casufi

You need to read how exactly the on method sets events.
I doubt that the submit event will fire on anything other than the form, although the specification says that it pops up.
www.w3schools.com/jsref/event_onsubmit.asp

A
Anton Bobylev, 2016-11-23
@dpigo

Try to handle submit on the form itself. If it works, then most likely the problem is either in the scope (it is impossible to find the specified selector in the document), or in the fact that the event inside the form is not an event on document and, accordingly, is not processed.

K
Kuzmina Maria, 2016-11-23
@orriole

Wrapping the script in $(document).ready() doesn't help? Your example works for me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question