How to get similar data?

For example, I need list of users.
In one case the list I need as an array [id] => name
In another, I need this list to start to add an empty element (for select)
Third, we need a list with the additional condition, say, all users with flag enable = 1
and so forth.
How did things to do?
To do 4 different functions? To do 1 function, but using a parameter to pass some sort of flag and functions through the case to return the set?
Thank you.
September 19th 19 at 13:31
2 answers
September 19th 19 at 13:33
It is better not to do so.
Initially, you need to do so that You don't have as many options list of users. Where in the code universality? I recommend to You to look in the direction of the PLO, to make the class one user class and a user list that inherits from a single user. And processor of users to learn to work only with 1 type of list.
Well, I have the PLO, in the controller I call the model behind the list of users for some specific. 1 type of list is that? All users? And in the controller this advanced crop that would bring in views?
I do not quite understand your idea. - hulda.Bashiri commented on September 19th 19 at 13:36
1) Here in this question:
"In one case, the list I need as an array [id] => name
In another, I need this list to start to add an empty element (to select)"
Why you need to give the list in a different form?

2) I like this view:

getUsers( $params = false )

If there are no parameters - get all users, if there is, then the parameters in the array-value, and collect from them the condition of the sample. ( Where key_1 = val_1 AND key_2 = val_2 )

The response function must always be "array of arrays" whatever it found users.
array( array('id'=>id, 'name'=>name).... array('id'=>id, 'name'=>name) ) - bailey.Okuneva commented on September 19th 19 at 13:39
Understood, thank you. - hulda.Bashiri commented on September 19th 19 at 13:42
Why you need to give the list in a different form?
For example I want in one case, to have select without blank item at the beginning, and the second empty. Select I have built using html::select() where I pass an array of elements. - hulda.Bashiri commented on September 19th 19 at 13:45
September 19th 19 at 13:35
For example, I need list of users.

GET api/users?field=name
In another, I need this list to start to add an empty element (for select)

This is generally to receive the data is not relevant, how you use the data is irrelevant to the API.
Third, we need a list with the additional condition, say, all users with flag enable = 1
and so forth.

GET api/users?field=name&enable=1

If you are talking about internal API is the same in General:
array_column(
$Users->get(
$Users->search()
),
'name',
'id'
)

array_column(
$Users->get(
 $Users->search(['enabled' => 1])
),
'name',
'id'
)

If parameters can be a small amount - you can just make each option a separate argument if the data is large - the filter which columns to return can also be put in a separate argument, but it is necessary to know what you have how and of what scale.
Most importantly make the operation atomic, then you can compose as needed.
Thank you. - hulda.Bashiri commented on September 19th 19 at 13:38
Little will clarify exactly how the data is get I understand my ORM and I just design the queries of the form: ... ->where('enable', '=', 1) and so forth, everything is simple. Another thing is that the whole I keep the whole sample in a method, say getUsers. And when data users need it, I just take them through $users->getUsers()
But this method returns users with certain conditions. But I need some of these conditions to adjust, to return and those who have 'active' = 1 and then I need either a new method or to pass additional parameters to an existing getUsers. So the question is. How can I do. I don't want the controllers to constantly write something like:
$users->getUsers(['enable'=>1,'active'=>1, and there are other options). Or maybe it's okay to do so and I worry in vain? - bailey.Okuneva commented on September 19th 19 at 13:41
: If the same type of queries perform too often - wrapped in an additional method, not to be repeated, and so it is normal, does not always do so very elegantly) - hulda.Bashiri commented on September 19th 19 at 13:44

Find more questions by tags PHP