protected function addUnique(Collection $collection, Entity $newItem, $uniqueField = null)
	{
		if ($collection->contains($newItem)) {
			return false;
		}

		if (is_null($uniqueField)) {
			$collection->add($newItem);
		} else {
			$indexBy = $newItem->getProperty($uniqueField);

			if ($collection->offsetExists($indexBy)) {
				throw new \RuntimeException("Cannot add value '{$newItem}' to '{$this}': element by {$uniqueField}={$indexBy} already exists in the collection");
			}

			$collection->set($indexBy, $newItem);
		}

		return true;
	}
protected function addUnique(Collection $collection, Entity $newItem, $uniqueField = null)
	{
		if ($collection->contains($newItem)) {
			return false;
		}

		if (is_null($uniqueField)) {
			$collection->add($newItem);
		} else {
			$indexBy = $newItem->getProperty($uniqueField);

			if ($collection->offsetExists($indexBy)) {
				throw new \RuntimeException("Cannot add value '{$newItem}' to '{$this}': element by {$uniqueField}={$indexBy} already exists in the collection");
			}

			$collection->set($indexBy, $newItem);
		}

		return true;
	}
protected function addUnique ( Collection $ collection , Entity $ newItem , $ uniqueField = null ) { if ( $ collection -> contains ( $ newItem ) ) { return false ; } if ( is_null ( $ uniqueField ) ) { $ collection -> add ( $ newItem ) ; } else { $ indexBy = $ newItem -> getProperty ( $ uniqueField ) ; if ( $ collection -> offsetExists ( $ indexBy ) ) { throw new \ RuntimeException ( "Cannot add value '{$newItem}' to '{$this}': element by {$uniqueField}={$indexBy} already exists in the collection" ) ; } $ collection -> set ( $ indexBy , $ newItem ) ; } return true ; }
Adds an element to collection preserving uniqueness of fields
@param Collection $collection
@param Entity $newItem
@param string $uniqueField
@return boolean true if added, false if already the same instance has been added
@throws \RuntimeException if element with the same unique field values exists
Adds an element to collection preserving uniqueness of fields