K
K
Kola2012-11-14 14:24:17
Yii
Kola, 2012-11-14 14:24:17

Condition block in the afterFind method

How can you implement different behavior of the afterFind method depending on a certain condition.

For example, when displaying a feed of materials, I do not need some data from other tables, which are obtained in the afterFind method, but on the page of the material itself, I do.

In other words, I need to create a block of conditions in the afterFind method. Only I will not think, for what to cling to a condition.

How can this be done correctly?

Now getting data looks like this: ModelClass::model()->findAll($criteria);

I tried to set class attribute values ​​before calling the findAll() method, but the findAll() method creates a new instance of the object for each found entry, so the attribute value is lost.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Kola, 2012-11-14
@LDZ

I did it with a getter.
For example, if I need a foo property, I created a getFoo() method and moved it to get the data that should be available in the foo property.
Now in the afterFind method there is no collection of data that is not needed in the feed

T
tnz, 2012-11-15
@tnz

You are doing something wrong, I think. If in some case you do not need data from related tables, do not select them. Not? And when necessary, take it to the scope and connect this scope in the desired controller before sampling.

E
Eugene, 2012-11-16
@yujin1st

you can call the hasRelated() method, and if some related data is loaded, then process it

M
Max Khartanovych, 2015-02-16
@maxkh

Try in ModelClass::model() - pass the script, based on the script it will be possible to figure out the condition. And also take out everything in separate methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question