gollumsf/entity-relation-setter

Trait for add method cross setter

v2.2.0 2022-04-03 23:50 UTC

This package is auto-updated.

Last update: 2024-11-19 12:44:24 UTC


README

Build Status Build Status Build Status Build Status

Build Status Build Status Build Status

Coverage License Latest Stable Version Latest Unstable Version Discord

Trait for add method cross setter

Installation:

composer require gollumsf/entity-relation-setter

Exemple

OneToOne

use GollumSF\EntityRelationSetter\OneToOneSetter;

class User {
	
	use OneToOneSetter;
    
	/**
	 * @ORM\OneToOne(targetEntity=Address::class, inversedBy="tiers")
	 * @var Address
	 */
	private $address;

	////////////
	// Setter //
	////////////

	public function setAddress(?Address $address): self {
		return $this->oneToOneSet($address/*, 'address', 'user'*/);
	}
}

class Address {
	
	use OneToOneSetter;
    
	/**
	 * @ORM\OneToOne(targetEntity=Tiers::User, mappedBy="address")
	 * @var Address
	 */
	private $address;

	////////////
	// Setter //
	////////////

	public function setUser(?User $user): self {
		return $this->oneToOneSet($user/*, 'user', 'address'*/);
	}
}

OneToManySetter and ManyToOneSetter

use GollumSF\EntityRelationSetter\ManyToOneSetter;
use GollumSF\EntityRelationSetter\OneToManySetter;

class Address {
	
	use ManyToOneSetter;

	/**
	 * @ORM\ManyToOne(targetEntity=Country::class, inversedBy="addresses")
	 * @var Country
	 */
	private $country;

	////////////
	// Setter //
	////////////

	public function setCountry(?Country $country): self {
		return $this->manyToOneSet($country/*, 'country', 'address'*/);
	}
}

class Country {
	
	use OneToManySetter;

	/**
	 * @ORM\OneToMany(targetEntity=Address::class, mappedBy="country")
	 * @var Address[]|ArrayCollection
	 */
	private $addresses;
	
	public function __construct() {
		$this->addresses = new ArrayCollection();
	}

	/////////
	// Add //
	/////////

	public function addAddress(Address $address): self {
		return $this->oneToManyAdd($address/*, 'addresses', 'country'*/);
	}
	
	////////////
	// Remove //
	////////////

	public function removeAddress(Address $address): self {
		return $this->oneToManyRemove($address/*, 'addresses', 'country'*/);
	}
}

ManyToManySetter

use GollumSF\EntityRelationSetter\ManyToManySetter;

class Post {
	
	use ManyToManySetter;

	/**
	 * @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="posts")
	 * @var Tag[]|ArrayCollection
	 */
	private $tags;
	
	public function __construct() {
		$this->tags = new ArrayCollection();
	}

	/////////
	// Add //
	/////////

	public function addTag(Tag $tag): self {
		return $this->manyToManyAdd($tag/*, 'tags', 'post'*/);
	}
	
	////////////
	// Remove //
	////////////

	public function removeTag(Tag $tag): self {
		return $this->manyToManyRemove($tag/*, 'tags', 'post'*/);
	}
}

class Tag {
	
	use ManyToManySetter;

	/**
	 * @ORM\ManyToMany(targetEntity=Post:class, inversedBy="tags")²&
	 * @var Post[]|ArrayCollection
	 */
	private $posts;
	
	public function __construct() {
		$this->posts = new ArrayCollection();
	}

	/////////
	// Add //
	/////////

	public function addPost(Post $post): self {
		return $this->manyToManyAdd($post/*, 'posts', 'tag'*/);
	}
	
	////////////
	// Remove //
	////////////

	public function removePost(Post $post): self {
		return $this->manyToManyRemove($post/*, 'posts', 'tag'*/);
	}
}