Executes delete for one-to-many relation

@param array|\ArrayAccess $result

@return array|\ArrayAccess
@throws RelationException
487245
public function delete(&$result)
    {
        $container = $this->accessor->getPropertyValue($result, $this->definition->container());
        if (empty($container)) {
            return $result;
        }

        foreach ($container as $entity) {
            $mediator = [];

            foreach ($this->definition->localKeys() as $local => $foreign) {
                $mediator[$foreign] = $this->accessor->getPropertyValue($result, $local);
            }

            foreach ($this->definition->foreignKeys() as $foreign => $local) {
                $mediator[$foreign] = $this->accessor->getPropertyValue($entity, $local);
            }

            $this->storage->delete($mediator, $this->definition->mediator())->execute();

        }
        $this->accessor->setPropertyValue($result, $this->definition->container(), $container);

        return $result;
    }
1035510
protected function _makeMultiGetResult(array $result) {
        return MultiGetResult::make($result, $this->getClient())->setModelClass($this->_fullModelClassNamePattern());
    }
909250
private function process(Statement $results)
    {
        return $this->manager
            ->getResultProcessor()
            ->processRecords(
                $this->entity,
                $this->with,
                $this->fetchResults(
                    $results,
                    $this->entity->getPrimaryKey()
                ),
                $this->readOnly
            );
    }
1126134
public function relationCount($result, $closure)
    {
        $localKey = $this->localKey;
        $count    = 0;

        if (isset($result->$localKey)) {
            if ($closure) {
                $closure($this->query);
            }

            $count = $this->query->where($this->foreignKey, '=', $result->$localKey)->count();
        }

        return $count;
    }
395164
private function _mergeAssociated(
        array $result,
        array $associated,
        $refKey,
        $colName
    ) {
        foreach ($result as $index => $item) {

            if (is_array($item)) {
                $refValue = $item[$refKey];
            } else {
                $refValue = $item->{$refKey};
            }

            if (isset($associated[$refValue])) {

                if (is_array($result[$index])) {
                    $result[$index][$colName] = $associated[$refValue];
                } else {
                    $result[$index]->{$colName} = $associated[$refValue];
                }
            }
        }
        return $result;
    }
1301234
public function assignChildren ($relationIdentifier, $id, $childIds, $subtractive = true)
		{
			$relation = $this->getRelation($relationIdentifier);
			$childIds = (array)$childIds;

			if (! $relation) {
				throw new RelationException('Invalid relation `' . $relationIdentifier);
			}

			if ($relation instanceof OneToMany) {
				$this->database->createQuery()
					->update()
					->table($relation->getRelatedTable())
					->set(
						array(
							$relation->getRelatedKey() => $id,
						)
					)
					->whereIn($this->database->primaryKey($relation->getRelatedTable()), $childIds)
					->execute();

				if ($subtractive) {
					$this->database->createQuery()
						->update()
						->table($relation->getRelatedTable())
						->set(
							array(
								$relation->getRelatedKey() => null,
							)
						)
						->whereEqual($relation->getRelatedKey(), $id)
						->whereNotIn($this->database->primaryKey($relation->getRelatedTable()), $childIds)
						->execute();
				}
			} else if ($relation instanceof ManyToMany) {
				if ($subtractive) {
					$this->database->createQuery()
						->delete()
						->from($relation->getRelationalTable())
						->whereEqual($relation->getKey(), $id)
						->whereNotIn($relation->getRelatedKey(), $childIds)
						->execute();
				}

				$query = $this->database->createQuery();

				$assignedIds =
					$query->select($relation->getRelatedKey())
						->from($relation->getRelationalTable())
						->whereEqual($relation->getKey(), $id)
						->execute()
						->fetchColumn();

				$newIds = array_diff($childIds, $assignedIds);

				foreach ($newIds as $newId) {
					$this->database->createQuery()
						->insert()
						->into($relation->getRelationalTable())
						->set(
							array(
								$relation->getKey()        => $id,
								$relation->getRelatedKey() => $newId,
							)
						)
						->execute();
				}
			} else {
				throw new RelationException('Invalid relation type `' . gettype($relation) . '`');
			}
		}
682701
protected function match($model, $relation, &$result)
    {
        // 重新组装模型数据
        foreach ($result->getData() as $key => $val) {
            if (strpos($key, '__')) {
                list($name, $attr) = explode('__', $key, 2);
                if ($name == $relation) {
                    $list[$name][$attr] = $val;
                    unset($result->$key);
                }
            }
        }

        if (isset($list[$relation])) {
            $array = array_unique($list[$relation]);

            if (count($array) == 1 && null === current($array)) {
                $relationModel = null;
            } else {
                $relationModel = new $model($list[$relation]);
                $relationModel->setParent(clone $result);
                $relationModel->isUpdate(true);
            }

            if (!empty($this->bindAttr)) {
                $this->bindAttr($relationModel, $result, $this->bindAttr);
            }
        } else {
            $relationModel = null;
        }

        $result->setRelation(Db::parseName($relation), $relationModel);
    }
