So, I have following php for wp:
$usersNames = array();
foreach ($userIDs as $userId) {
$userInfo = get_userdata($userId);
$usersNames[] = $userInfo->display_name; //this one
}
I am getting an error for $usersNames.
"PHP Notice: Trying to get property of non-object in /functions.php on line"
What is going on?
Any suggestions?
Thanks
EDIT:
So, for $userIDs, I have an array of user ids. Then I am trying to get user display name etc for individual ids.
Your function get_userdata() will return False on failure, WP_User object on success.
And error Trying to get property of non-object means that this function has returned false.
Simply check if $userInfo is not empty
$userInfo = get_userdata($userId);
if (!empty($userInfo)) {
$usersNames[] = $userInfo->display_name;
}
you need judge whether $userInfo return the right obj
$usersNames = array();
foreach ($userIDs as $userId) {
$userInfo = get_userdata($userId);
$usersNames[] = isset($userInfo->display_name)?$userInfo->display_name:''; //this one
}
Related
I have had this app developed for over two years, and nothing has changed on it. But all of a sudden this week, it started throwing out this error.
Trying to get property 'conversation_interlocutors' of non-object
at HandleExceptions->handleError('8', 'Trying to get property 'conversation_interlocutors' of non-object', '/home/admin/web/365onlinework.com/public_html/app/Services/MessageService.php', '47', array('collection' => object(WebsiteUser))) in MessageService.php line 47
So, the script in questions is here.
public function getConversations()
{
$websites = Website::orderBy('created_at', 'desc')->get();
$conversations = collect();
foreach ($websites as $website) {
if (auth()->user()->can('view', $website)) {
$this->tenant->connect($website);
$collections = $website->managed_users()
->with(['user.conversation_interlocutors' => function ($query) {
$query
->select('id', 'read', 'initiatorId', 'interlocutorId')
->where('read', 0)
->orWhere('read', 1)
->has('initiator')
->has('interlocutor')
->has('messages')
->with(['flagged', 'interlocutor.website',
'initiator' => function ($i) {
$i->select('id', 'username');
}])
->withCount('messages');
}])->get();
$filtered = $collections->filter(function ($collection) {
return $collection->user->conversation_interlocutors;
});
foreach ($filtered as $filter) {
$conversations->push($filter->user->conversation_interlocutors);
}
}
}
$conversations = $conversations->flatten()->sortBy('lastMessageTimestamp')->values();
return $conversations;
}
Line 47 is:
return $collection->user->conversation_interlocutors;
I tried to do an if isset to check if it's nulled and it goes through, but then it shows nothing on the output at all. Normally it shows the list of conversations.
As well we already do a check if it has a value while doing the query, this has been running for a long time already, and I am not sure what is causing the error.
This happens because $collection->user is null. To fix it use this code:
$collection->user->conversation_interlocutors ?? null;
or
!empty($collection->user->conversation_interlocutors)
? $collection->user->conversation_interlocutors
: null;
I, using the Graphaware Neo4j-php-OGM. I would like to access the 2nd level relationship. I can't seen to get it to work. What am i doing wrong?
I'm trying to execute the following:
public function allowToContinue($userUuid, $permissionUuid)
{
$userRepo = $this->entityManager->getRepository(User::class);
$permRoleRepo = $this->entityManager->getRepository(PermRole::class);
$permissionRepo = $this->entityManager->getRepository(Permission::class);
$user = $userRepo->findOneBy(['uuid' => $userUuid]);
$permission = $permissionRepo->findOneBy(['uuid' => $permissionUuid]);
$allowed = false;
foreach ($user->getPermrole() as $userRole)
{
var_dump($userRole);
$role = $permRoleRepo->findOneBy(['uuid' => $userRole->getUuid()]);
var_dump($role);
foreach ($role->getPermissions() as $perm)
{
var_dump($perm->getUuid());
if($perm->getUuid() === $permissionUuid){
$allowed = true;
}
}
}
return $allowed;
}
Stacktrace:
Error:
Call to a member function isCollection() on null
at vendor/graphaware/neo4j-php-ogm/src/Hydrator/EntityHydrator.php:107
at GraphAware\Neo4j\OGM\Hydrator\EntityHydrator->hydrateSimpleRelationshipCollection('permissions', object(Result), object(neo4j_ogm_proxy_App_Entity_Generic_PermRole))
(vendor/graphaware/neo4j-php-ogm/src/Persisters/BasicEntityPersister.php:104)
at GraphAware\Neo4j\OGM\Persisters\BasicEntityPersister->getSimpleRelationshipCollection('permissions', object(neo4j_ogm_proxy_App_Entity_Generic_PermRole))
(vendor/graphaware/neo4j-php-ogm/src/Proxy/NodeCollectionInitializer.php:22)
at GraphAware\Neo4j\OGM\Proxy\NodeCollectionInitializer->initialize(object(Node), object(neo4j_ogm_proxy_App_Entity_Generic_PermRole))
(vendor/graphaware/neo4j-php-ogm/src/Proxy/LazyCollection.php:52)
at GraphAware\Neo4j\OGM\Proxy\LazyCollection->doInitialize()
(vendor/doctrine/collections/lib/Doctrine/Common/Collections/AbstractLazyCollection.php:332)
at Doctrine\Common\Collections\AbstractLazyCollection->initialize()
(vendor/doctrine/collections/lib/Doctrine/Common/Collections/AbstractLazyCollection.php:274)
at Doctrine\Common\Collections\AbstractLazyCollection->getIterator()
(src/Security/RoleChecker.php:45)
at App\Security\RoleChecker->allowToContinue('8d88d920-5ab0-11e8-a371-001c42dff143', 'd93370b0-585d-11e8-a371-001c42dff143')
(src/Controller/Generic/UserController.php:146)
at App\Controller\Generic\UserController->destroy('c34f1380-5ab5-11e8-a371-001c42dff143')
(vendor/symfony/http-kernel/HttpKernel.php:149)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:66)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:188)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:37)
It throws the error on the 2nd foreach loop on line:
foreach ($role->getPermissions() as $perm)
It's strange, working the first time correctly and not the 2nd time. Also after fetching the object again to be sure. Without this it throws the exact same notice.
Thanks in advance!
All code is at github: https://github.com/djkevino/Support4Neo
Honestly, pretty confused by this code. The var_dump() die() stuff in destroy()... Also, took me a while to realize we are talking about code in the library, not your code that interacts with this library.
Can you avoid the issue like so?
//add this next line
if (!is_iterable($user->getPermrole())) continue;
foreach ($role->getPermissions() as $perm)
If you are not on PHP version 7.1 or greater you could simply check if the returned value is_null or test for instance of \Traversable and is_array...
is_array($foo) || (is_object($foo) && ($foo instanceof \Traversable ));
As stated in my comment, not sure if that role having no "permissions" is considered a valid state or not.
It looks like $role->getPermissions() can return a null result. So, make sure the result is not null before executing the inner foreach loop.
Since you seem to be testing if any of the user's roles has the desired permission, you do not need the $allowed variable at all. The inner foreach loop can just immediately return true once a match is found, avoiding unnecessary processing.
That is, try this:
public function allowToContinue($userUuid, $permissionUuid) {
$userRepo = $this->entityManager->getRepository(User::class);
$permRoleRepo = $this->entityManager->getRepository(PermRole::class);
$permissionRepo = $this->entityManager->getRepository(Permission::class);
$user = $userRepo->findOneBy(['uuid' => $userUuid]);
$permission = $permissionRepo->findOneBy(['uuid' => $permissionUuid]);
foreach ($user->getPermrole() as $userRole) {
var_dump($userRole);
$role = $permRoleRepo->findOneBy(['uuid' => $userRole->getUuid()]);
var_dump($role);
$rolePerms = $role->getPermissions();
if (!is_null($rolePerms)) {
foreach ($rolePerms as $perm) {
var_dump($perm->getUuid());
if ($perm->getUuid() === $permissionUuid) {
return true;
}
}
}
}
return false;
}
I have the following codes.
<?php
class Reg {
private $pros = array();
public function __set($key,$val) {
$this->pros($key)= $val;
}
public function __get($key) {
return $this->pros($key);
}
}
$reg= new Reg;
$reg->tst="tst";
echo $reg->tst;
?>
But when executing this script I got the error following.
Fatal error : can't use method return value in write context in line 5
I believe that to add an element to array is possible like the above.
$array = array();
$array('key')='value';
Please make clear that I was wrong.
Thanks
The is because of you are trying to set a functions return value. $this->pros($key) means calling pros($key) function. Not setting a value to $pros array.
The syntax is wrong. Setting values to array be -
$array['index'] = 'value';
Change
$this->pros($key)= $val; -> $this->pros[$key]= $val;
and
return $this->pros[$key];
Working code
$this->pros[$key] = $value;
OR
$keys = array($key);
$this->pros = array_fill_keys($keys,$value);
The array_fill_keys() function fills an array with values, specifying keys.
Syntax:
array_fill_keys(keys,value);
i got a php function in Wordpress that get serialized user meta : like this
function get_allowwed_tournees_ids(){
$tournees = get_user_meta($this->ID, 'tournees',true);
$tournees = unserialize($tournees);
$tournees_ids = array();
foreach ($tournees as $key => $value) {
array_push($tournees_ids, $key);
}
var_dump($tournees_ids);
return $tournee_ids;
}
get_allowwed_tournees_ids() is in a class that extends WP_User
and when i want to call it :
$id_tournees = $current_user->get_allowwed_tournees_ids();
var_dump($id_tournees);
the var_dump inside the function returns me the unserialised array, and the second var_dump outside the function returns null.
Any idea ?? Thanks !
Because you are returning $tournee_ids which is never defined. I think you should
return $tournees_ids;
I'm trying to find out why I'm getting a Trying to get property of non-object. I'm not completely skilled with objects and arrays but i'm trying. Here's the code and the error message. Any ideas on how to fix this issue?
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/sitemodel.php
Line Number: 208 and 215
function getSubMenuPages()
{
$this->db->select('site_menu_structures.id');
$this->db->from('site_menu_structures');
$this->db->where('site_menu_structures.short_name', 'mainnav');
$query = $this->db->get();
$menu_id = $query->row()->id;
$this->db->select('site_menu_structures_links.id, site_menu_structures_links.short_name, is_category');
$this->db->from('site_menu_structures_links');
$this->db->where('site_menu_structures_links.menu_structure_id', $menu_id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray = $query->result();
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$linksArray->{$key}->child_links;
$this->db->select('site_menu_structures_links_children.link_name');
$this->db->from('site_menu_structures_links_children');
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray->{$key}->child_links = $query->result();
}
}
}
}
return $linksArray;
}
My guess would be $linksArray is an array, not an object so the line
$linksArray->{$key}->child_links;
will not work. In any case, this line does nothing so why have it at all?
Where you assign a value to this "property", try this instead
$linksArray[$key]->child_links = $query->result();
"Trying to get property of non-object"
this type of errors only exist if you try to treat a variable as an object instance and you actually failed to create that instance successfully try to check this part of the code if this is really an object or not:
$linksArray->{$key}->child_links
this is on the bottom of your code.