XF 2.0 How to call an entity's property?

Matt C.

Well-known member
How would you go about calling an entities property? If I have an entity called Field, and it has the property field_id, how would I go about calling that?

Thank you.
 
Oh well, if your Entity is literally called field it would be
$Field->getValue("field_id");

Well, it's actually GamerProfile, I was using Field as an example.

I've tried this $GamerProfile->getValue("gamerprofile_id"); and it still gives the same error.
 
:unsure: Two possibilities, either use the finder system to get an object of your entity oder construct an entity. How did you create your custom entity anyways?
 
Code:
protected function getGamerProfileRepo()
{
    return $this->repository('AH\GamerProfiles:GamerProfile');
}

Code:
public function getGamerProfiles()
{
    return $this->getGamerProfileRepo()->findGamerProfilesForList();
}

Using this in a separate function $gamerprofileId = $this->getGamerProfiles()->getValue("gamerprofile_id");

Do I need to use any other namespaces for getValue to work?
 
Last edited:
I just use:
Code:
$entity->property_id

That seems to work. Thank you.
PHP:
$finder = $this->findGamerProfileValues();
$finder
    ->with('User')
    ->with('GamerProfile')
    ->where('user_id', '=', $visitor)
    ->where('gamerprofile_id', '=', $gamerprofileId);

Would you happen to know why I get this error when I use ->where('gamerprofile_id', '=', $gamerprofileId);?

Code:
LogicException: Unknown relation gamerprofile_id accessed on xf_ah_gamerprofile in src/XF/Mvc/Entity/Finder.php at line 90
 
Last edited:
Code:
$finder = $this->findGamerProfileValues()
    ->with(['User','GamerProfile'])
    ->where('user_id', $visitor)
    ->where('GamerProfile.gamerprofile_id', $gamerprofileId)
    ->fetchOne();
 
Code:
$finder = $this->findGamerProfileValues()
    ->with(['User','GamerProfile'])
    ->where('user_id', $visitor)
    ->where('GamerProfile.gamerprofile_id', $gamerprofileId)
    ->fetchOne();

Thank you, but unfortunately, I still get the same error. I think I have a problem with my entity relation.
 
Then the issue is in your entity.

I actually don't think I explained it enough. I want to get the gamerprofile_id of each entity, which means I have to run it through a foreach loop.

So I have this in Account.php
PHP:
foreach($gamerprofiles as $gamerprofileId => $gamerprofile)
{
  $gamerprofile = $gamerprofile->toArray();
  $gamerprofileId = $gamerprofile['gamerprofile_id'];

  $gamerprofileValue = $this->getGamerProfileValueRepo()->getGamerProfileValuesForUser($userId, $gamerprofileId);
}

Here is the getGamerProfileValuesForUser function
Code:
public function getGamerProfileValuesForUser($userId, $gamerprofileId)
{
    $gamerprofileValues = $this->finder('AH\GamerProfiles:GamerProfileValue')
        ->with(['User','GamerProfile'])
        ->where([
            'user_id' => $userId,
            'gamerprofile_id' => $gamerprofileId
        ]
    )->fetch();

    foreach ($gamerprofileValues AS $gamerprofileValueId => $gamerprofileValue)
    {
       $gamerprofileValue = $gamerprofileValue->toArray();

    }

    return $gamerprofileValue;
}

I thought if I put the getGamerProfileValuesForUser inside foreach($gamerprofiles as $gamerprofileId => $gamerprofile), it would find the id for each gamer profile, but it's only returning the last one.

Any help would be appreciated.
 
Back
Top Bottom