1329895
public function afterFetch($saved, $result)
    {
        // Multiple results
        if (is_array($result)) {
            $all = new ArrayCollection();
            foreach ($result as $entity) {
                if ($entity instanceof Entity) {
                    $all[] = self::afterFetch($saved, $entity);
                }
            }
            return $all;
        }

        // Normal single processing.
        if ($result instanceof Entity) {
            $result->_saved = $saved;

            // Process relation properties, delete the 'real' props
            $this->injectVirtualProperties($result);
        }
        return $result;
    }
1220262
public function relationCount($result, $closure)
    {
        $pk    = $result->getPk();
        $count = 0;

        if (isset($result->$pk)) {
            if ($closure) {
                $closure($this->query);
            }

            $count = $this->query
                ->where([
                    [$this->morphKey, '=', $result->$pk],
                    [$this->morphType, '=', $this->type],
                ])
                ->count();
        }

        return $count;
    }
182186
protected function resultToModel(&$result, $options = [], $resultSet = false, $withRelationAttr = [])
    {
        // 动态获取器
        if (!empty($options['with_attr']) && empty($withRelationAttr)) {
            foreach ($options['with_attr'] as $name => $val) {
                if (strpos($name, '.')) {
                    list($relation, $field) = explode('.', $name);

                    $withRelationAttr[$relation][$field] = $val;
                    unset($options['with_attr'][$name]);
                }
            }
        }

        $condition = (!$resultSet && isset($options['where']['$and'])) ? $options['where']['$and'] : null;
        $result    = $this->model->newInstance($result, $condition);

        // 动态获取器
        if (!empty($options['with_attr'])) {
            $result->setModelAttrs($options['with_attr']);
        }

        // 关联查询
        if (!empty($options['relation'])) {
            $result->relationQuery($options['relation'], $withRelationAttr);
        }

        // 预载入查询
        if (!$resultSet && !empty($options['with'])) {
            $result->eagerlyResult($result, $options['with'], $withRelationAttr);
        }

        // 关联统计
        if (!empty($options['with_count'])) {
            foreach ($options['with_count'] as $val) {
                $result->relationCount($result, $val[0], $val[1], $val[2]);
            }
        }

    }
960862
protected function reduceResult($result)
    {
        if (is_array($result) && (count($result) == 1)) {
            reset($result);
            return $this->reduceResult($result[key($result)]);
        } else {
            return $result;
        }
    }
1277864
public function match(array $results, $relation)
    {
        foreach (array_keys($this->dictionary) as $type) {
            $results = $this->matchToMorphParents($type, $this->getResultsByType($type), $results);
        }

        return $results;
    }
1361257
public function eagerlyResult(&$result, $relation, $subRelation, $closure)
    {
        $pk = $result->getPk();

        if (isset($result->$pk)) {
            $pk   = $result->$pk;
            $data = $this->eagerlyMorphToOne([
                [$this->morphKey, '=', $pk],
                [$this->morphType, '=', $this->type],
            ], $relation, $subRelation, $closure);

            if (isset($data[$pk])) {
                $relationModel = $data[$pk];
                $relationModel->setParent(clone $result);
                $relationModel->isUpdate(true);
            } else {
                $relationModel = null;
            }

            $result->setRelation(Db::parseName($relation), $relationModel);
        }
    }
331879
protected function autoRelationDelete()
    {
        foreach ($this->relationWrite as $key => $name) {
            $name   = is_numeric($key) ? $name : $key;
            $result = $this->getRelation($name);

            if ($result instanceof Model) {
                $result->delete();
            } elseif ($result instanceof Collection) {
                foreach ($result as $model) {
                    $model->delete();
                }
            }
        }
    }
809838
protected function filterResult(Result $result)
    {
        $request = $result->getData('request') ?: array();
        foreach (array('AccountNumber', 'RoutingNumber') as $key) {
            if (array_key_exists($key, $request)) {
                $request[$key] = '[filtered]';
            }
        }

        $result->setData('request', $request);

        return $result;
    }
518242
protected function cacheManyRelationResults(string $parentKey, string $relation, $results, Relationship $relationship)
    {
        $this->cache[$parentKey][$relation] = [];

        foreach ($results as $result) {
            $cachedRelationship = $this->getCachedRelationship($relation, $result, $relationship);

            $relatedHash = $cachedRelationship->getHash();

            $this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
        }
    }
461813
public static function parseRelationShips(string $relationships, &$results): array
    {
        $relationships = explode(',', $relationships);

        $newResults = [];

        //if its a list
        if ($results instanceof ResultsetInterface && count($results) >= 1) {
            foreach ($results as $key => $result) {
                //clean records conver to array
            $newResults[$key] = $result->toArray();
                foreach ($relationships as $relationship) {
                    if ($results[$key]->$relationship) {
                        $newResults[$key][$relationship] = $results[$key]->$relationship;
                    }
                }
            }
        } else {
            //if its only 1 record
            if ($results instanceof Model) {
                $newResults = $results->toArray();
                foreach ($relationships as $relationship) {
                    if ($results->$relationship) {
                        $newResults[$relationship] = $results->$relationship;
                    }
                }
            }
        }

        unset($results);
        return $newResults;
    }