U
U
ukoHka2017-09-29 11:09:37
Yii
ukoHka, 2017-09-29 11:09:37

How to return a custom relation of a model via ajax?

The user table has fields Last name First name Middle name
The model has the getInitials() function:

public function getInitials()
    {
        return $this->surname . ' ' . mb_substr($this->name, 0, 1, 'UTF-8') . '.' . mb_substr($this->patronymic, 0, 1, 'UTF-8') . '.';
    }

On the server, the model is easily passed between functions and $model->initials can be called everywhere, but when using ajax, this relationship disappears. Since this is not a database link, joinWith() does not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-09-29
@ukoHka

On the server, the model is easily passed between functions and $model->initials can be called everywhere, but when using ajax, this relationship disappears.

Ajax processing also happens on the server, and what does it mean disappears? the method of the class cannot disappear, you would have given the error that you have
And what does joinWith have to do with it if the getInitials method glues data from model fields? Most likely it's not that something is missing. Most likely you have a problem in something else, for example, the model is not instantiated ... In general, you need a more detailed description of what "disappears" means and the Ajax request processing code

M
Maxim Timofeev, 2017-09-29
@webinar

but when using ajax this relationship disappears

it doesn't go anywhere. Show an action that receives an ajax request
Link not found in your code. What does the database and joins have to do with it? This is a normal hetero. And regardless of ajax, it should work$model->initials

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question