ev/ev-copy-bundle

这是一个Symfony Bundle,可以帮助您复制实体

安装数: 11,215

依赖项: 0

建议者: 0

安全: 0

星标: 5

关注者: 3

分支: 1

开放问题: 0

类型:symfony-bundle

v1.4 2024-01-12 13:47 UTC

This package is auto-updated.

Last update: 2024-09-12 15:29:11 UTC


README

这是一个Symfony Bundle,可以帮助您复制具有其依赖关系的实体

功能

  • 轻松配置实体的复制行为

安装

在composer.json文件中,添加

{
    "require": {
        "ev/ev-copy-bundle": "^1.2"
    }
}

在app/AppKernel.php文件中,添加

public function registerBundles()
{
    return array(
        // ...
        new EV\CopyBundle\EVCopyBundle(),
        // ...
    );
}

实体配置

注解

  • @Copy\Simple : 取值并添加到复制中

  • @Copy\Variable : 根据传递给Cloner的参数设置值

    必需属性

    • name
  • @Copy\Collection : 复制集合中的每个实体

    可选属性

    • memorizeMatching : 启用匹配记忆并定义记忆包的名称
  • @Copy\Entity : 复制实体

  • @Copy\UseMatching : 使用之前复制的实体。它使用匹配记忆系统

    必需属性

    • name : 记忆包的名称
  • @Copy\Construct : 根据传递给Cloner的参数向构造函数提供参数

    必需属性

    • variables : 参数数组

示例

namespace EV\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use EV\CopyBundle\Annotation as Copy;

class Article
{

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(name="title", type="string", length=255)
     * @Copy\Variable(name="articleTitle")
     */
    private $title;

    /**
     * @ORM\Column(name="content", type="text")
     * @Copy\Simple
     */
    private $content;

    /**
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

    /**
     * @ORM\OneToOne(targetEntity="EV\BlogBundle\Entity\Options", cascade={"persist","remove"})
     * @ORM\JoinColumn(name="optionsId", referencedColumnName="id")
     * @Copy\Entity
     */
    private $options;

    /**
     * @ORM\ManyToOne(targetEntity="EV\BlogBundle\Entity\Author", inversedBy="articles")
     * @ORM\JoinColumn(name="authorId", referencedColumnName="id", nullable=false)
     * @Copy\Simple
     */
    private $author;

    /**
     * @ORM\OneToMany(targetEntity="EV\BlogBundle\Entity\Comment", mappedBy="article", cascade={"persist"})
     * @Copy\Collection
     */
    private $comments

    /**
     * @ORM\ManyToOne(targetEntity="EV\BlogBundle\Entity\Blog", inversedBy="articles")
     * @ORM\JoinColumn(name="blogId", referencedColumnName="id", onDelete="cascade")
     */
    protected $blog;

    /**
     * @Copy\Construct(variables={"blog"})
     */
    public function __construct(Blog $blog)
    {
        $this->blog = $blog;
        $this->date = new \DateTime('now');
    }

    // Getters, Setters and Adders methods...

    public function addComment(\EV\BlogBundle\Entity\Comment $comment)
    {
        $this->comments[] = $comment;

        // IMPORTANT : without this line, the copy won't work
        $comment->setArticle($this);

        return $this;
    }

}
namespace EV\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use EV\CopyBundle\Annotation as Copy;

class Comment
{

    /**
     * @ORM\Column(name="pseudo", type="string", length=255)
     * @Copy\Simple
     */
    $pseudo;

    /**
     * @ORM\Column(name="content", type="text")
     * @Copy\Simple
     */
    $content;

    /**
     * @ORM\ManyToOne(targetEntity="EV\BlogBundle\Entity\Article", inversedBy="comments")
     * @ORM\JoinColumn(name="articleId", referencedColumnName="id", nullable=false)
     */
    protected $article;

    // Getters, Setters and Adders methods...

}

使用示例

public function articleCopyAction() {

    //...

    $params = array(
        'blog' => $blog,
        'articleTitle' => $article->getTitle().' - Copy'
    );

    $articleCopy = $this->get('ev_copy.factory.cloner')->copy($article, $params);

    //...

}

未来功能

  • 添加注解以条件化参数的复制
  • 定义复制参数的顺序

如何贡献

要贡献,只需打开一个包含您新代码的Pull Request,注意如果您添加新功能或修改现有功能,您必须在此README中说明它们的功能。

许可

EVCopyBundle采用MIT许可。