R
R
Roman2015-05-26 16:11:42
Yii
Roman, 2015-05-26 16:11:42

How can I set a script or define a page action in fields() (REST) ​​(Yii2)?

I need to somehow display different fields for different Actions (REST)

// explicitly list every field, best used when you want to make sure the changes
    // in your DB table or model attributes do not cause your field changes (to keep API backward compatibility).
    public function fields()
    {

        return [
            // field name is the same as the attribute name
            'user_id','name', 'email','gender','age','image'

        ];
    }

Now only these fields are allowed, I want to add or remove a field from this list for a specific Action.
How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
matperez, 2015-05-26
@matperez

You can try like this:

$action = Yii::$app->requestedAction; 
        if ($action->id === 'form' && $action->controller->id === 'default') {
            return [...];
        } else {
            return [...];
        }

It seems to me, architecturally, it is not quite correct that the model knows about which action it is called in.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question