承接 c33s/doctrine-extra 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

c33s/doctrine-extra

Composer 安装命令:

composer require c33s/doctrine-extra

包简介

A Collection of Traits which can be used to build a Doctrine Entity.

README 文档

README

A Collection of Traits which can be used to build a Doctrine Entity.

Example Entity Customer:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use C33s\Entity\Traits\Field;
use C33s\Entity\Traits\Behaviour;
use C33s\Entity\Embeddable\CustomerNumber;
use Gedmo\Timestampable\Traits\TimestampableEntity;

class Customer
{
    use Field\RequiresId;
    use Field\RequiresUniqueName;
    use Field\RequiresEmail;
    use Field\HasDescription;
    use Field\HasIsActive;
    use Behaviour\IsTimestampable;
    use Behaviour\IsPublishable;
    use TimestampableEntity;
}

Requirements

Minimum Php version is 7.1

Custom Types Abstract Classes

if you prefer to work with value objects you can easily define new doctrine orm types (like string) with the available abstract types. there are three abstract types you can use:

  • AbstractValueObjectJsonType for using with objects which can be serialized into arrays/json
  • AbstractValueObjectStringType used for simple string value objects like for a name
  • AbstractValueObjectTextType the same as AbstractValueObjectStringType but extends Doctrines TextType to use the correct database field for text.

if you want to create a custom type address to put into your Person entity

first create a value object for your address (which can of course contain other objects). it must implement the array interface so the data can be converted to an array.

src/Value/Address.php:


use C33s\Doctrine\Interfaces\ArrayInterface;

final class Address extends ArrayInterface
{
    private AddressLine $streetLine1;
    private AddressLine $streetLine2;
    private City $city;
    private Zip $zip;
    private Province $province;
    private CountryCode $country;
}

then create your custom type where the name is the name of the type and valueObject class is the class it should be mapped to.

src/ORM/Types/AddressType.php:

namespace App\ORM\Types;

use App\Value\Address;
use C33s\Doctrine\Types\AbstractValueObjectJsonType;

class AddressType extends AbstractValueObjectJsonType
{
    protected $name = 'address';
    protected $valueObjectClass = Address::class;
}

App\Entity\Person.php:

class Person
{
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="address")
     */
    private $address;
    
    public function getAddress(): Address
    {
        return $this->address;    
    }
      
}

config/../doctrine.yaml:

doctrine:
    dbal:
        types:
            address: App\ORM\Types\AddressType

Fields

Basic Definitions

  • Embeds... -> `@ORM\Embedded(class="...")
  • Has... -> @ORM\Column(type="...") & getProperty(): ?type & setProperty(?type $property)
  • Requires... -> @ORM\Column(type="...", nullable=false) & @Assert\NotBlank() & getProperty(): ?type & setProperty(type $property)
  • RequiresUnique... -> @ORM\Column(type="...", unique=true, nullable=false) & @Assert\NotBlank() & getProperty(): ?type & setProperty(type $property)

Requires Traits use

  • @var Type|null for ease of use with __construct($name=null) defaults
  • getProperty(): ?type because the function may be called before the property is set - for example in EasyAdminBundle when adding a new entry

Available Fields

  • HasDate
  • HasDescription
  • HasDisplayName
  • HasEmail
  • HasEmbeddedEmail
  • HasFaxNumber
  • HasFirstName
  • HasIsActive
  • HasIsEnabled
  • HasIsPublished
  • HasIsVisible
  • HasLastName
  • HasPhoneNumber
  • HasPostTitle
  • HasPreTitle
  • HasRank
  • HasRoles
  • HasSlugFromName
  • HasUserSerializer
  • RequiresDate
  • RequiresDescription
  • RequiresEmail
  • RequiresFaxNumber
  • RequiresFirstName
  • RequiresId
  • RequiresLastName
  • RequiresPassword
  • RequiresPhoneNumber
  • RequiresPostTitle
  • RequiresRank
  • RequiresUrl
  • RequiresUniqueName
  • RequiresUniqueUrl
  • RequiresUniqueUsername
  • EntityHasUploadableImage: found in src/Entity/Traits/Field/edition -> copy to src/Entity/Traits/Field/
  • Replace Trait name to reflect the name of your Entity (like ProductHasUploadableImage)
  • change namesapce
  • adjust mapping(currently entity_image)
     * @Vich\UploadableField(mapping="entity_image", fileNameProperty="imageName", size="imageSize")
    
  • create vich_uploader configuration for your mapping
    vich_uploader:
    mappings:
      entity_image:
    # see config/app/vich_uploader.yaml or config/edition/vich_uploader.yaml for examples
    

Behavior

  • IsBlameable
  • IsFeatureFlagable
  • IsPublishable
  • IsTimestampable

Method

  • HasDummyAdvancedUserInterface
  • HasDummySalt
  • HasParentAndChildren

General

  • UploadableTypeEmbeddedTrait

RequirePassword & User Entity & UserBundle

we need an entity called LoginUser to work with this lib out of the box. in the user bundle RequirePassword is used. The user entity should implement Serializable, EquatableInterface, UserInterface

RequiresPassword is a little bit tricky because of the requirement of hashed/crypted passwords. we have two fields password and plainPassword. the password field is the only mapped field. plainPassword is only used to set a plain password which will be auto-encoded using an event listener.

Overriding Metadata of Traits

this will override length and all @Assert. so in this case no @Assert\NotBlank from the Trait. this works out of the box without the

<?php


namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use C33s\Doctrine\Entity\Traits\Field;

/**
 * @ORM\Entity()
 * @codeCoverageIgnore
 */
class OverrideTest
{
    use Field\RequiresId;
    use Field\RequiresEmail;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=10, unique=true)
     * @Assert\Email()
     */
    protected $email;
}

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/override-field-association-mappings-in-subclasses.html https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html https://stackoverflow.com/questions/32571920/overriding-doctrine-trait-properties https://andy-carter.com/blog/overriding-extending-a-php-trait-method

c33s/doctrine-extra 适用场景与选型建议

c33s/doctrine-extra 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 183 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「doctrine」 「helper」 「uuid」 「traits」 「types」 「ValueObject」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 c33s/doctrine-extra 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 c33s/doctrine-extra 我们能提供哪些服务?
定制开发 / 二次开发

基于 c33s/doctrine-extra 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-09