/** * Converts an entity class type to its DocBlock hint type counterpart. * * @param string $type The entity class type (a fully qualified class name). * @param \Cake\ORM\Association $association The association related to the entity class. * @return string The DocBlock type */ public function associatedEntityTypeToHintType($type, Association $association) { if ($association->type() === Association::MANY_TO_MANY || $association->type() === Association::ONE_TO_MANY) { return $type . '[]'; } return $type; }
/** * Create a new sub-marshaller and marshal the associated data. * * @param \Cake\ORM\Association $assoc The association to marshall * @param array $value The data to hydrate * @param array $options List of options. * @return mixed */ protected function _marshalAssociation($assoc, $value, $options) { if (!is_array($value)) { return null; } $targetTable = $assoc->target(); $marshaller = $targetTable->marshaller(); $types = [Association::ONE_TO_ONE, Association::MANY_TO_ONE]; if (in_array($assoc->type(), $types)) { return $marshaller->one($value, (array) $options); } if ($assoc->type() === Association::ONE_TO_MANY || $assoc->type() === Association::MANY_TO_MANY) { $hasIds = array_key_exists('_ids', $value); $onlyIds = array_key_exists('onlyIds', $options) && $options['onlyIds']; if ($hasIds && is_array($value['_ids'])) { return $this->_loadAssociatedByIds($assoc, $value['_ids']); } if ($hasIds || $onlyIds) { return []; } } if ($assoc->type() === Association::MANY_TO_MANY) { return $marshaller->_belongsToMany($assoc, $value, (array) $options); } return $marshaller->many($value, (array) $options); }
/** * Create a new sub-marshaller and marshal the associated data. * * @param \Cake\ORM\Association $assoc The association to marshall * @param array $value The data to hydrate * @param array $options List of options. * @return mixed */ protected function _marshalAssociation($assoc, $value, $options) { $targetTable = $assoc->target(); $marshaller = $targetTable->marshaller(); $types = [Association::ONE_TO_ONE, Association::MANY_TO_ONE]; if (in_array($assoc->type(), $types)) { return $marshaller->one($value, (array) $options); } if ($assoc->type() === Association::MANY_TO_MANY) { return $marshaller->_belongsToMany($assoc, $value, (array) $options); } return $marshaller->many($value, (array) $options); }