Add permission set lazy loading to Profile Field model

This commit is contained in:
Hypolite Petovan 2020-01-13 22:22:02 -05:00
parent d475cb5028
commit d11a0d3814
3 changed files with 59 additions and 4 deletions

View file

@ -6,5 +6,12 @@ use Friendica\BaseCollection;
class ProfileFields extends BaseCollection
{
/**
* @param callable $callback
* @return ProfileFields
*/
public function map(callable $callback)
{
return parent::map($callback);
}
}

View file

@ -3,6 +3,9 @@
namespace Friendica\Model;
use Friendica\BaseModel;
use Friendica\Database\Database;
use Friendica\Network\HTTPException;
use Psr\Log\LoggerInterface;
/**
* Custom profile field model class.
@ -17,8 +20,40 @@ use Friendica\BaseModel;
* @property string value
* @property string created
* @property string edited
* @property PermissionSet permissionset
*/
class ProfileField extends BaseModel
{
/** @var PermissionSet */
private $permissionset;
/** @var \Friendica\Repository\PermissionSet */
private $permissionSetRepository;
public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\PermissionSet $permissionSetRepository, array $data = [])
{
parent::__construct($dba, $logger, $data);
$this->permissionSetRepository = $permissionSetRepository;
}
public function __get($name)
{
$this->checkValid();
switch ($name) {
case 'permissionset':
$this->permissionset =
$this->permissionset ??
$this->permissionSetRepository->selectFirst(['id' => $this->psid, 'uid' => $this->uid]);
$return = $this->permissionset;
break;
default:
$return = parent::__get($name);
break;
}
return $return;
}
}

View file

@ -5,9 +5,10 @@ namespace Friendica\Repository;
use Friendica\BaseModel;
use Friendica\BaseRepository;
use Friendica\Collection;
use Friendica\Database\Database;
use Friendica\Model;
use Friendica\Model\PermissionSet;
use Friendica\Util\DateTimeFormat;
use Psr\Log\LoggerInterface;
class ProfileField extends BaseRepository
{
@ -17,13 +18,23 @@ class ProfileField extends BaseRepository
protected static $collection_class = Collection\ProfileFields::class;
/** @var PermissionSet */
private $permissionSet;
public function __construct(Database $dba, LoggerInterface $logger, PermissionSet $permissionSet)
{
parent::__construct($dba, $logger);
$this->permissionSet = $permissionSet;
}
/**
* @param array $data
* @return Model\ProfileField
*/
protected function create(array $data)
{
return new Model\ProfileField($this->dba, $this->logger, $data);
return new Model\ProfileField($this->dba, $this->logger, $this->permissionSet, $data);
}
/**
@ -84,7 +95,9 @@ class ProfileField extends BaseRepository
*/
public function selectByContactId(int $cid, int $uid)
{
$psids = PermissionSet::get($uid, $cid);
$permissionSets = $this->permissionSet->selectByContactId($cid, $uid);
$psids = $permissionSets->column('id');
// Includes public custom fields
$psids[] = 0;