Since the $clients
variable is an array that contains a list of all the clients that have accessed that page, as you're getting the collection here:
$clients = UCRMAPIAccess::doRequest('clients') ?: [];
And I believe you're trying to get the one client that just accessed the page, since you said:
"when I click on Contract page I want to get my client information, not of all clients"
Then I believe you can filter the $clients
array for a single client that matches an id, ONLY if you know the id of the client you're looking for.
For example:
$clients = UCRMAPIAccess::doRequest('clients') ?: [];
$you = (object) array('firstName' => 'foo', 'lastName' => 'bar', 'id' => 1);
$meInClients = array_filter($clients, function($k) {
// filter by key
}, ARRAY_FILTER_USE_KEY)
$clients[meInClients]
But in this case it seems your program doesn't know what user to look for, so a filter or search might not work here. What you want to do is get the last user who JUST visited that page, naturally that user would be the last in the array.
So:
$clients = UCRMAPIAccess::doRequest('clients') ?: [];
$whoJustVisitedPage = $clients[count($clients) - 1]
So this might work.
UPDATE:
When you access the page as John Doe and go to that page, ( the $clientsWhoVisitedPage array now becomes $clientsWhoVisitedPage + YOU (currently authenticated user a.k.a the last person who viewed the page) What you can do in this case is first to filter the $client array so it doesn't contain the currently authenticated user. Then get the last user in the new array.
Like so:
$me = $security->getUser();
$clientsExceptMe = array_filter($clients, function($client, $x) {
return $client->id != $me->id;
}, ARRAY_FILTER_USE_BOTH));
$lastPageVisitor = $clientsExceptMe[count($clientsExceptMe) - 1]
lastPageVisitor
should now give you the last person that visited that page that isn't the currently authenticated user.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…