Miscellaneous - kulbakin/anahita GitHub Wiki

The page conains misceleneous topics before they are placed into dedicated pages

disableChain

ORM system in Anahita among other things tied to event system. Domain specific repository objects extended from AnDomainRepositoryDefault and domain behaviors extended from AnDomainBehaviorAbstract can define _beforeRepositoryFetch method which would apply each time database is queued to fetch corresponding domain entities. Usual utilization of the logic is to apply additional conditions, e.g. ComBaseDomainBehaviorEnableable. Such behavior allows not to specify query where rules for each database query, which is useful for access permission checks etc.

There are situations though when one have to fetch records ignoring any automatic where conditions, i.e. specify all query filtering rules explicitly. These are the cases when AnDomainQuery::disableChain() method comes into play.

The common routines to utilize disableChain functionality would be

$query = KService::get('repos:pages:page')->getQuery()->disableChain();

or equivalent shortened version

$query = KService::get('repos:pages:page')->getQuery(true);

write access restrictions for domain attributes

When domain entities are defined, attributes config parameter is used to define its properties. Among other things (mapped database table column, type etc.) write access restrictions can be specified with write parameter, e.g. ComPeopleDomainEntityPerson definition has userType attribute with write restriction:

'attributes' => array(
    // ...
    'userType' => array('column' => 'person_usertype', 'write' => 'protected'),
    // ...
)

This parameter is handy to restrict assignment to the fields with AnDomainEntityAbstract::setData() method used by _actionEdit and _actionAdd() defined in KControllerService controller which is often a parent class for component specific controllers thus making any data submitted via html form to be stored in domain entity. Data are stored only if corresponding attribute is defined and its write access is public. You must leave write access as public only for attributes you intend for end user to modify with forms (even if fields are not present on the form, a post request can be manipulated to add any number of additional parameters to be submitted to the server).

To assign values to fields with restricted write access, a second attribute can be passed to setData, e.g.

$person->setData(array(
    'component'        => 'com_people',
    'name'             => $user->name,
    'username'         => $user->username,
    'email'            => $user->email,
    'userType'         => $user->usertype,
    'registrationDate' => AnDomainAttribute::getInstance('date')->setDate($user->registerDate),
    'lastVisitDate'    => AnDomainAttribute::getInstance('date')->setDate($user->lastvisitDate),
    'language'         => $params->get('language'),
    'timezone'         => $params->get('timezone'),
    'enabled'          => ! $user->block,
), AnDomain::ACCESS_PROTECTED);

Fields with protected and private write access can be manipulated with set method as well, e.g.

$person->set('userType', 'Administrator');