// Вариант 1
$user->setCounter(clone $user->getCounter());
$user->raiseCount();
// User.php
public function raiseCount(): void
{
$this->counter = $this->counter->add();
}
// Counter.php
public function add(): self
{
$self = clone $this;
++$self->value;
return $self;
}
Arr:undot()
use Illuminate\Support\Arr;
$array = [
'user.name' => 'Kevin Malone',
'user.occupation' => 'Accountant',
];
$array = Arr::undot($array);
// ['user' => ['name' => 'Kevin Malone', 'occupation' => 'Accountant']]
В `symfony` есть mybuilder/cronos-bundle, но есть ли возможность,чтобы время исполнения задач брать из таблицы-сущности `cron`?
symfony/config
symfony/cache
doctrine/annotations
resetManager()
— вы убиваете и этот SQL Logger с его объектами в памяти$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
$this->client->request('GET', self::API_PREFIX . '/account/userinfo', [
'Authorization' => 'Bearer ' . $token,
]);
$this->client->request(
method: 'GET',
uri: self::API_PREFIX . '/account/userinfo',
server: ['HTTP_Authorization' => 'Bearer ' . $token,]
);
$qb->expr()->andX(
$qb->expr()->andX(
$qb->expr()->isInstanceOf('sub1', 'TargetClass'),
$qb->expr()->eq('sub1.sub1Field', $sub1FieldValue)
)
);
/**
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"cat" = "Cat", "dog" = "Dog"})
*
* @ORM\Entity(repositoryClass="AnimalRepository")
*/
class Animal
{
/**
* @ORM\Id
* @ORM\Column(type="integer", nullable=true)
* @ORM\GeneratedValue(strategy="AUTO")
*/
public ?string $id = null;
}
/** @ORM\Entity */
class Cat extends Animal
{
/**
* @ORM\Column(type="string")
*/
public $meow;
}
/** @ORM\Entity */
class Dog extends Animal
{
/**
* @ORM\Column(type="string")
*/
public $woof;
}
class AnimalRepository extends EntityRepository
{
/**
* @param array $filters
*
* @return \Generator<Animal::class>|Animal[]
*/
public function filter(array $filters): \Generator
{
foreach ($filters as $filter) {
[$entityClass, $conditions] = $filter;
if (!is_a($entityClass, Animal::class, true)) {
throw new \LogicException('Класс должен быть ребенком ' . Animal::class);
}
yield from $this->getSubEntities($entityClass, [$conditions]);
}
}
private function getSubEntities(string $subEntityClassName, array $conditions): \Generator
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('e')->from($subEntityClassName, 'e');
foreach ($conditions as $condition) {
[$param, $paramValue] = $condition;
$qb
->andWhere("e.$param = :$param")
->setParameter($param, $paramValue);
}
foreach ($qb->getQuery()->getResult() as $entity) {
yield $entity;
}
}
}
class AnimalRepositoryCest
{
/** @var EntityManagerInterface */
private $em;
public function _before(FunctionalTester $I): void
{
$this->em = $I->grabService('doctrine.orm.entity_manager');
}
public function testSuccessFilter(): void
{
$cat1 = new Cat();
$cat1->meow = 'meow1';
$cat2 = new Cat();
$cat2->meow = 'meow2';
$dog1 = new Dog();
$dog1->woof = 'woof1';
$dog2 = new Dog();
$dog2->woof = 'woof2';
$this->em->persist($cat1);
$this->em->persist($cat2);
$this->em->persist($dog1);
$this->em->persist($dog2);
$this->em->flush();
/** @var AnimalRepository $animalRepository */
$animalRepository = $this->em->getRepository(Animal::class);
assertInstanceOf(AnimalRepository::class, $animalRepository);
// этот код можно в репозитории сокрыть
$resultIter = $animalRepository->filter([
Cat::class, [['meow' => 'meow2']], // тут точка расширения гибкости добавления условий, чтобы все не только eq() было
Dog::class, [['woof' => 'woof1']],
]);
$result = iterator_to_array($resultIter);
assertCount(2, $result);
assertInstanceOf(Cat::class, $result[0]);
assertSame('meow2', $result[0]->meow);
assertInstanceOf(Dog::class, $result[1]);
assertSame('woof1', $result[1]->woof);
}
}
В итоге я получу вот такую бороду.
0: "ROLE_ADMIN"
1: "ROLE_OPERATOR"