kikwik/doctrine-relation-count-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kikwik/doctrine-relation-count-bundle

最新稳定版本:v1.0.2

Composer 安装命令:

composer require kikwik/doctrine-relation-count-bundle

包简介

Manage counter as database property for doctrine relations

README 文档

README

Manage counter as database property for doctrine relations

Installation

  1. require the bundle
#!/bin/bash
composer require kikwik/doctrine-relation-count-bundle

Usage

  • Add an integer field in the related classes wich will contain the count value, setter is not required.
#[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;
    }
}
  • Add the #[CountableEntity] attribute on top of your child class
  • Add the #[CountableRelation] attribute on the ManyToOne or ManyToMany relations which you would count
  • Set the targetProperty parameter to the count property in the related class
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;
}

Custom updater

If you need to use a customized query for the counter update you can define a updateCountableRelation method in your child repository

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();
    }
}

统计信息

  • 总下载量: 175
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-28