2017 © Pedro Peláez
 

symfony-bundle relationship-bundle

Symfony Relationship Bundle

image

tangoman/relationship-bundle

Symfony Relationship Bundle

  • Sunday, April 8, 2018
  • by Tangoman
  • Repository
  • 0 Watchers
  • 0 Stars
  • 60 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 8 Versions
  • 2 % Grown

The README.md

TangoMan Relationship Bundle

TangoMan Relationship Bundle provides magic methods for OneToOne, OneToMany, ManyToOne, ManyToMany, relationships., (*1)

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:, (*2)

$ composer require tangoman/relationship-bundle

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

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:, (*4)

<?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., (*5)

Step 4: Update your database schema

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

$ 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, (*7)

<?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)., (*8)

<?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, (*9)

License

Copyright (c) 2018 Matthias Morin, (*10)

License Distributed under the MIT license., (*11)

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

Matthias Morin | LinkedIn, (*13)

The Versions

08/04 2018

dev-master

9999999-dev

Symfony Relationship Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Fabrice Lazzarotto
by Matthias Morin

bundle symfony traits relationships manytomany onetomany onetoone tangoman manytoone

08/04 2018

1.1.3

1.1.3.0

Symfony Relationship Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Fabrice Lazzarotto
by Matthias Morin

bundle symfony traits relationships manytomany onetomany onetoone tangoman manytoone

21/02 2018

1.1.2

1.1.2.0

Symfony Relationship Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Fabrice Lazzarotto
by Matthias Morin

bundle symfony traits relationships manytomany onetomany onetoone tangoman manytoone

20/11 2017

1.1.1

1.1.1.0

Symfony Relationship Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Fabrice Lazzarotto
by Matthias Morin

bundle symfony traits relationships manytomany onetomany onetoone tangoman manytoone

20/11 2017

1.1.0

1.1.0.0

Symfony Relationship Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Fabrice Lazzarotto
by Matthias Morin

bundle symfony traits relationships manytomany onetomany onetoone tangoman manytoone

17/11 2017

1.0.2

1.0.2.0

Symfony Relationship Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Fabrice Lazzarotto
by Matthias Morin

bundle symfony traits relationships manytomany onetomany onetoone tangoman manytoone

14/11 2017

1.0.1

1.0.1.0

Symfony Relationship Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Fabrice Lazzarotto
by Matthias Morin

bundle symfony traits relationships manytomany onetomany onetoone tangoman manytoone

10/11 2017

1.0.0

1.0.0.0

Symfony Relationship Bundle

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Fabrice Lazzarotto
by Matthias Morin

bundle symfony traits relationships manytomany onetomany onetoone tangoman manytoone