PHP code example of gollumsf / entity-relation-setter

1. Go to this page and download the library: Download gollumsf/entity-relation-setter 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/ */

    

gollumsf / entity-relation-setter example snippets


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


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


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