1. Go to this page and download the library: Download efabrica/nette-repository library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
final class AdminScope implements \Efabrica\NetteRepository\Repository\Scope\Scope
{
public function apply(RepositoryBehaviors $behaviors, Repository $repository): void
{
// Remove these behaviors because they are not needed for the Admin
$behaviors
->remove(SoftDeleteBehavior::class)
->remove(PublishBehavior::class);
// Do not add any new behaviors here, because this scope can be used by different repositories
// and you might introduce unwanted side effects.
// However, you can conditionally add behaviors based on the repository type and some parameter
// For example, if you want to apply a special behavior for admin users in the user repository, you can do this:
if ($repository instanceof UserRepository && $someContainerParameter) { // Scopes can be services and receive parameters.
$behaviors->add(AdminBehavior::class); // This is a hypothetical behavior, just for illustration.
}
}
}
use Efabrica\NetteRepository\Repository\RepositoryBehaviors;
abstract class RepositoryBase extends \Efabrica\NetteRepository\Repository\Repository
{
/** @inject */
public AdminScope $adminScope;
public function scopeAdmin(): self
{
return $this->withScope($this->adminScope);
}
// Do this if you want to set the AdminScope as default:
protected function setup(RepositoryBehaviors $behaviors) : void
{
$behaviors->setScope($this->adminScope);
}
}
use YourBeautifulApplication\Admin\AdminScope;
abstract class QueryBase extends \Efabrica\NetteRepository\Repository\Query
{
public function scopeAdmin(): self
{
// This method returns a copy of the query with your own Admin scope applied
// The Admin scope removes some behaviors that are not relevant for the Admin
return $this->withScope($this->repository->adminScope);
// Alternatively, if the Admin scope does not depend on any parameters, you can create a new instance of it like this:
return $this->withScope(new AdminScope());
}
}
// all of these are equivalent:
$repository->findBy(['age > ?' => 18])->scopeAdmin()->fetchAll();
$repository->scopeAdmin()->findBy(['age > ?' => 18])->fetchAll();
$repository->query()->where('age > ?', 18)->scopeAdmin()->fetchAll();
$repository->scopeAdmin()->query()->where('age > ?', 18)->fetchAll();
$repository->query()->scopeAdmin()->where('age > ?', 18)->fetchAll();
sh
>$ php bin/console efabrica:nette-repo:code-gen
># OR
>$ php bin/console e:n:c
># OR
>$ php vendor/bin/enc
>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.