PHP code example of tangoman / relationship-bundle

1. Go to this page and download the library: Download tangoman/relationship-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tangoman / relationship-bundle example snippets



// app/AppKernel.php

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

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

        // ...
    }
}


// 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;

    // ...


// 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;

    // ...


// 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;

    // ...


// 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;

    // ...


// 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();
    }


// 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,
                    '