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

protected method

Fetch hasMany related data (through an embedded list) 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 _embedHasManyAsList(&$collection, $options) {
		$keys = $this->key();
		$formKey = key($keys);
		$toKey = current($keys);

		$related = [];

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

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

		return $related;
	}