承接 tangoman/relationship-bundle 相关项目开发

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

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

tangoman/relationship-bundle

Composer 安装命令:

composer require tangoman/relationship-bundle

包简介

Symfony Relationship Bundle

README 文档

README

TangoMan Relationship Bundle provides magic methods for OneToOne, OneToMany, ManyToOne, ManyToMany, relationships.

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require tangoman/relationship-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    // ...

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new TangoMan\FrontBundle\TangoManRelationshipBundle(),
        );

        // ...
    }
}

Step 3: Update your entities

Use TangoMan\RelationshipBundle\Traits\HasRelationships trait inside your entities, and define properties with appropriate doctrine annotations.

Step 4: Update your database schema

Open a command console, enter your project directory and execute the following command to update your database schema:

$ php bin/console schema:update

Usage

Entities

  • Both entities must use HasRelationships trait.
  • Both entities must define properties with appropriate doctrine annotations.
  • cascade={"persist"} annotation is MANDATORY (will allow bidirectional linking between entities).
  • @method annotation will allow for correct autocomplete in your IDE (optional).
  • NOTE: inversedBy is always on the owning side of the relationship (but I believe you know that already.)

OneToOne relationships

  • cascade={"remove"} will avoid orphan Item on Owner deletion (optional).
<?php
// src\AppBundle\Entity\Owner.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use TangoMan\RelationshipBundle\Traits\HasRelationships;

/**
 * Class Owner
 * @ORM\Table(name="owner")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\OwnerRepository")
 *
 * @package AppBundle\Entity
 *
 * @method $this setItem(Item $item)
 * @method Item getItems()
 */
class Owner
{
    use HasRelationships;

    // ...

    /**
     * @var Item
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Item", inversedBy="owner", cascade={"persist", "remove"})
     */
    private $item;

    // ...
<?php
// src\AppBundle\Entity\Item.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use TangoMan\RelationshipBundle\Traits\HasRelationships;

/**
 * Class Item
 * @ORM\Table(name="item")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ItemRepository")
 *
 * @package AppBundle\Entity
 *
 * @method $this setOwner(Owner $owner)
 * @method Owner getOwner()
 */
class Item
{
    use HasRelationships;

    // ...

    /**
     * @var Owner
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Owner", mappedBy="item", cascade={"persist"})
     */
    private $owner;

    // ...

ManyToMany relationships

Entity properties

  • Property must own @var ArrayCollection
  • Property name MUST use plural form (as it represents several entities)
  • @ORM\OrderBy({"name"="ASC"}) will allow to define custom orderBy when fetching items (optional).
<?php
// src\AppBundle\Entity\Owner.php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use TangoMan\RelationshipBundle\Traits\HasRelationships;

/**
 * Class Owner
 * @ORM\Table(name="owner")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\OwnerRepository")
 *
 * @package AppBundle\Entity
 *
 * @method $this setItems(Item[] $items)
 * @method Item getItems()
 */
class Owner
{
    use HasRelationships;

    // ...

    /**
     * @var ArrayCollection|Item[]
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Item", inversedBy="owners", cascade={"persist", "remove"})
     * @ORM\OrderBy({"name"="ASC"})
     */
    private $items;

    // ...
<?php
// src\AppBundle\Entity\Item.php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use TangoMan\RelationshipBundle\Traits\HasRelationships;

/**
 * Class Item
 * @ORM\Table(name="item")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ItemRepository")
 *
 * @package AppBundle\Entity
 *
 * @method $this setOwners(Owner[] $owners)
 * @method Owner getOwners()
 */
class Item
{
    use HasRelationships;

    // ...

    /**
     * @var ArrayCollection|Owner[]
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Owner", mappedBy="items", cascade={"persist"})
     * @ORM\OrderBy({"name"="ASC"})
     */
    private $owners;

    // ...

Entity constructor

Constructors MUST initialize properties with ArrayCollection

<?php
// src\AppBundle\Entity\Owner.php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use TangoMan\RelationshipBundle\Traits\HasRelationships;

// ...

    /**
     * Owner constructor.
     */
    public function __construct()
    {
        // ...

        $this->Items = new ArrayCollection();
    }

FormTypes

Your formTypes elements from the INVERSE side of relationships MUST own 'by_reference' => false, attribute to force use of appropriate setters and getters (i.e. add and remove methods).

<?php
// src\AppBundle\Form\ItemType.php
// ...

    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add(
                'owner',
                EntityType::class,
                [
                    'label'         => 'Owner',
                    'placeholder'   => 'Select owner',
                    'class'         => 'AppBundle:Owner',
                    'by_reference'  => false,
                    'multiple'      => true,
                    'expanded'      => false,
                    'required'      => false,
                    'query_builder' => function (EntityRepository $em) {
                        return $em->createQueryBuilder('o')
                            ->orderBy('o.name');
                    },
                ]
            );
    }

Note

If you find any bug please report here : Issues

License

Copyright (c) 2018 Matthias Morin

License Distributed under the MIT license.

If you like TangoMan Relationship Bundle please star! And follow me on GitHub: TangoMan75 ... And check my other cool projects.

Matthias Morin | LinkedIn

tangoman/relationship-bundle 适用场景与选型建议

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

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

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

围绕 tangoman/relationship-bundle 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-10