xanweb/c5-entity

ConcreteCMS 实体

v2.0 2021-10-22 14:29 UTC

This package is auto-updated.

Last update: 2024-09-22 20:43:18 UTC


README

Latest Version on Packagist Software License

实体可复用类

安装

将库包含到您的 composer.json 文件中

composer require xanweb/c5-entity

使用

您可以通过扩展 EntityService 和 EntityObject 类来利用预定义的方法。

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="MyEntityTable")
 */
class MyEntity extends \Xanweb\C5\Entity\EntityObject {
    ...
} 

/**
 * @method MyEntity createEntity()
 * @method MyEntity create($data)
 * @method MyEntity getByID($id)
 * @method MyEntity[] getList()
 * 
 * or if your IDE supports generic type
 * @implements EntityService<MyEntity>
 */
class MyEntityService extends \Xanweb\C5\Entity\Service\EntityService {
    /**
     * {@inheritdoc}
     *
     * @see \Xanweb\C5\Entity\Service\EntityService::getEntityClass()
     */
    public function getEntityClass(): string
    {
        return MyEntity::class;
    }
}

使用 TimeStampableTrait 将创建日期和最后更新日期字段包含到您的实体中。
请注意,使用此 trait 需要 "HasLifecycleCallbacks" 注解。
查看https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/annotations-reference.html#annref_haslifecyclecallbacks

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="MyEntityTable")
 * @ORM\HasLifecycleCallbacks
 */
class MyEntity extends \Xanweb\C5\Entity\EntityObject {
    use \Xanweb\C5\Entity\Traits\TimeStampableTrait;
    ...
}