Do you have any examples of these setters and getters?
Certainly
You don't need a setter in your entities, unless you need to add an option. For instance, consider the following structure definition:
PHP:
$structure->columns = [
'license_id' => ['type' => self::UINT, 'autoIncrement' => true, 'nullable' => true, 'changeLog' => false],
'product_id' => ['type' => self::UINT, 'required' => true, 'default' => 0,
'verify' => 'verifyProduct'
],
'user_id' => ['type' => self::UINT, 'required' => true],
'purchase_date' => ['type' => self::UINT, 'default' => \XF::$time],
'expiry_date' => ['type' => self::UINT, 'default' => \XF::$time],
'license_key' => ['type' => self::STR, 'readOnly' => true],
'custom_fields' => ['type' => self::SERIALIZED_ARRAY, 'default' => [], 'changeLog' => 'customFields'],
];
Notice
'license_key' => ['type' => self::STR, 'readOnly' => true],
- this means if you try to do
$entity->license_key = 'X';
you'll get an error message.
However, you can do this:
$entity->set('license_key', 'X', ['forceSet' => true]);
to override the readOnly property.
As for getters, the following is taken from
\XF\Entity\User
:
PHP:
public function getAvatarType()
{
if ($this->gravatar && $this->app()->options()->gravatarEnable)
{
return 'gravatar';
}
else if ($this->avatar_date)
{
return 'custom';
}
else
{
return 'default';
}
}
In XF1, you might have had a block of code that set
$user['avatar_type']
whenever the user object was fetched, but in XF2 you use a getter like this.
Furthermore, you can explicitly define getters to refer to functions instead of column values. For instance, if you have this structure:
PHP:
$structure->getters = [
'is_super_admin' => true
];
Then the corresponding getter would be:
PHP:
/**
* @return bool
*/
public function getIsSuperAdmin()
{
if ($this->Admin)
{
return $this->Admin->is_super_admin;
}
return false;
}
As you will have noticed from the function name, it takes the
$structure->getters
array key and turns it into CamelCase, and puts "get" in front of it. is_super_admin -> getIsSuperAdmin, avatar_type -> getAvatarType, etc.
The benefit of doing this is that every time you call
$entity->is_super_admin
, the
getIsSuperAdmin
function is actually called instead, which is what I assume you will want when it comes to adding new columns.
In short: Take the new array keys you wanted to add to the array (which is now an Entity), define them as getters in your entity structure, and do whatever calculations you normally did in-line in the getter function instead.
Hope that made sense, it is past midnight here
Fillip