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

protected method

Fetch belongsTo 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 _embedBelongsTo(&$collection, $options) {
		$keys = $this->key();
		$formKey = key($keys);
		$toKey = current($keys);

		$related = [];

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

		$fieldName = $this->fieldName();

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