PHP code example of kikwik / doctrine-relation-count-bundle
1. Go to this page and download the library: Download kikwik/doctrine-relation-count-bundle 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/ */
kikwik / doctrine-relation-count-bundle example snippets
#[ORM\Entity(repositoryClass: FamigliaRepository::class)]
class Famiglia
{
#[ORM\Column]
private int $numProdotti = 0;
public function getNumProdotti(): int
{
return $this->numProdotti;
}
}
#[ORM\Entity(repositoryClass: SimboloRepository::class)]
class Simbolo
{
#[ORM\Column]
private int $numProdotti = 0;
public function getNumProdotti(): int
{
return $this->numProdotti;
}
}
use Kikwik\DoctrineRelationCountBundle\Attribute\CountableEntity;
use Kikwik\DoctrineRelationCountBundle\Attribute\CountableRelation;
#[ORM\Entity(repositoryClass: ProdottoRepository::class)]
#[CountableEntity]
class Prodotto
{
#[ORM\ManyToOne(inversedBy: 'prodotti')]
#[CountableRelation(targetProperty: 'numProdotti')]
private ?Famiglia $famiglia = null;
#[ORM\ManyToMany(targetEntity: Simbolo::class, inversedBy: 'prodotti')]
#[CountableRelation(targetProperty: 'numProdotti')]
private Collection $simboli;
}
class ProdottoRepository extends ServiceEntityRepository
{
public function updateCountableRelation(object $localObject, string $relationName, object $relatedObject, string $relatedProperty)
{
$dql = sprintf('UPDATE %s related set related.%s = (SELECT COUNT(local.id) FROM %s local WHERE local.%s = :id AND local.isActive = 1) WHERE related.id = :id',
get_class($relatedObject),
$relatedProperty,
get_class($localObject),
$relationName,
);
$query = $this->getEntityManager()->createQuery($dql)
->setParameter('id', $relatedObject->getId());
$query->execute();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.