lithium\data\model\Relationship::_embedOne()

protected method

Fetch hasOne related data to a whole collection and embed the result in it.

Parameters

  • mixed $collection

    A collection of data.

  • array $options

    The embed query options.

Returns

mixed

The fetched data.

Source

	protected function _embedOne(&$collection, $options) {
		$keys = $this->key();
		$formKey = key($keys);
		$toKey = current($keys);

		$related = [];

		$indexes = $this->_index($collection, $formKey);
		$related = $this->_find(array_keys($indexes), $options);
		$fieldName = $this->fieldName();

		foreach ($related as $index => $entity) {
			if (is_object($entity)) {
				$value = (string) $entity->{$toKey};
				if (isset($indexes[$value])) {
					$source = $collection[$indexes[$value]];
					$source->{$fieldName} = $entity;
				}
			} else {
				$value = (string) $entity[$toKey];
				if (isset($indexes[$value])) {
					$collection[$indexes[$value]][$fieldName] = $entity;
				}
			}
		}
		return $related;
	}