Skip to content

Using Scribe without a Controller

William Tempest Wright Ferrer edited this page Oct 26, 2018 · 5 revisions

Introduction

Scribe does not need to be used exclusive inside a controller. All pieces of Scribe can be used independently from each other, it's just much easier to use them all together (controller to a repository, repository to an entity).

This section will include examples of using Scribe in custom ways.

Note

In this guide I just give the signatures for the methods you have to call on the entities/repos. It should be pretty obvious how to then call those methods -- ie calls like: $entity->init('create', $arrayHelper, ['default'], ['default'])

Use an Entity by itself:

init the entity.

In order to use a Scribe entity, you must first call init on the entity. When you call init you are telling the entity what mode it is in (create, read, update or delete). You also give it an ArrayHelper, as well as a path and a fallback path for its context, and whether or not you are forcing it to recalculate all of its configuration values or not.

The init method signature looks like this:

/**
     * @param string $mode
     * @param ArrayHelperContract|null $arrayHelper
     * @param array|null $path
     * @param array|null $fallBack
     * @param bool $force
     * @throws \RuntimeException
     */
    public function init(string $mode, ArrayHelperContract $arrayHelper = null, array $path = null, array $fallBack = null, bool $force = false): void;

Bind data to it.

you can then call bindAssociation and setField on the entity to bind associations to it, and set values on fields.

The signatures for these methods look like this:

/**
     * @param string $assignType
     * @param string $associationName
     * @param EntityContract $entityToBind
     * @param bool $force
     */
    public function bindAssociation(string $assignType = null, string $associationName, EntityContract $entityToBind =null, $force = false): void;
/**
     * @param string $fieldName
     * @param $value
     * @throws RuntimeException
     */
    public function setField(string $fieldName, $value): void;

Use a Repository by itself:

A much more reasonable way to use Scribe without a controller is just to interact with a repository.

Scribe repositories have convenience methods create, read, update and delete.

Init the repository.

In order to use a Scribe repository, you must first call init on the repository. You give it an ArrayHelper, as well as a path and a fallback path for its context and whether or not you are forcing it to recalculate all of its configuration values or not.

The method signature for init looks like this:

/**
     * Initializes the classes with the helpers and config context path information
     * @param ArrayHelperContract|null $arrayHelper
     * @param array|null $path
     * @param array|null $fallBack
     * @param bool $force
     * @throws \RuntimeException
     */
    public function init( ArrayHelperContract $arrayHelper = NULL, array $path=NULL, array $fallBack=NULL, bool $force = false):void

Convience methods

Now you can call the convenience methods on the repository for create, read, update, and delete. Here are the signatures for those methods.

/**
     * Handles a create operation
     * @param array $params
     * @param array $optionOverrides
     * @param array $frontEndOptions
     * @return array
     * @throws \Doctrine\ORM\ORMInvalidArgumentException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \InvalidArgumentException
     * @throws \RuntimeException
     * @throws \Doctrine\DBAL\ConnectionException
     * @throws Exception
     */
    public function create(array $params, array $optionOverrides = [], array $frontEndOptions=[]):array
/**
     * Handles an update operation
     * @param array $params
     * @param array $optionOverrides
     * @param array $frontEndOptions
     * @return array
     * @throws \Doctrine\ORM\ORMInvalidArgumentException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \InvalidArgumentException
     * @throws \RuntimeException
     * @throws \Doctrine\DBAL\ConnectionException
     * @throws Exception
     */
    public function update(array $params, array $optionOverrides = [], array $frontEndOptions=[]):array
/**
     * Handles an delete operation
     * @param array $params
     * @param array $optionOverrides
     * @param array $frontEndOptions
     * @return array
     * @throws \Doctrine\ORM\ORMInvalidArgumentException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \InvalidArgumentException
     * @throws \RuntimeException
     * @throws \Doctrine\DBAL\ConnectionException
     * @throws Exception
     */
    public function delete(array $params, array $optionOverrides = [], array $frontEndOptions=[]):array
/**
     * Handles a read operation
     * @param array $params
     * @param array $optionOverrides
     * @param array $frontEndOptions
     * @return array
     * @throws \RuntimeException
     * @throws \Doctrine\ORM\ORMException
     */
    public function read (array $params=[], array $frontEndOptions=[], array $optionOverrides = []):array

Example

In the skeleton, we use an event listener to change a person's passwords, if they used a password reset link.

The relivant piece of the code to this example is as follows:

$userRepo = $this->getEm()->getRepository(User::class);
$userRepo->init($entity->getArrayHelper(), ['admin'], ['admin']);
$userRepo->update(
	[
		[
			'id' => $user->getId(),
			'password' => $k['frontEndOptions']['password']
		],
	], ['flush' => false], ['simplifiedParams' => true]
);

You see here that we use the repo to update the password. Since this already running inside a Tempest Tools standard flow, it doesn't need to flush again (in fact we find that additional flushes can trigger bugs with Doctrine), so we pass a 'flush' => false option.

What is important to note here is that we call init on the repo with an arrayHelper that we have previously used for this requestion, we are setting the context to be "admin", with no fallback, and then we call the update method using simplified params, to change the password on the repo.

This will trigger the validation options stored on the entity to ensure that the password is propperly formated, and all other Scribe related functionality on the Entity and Repo.