Creates an associative array using a $path1 as the path to build its keys, and optionally
$path2 as path to get the values. If $path2 is not specified, all values will be
initialized to null (useful for Set::merge()). You can optionally group the values by
what is obtained when following the path specified in $groupPath.
public static combine ( array $data, mixed $path1 = null, mixed $path2 = null, string $groupPath = null ) : array | ||
$data | array | Array from where to extract keys and values. |
$path1 | mixed | As an array, or as a dot-delimited string. |
$path2 | mixed | As an array, or as a dot-delimited string. |
$groupPath | string | As an array, or as a dot-delimited string. |
Результат | array | Combined array. |
public function loadMetadataForClass($className, ClassMetadataInfo $metadata) { if (!$metadata->reflClass instanceof SchemaReflection) { $metadata->reflClass = new SchemaReflection(get_class($metadata)); } $metadata->primaryTable['name'] = $className::meta('source'); $primaryKey = $className::meta('key'); $bindings = static::bindings($className); $relations = array(); if (!empty($bindings)) { foreach ($bindings as $type => $set) { foreach ($set as $key => $relation) { $mapping = array('fetch' => \Doctrine\ORM\Mapping\AssociationMapping::FETCH_EAGER, 'fieldName' => $relation['fieldName'], 'sourceEntity' => $className, 'targetEntity' => $relation['class'], 'mappedBy' => null, 'cascade' => !empty($relation['dependent']) ? array('remove') : array(), 'optional' => $type != 'belongsTo'); if (in_array($type, array('hasOne', 'hasMany'))) { $inverse = $type == 'belongsTo'; $mapping['joinColumns'][] = array('fieldName' => !$inverse ? $relation['key'] : $relation['fieldName'], 'name' => !$inverse ? $relation['fieldName'] : $relation['key'], 'referencedColumnName' => $relation['class']::meta('key')); } if (in_array($type, array('belongsTo', 'hasOne', 'hasMany'))) { $mapping['mappedBy'] = static::_fieldName($mapping); } $relations[$type][$key] = $mapping; } } } $schema = (array) $className::schema(); $metadata->reflClass->setRelations($relations); $metadata->reflClass->setSchema($schema); $belongsToFields = !empty($bindings['belongsTo']) ? Set::combine(array_values($bindings['belongsTo']), '/key', '/fieldName') : array(); foreach ($schema as $field => $column) { $mapping = array_merge(array('id' => $field == $primaryKey, 'fieldName' => !empty($belongsToFields[$field]) ? $belongsToFields[$field] : $field, 'columnName' => $field), (array) $column); $metadata->mapField($mapping); if ($mapping['id']) { $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_AUTO); } } foreach ($relations as $type => $set) { foreach ($set as $key => $mapping) { $metadata->{static::$_bindingMapping[$type]}($mapping); $mapping = $metadata->associationMappings[$mapping['fieldName']]; } } }
public function testCombine() { $result = Set::combine(array(), '/User/id', '/User/Data'); $this->assertFalse($result); $result = Set::combine('', '/User/id', '/User/Data'); $this->assertFalse($result); $a = array( array('User' => array('id' => 2, 'group_id' => 1, 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))), array('User' => array('id' => 14, 'group_id' => 2, 'Data' => array('user' => 'jperras', 'name' => 'Joel Perras'))), array('User' => array('id' => 25, 'group_id' => 1, 'Data' => array('user' => 'gwoo','name' => 'The Gwoo')))); $result = Set::combine($a, '/User/id'); $expected = array(2 => null, 14 => null, 25 => null); $this->assertIdentical($expected, $result); $result = Set::combine($a, '/User/id', '/User/non-existant'); $expected = array(2 => null, 14 => null, 25 => null); $this->assertIdentical($expected, $result); $result = Set::combine($a, '/User/id', '/User/Data/.'); $expected = array( 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'), 14 => array('user' => 'jperras', 'name' => 'Joel Perras'), 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')); $this->assertIdentical($expected, $result); $result = Set::combine($a, '/User/id', '/User/Data/name/.'); $expected = array( 2 => 'Mariano Iglesias', 14 => 'Joel Perras', 25 => 'The Gwoo'); $this->assertIdentical($expected, $result); $result = Set::combine($a, '/User/id', '/User/Data/.', '/User/group_id'); $expected = array( 1 => array( 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'), 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')), 2 => array( 14 => array('user' => 'jperras', 'name' => 'Joel Perras'))); $this->assertIdentical($expected, $result); $result = Set::combine($a, '/User/id', '/User/Data/name/.', '/User/group_id'); $expected = array( 1 => array( 2 => 'Mariano Iglesias', 25 => 'The Gwoo'), 2 => array( 14 => 'Joel Perras')); $this->assertIdentical($expected, $result); $result = Set::combine( $a, '/User/id', array('{0}: {1}', '/User/Data/user', '/User/Data/name'), '/User/group_id' ); $expected = array( 1 => array(2 => 'mariano.iglesias: Mariano Iglesias', 25 => 'gwoo: The Gwoo'), 2 => array(14 => 'jperras: Joel Perras') ); $this->assertIdentical($expected, $result); $result = Set::combine( $a, array('{0}: {1}', '/User/Data/user', '/User/Data/name'), '/User/id' ); $expected = array( 'mariano.iglesias: Mariano Iglesias' => 2, 'jperras: Joel Perras' => 14, 'gwoo: The Gwoo' => 25 ); $this->assertIdentical($expected, $result); $result = Set::combine( $a, array('{1}: {0}', '/User/Data/user', '/User/Data/name'), '/User/id' ); $expected = array( 'Mariano Iglesias: mariano.iglesias' => 2, 'Joel Perras: jperras' => 14, 'The Gwoo: gwoo' => 25 ); $this->assertIdentical($expected, $result); $result = Set::combine($a, array( '%1$s: %2$d', '/User/Data/user', '/User/id'), '/User/Data/name' ); $expected = array( 'mariano.iglesias: 2' => 'Mariano Iglesias', 'jperras: 14' => 'Joel Perras', 'gwoo: 25' => 'The Gwoo' ); $this->assertIdentical($expected, $result); $result = Set::combine($a, array( '%2$d: %1$s', '/User/Data/user', '/User/id'), '/User/Data/name' ); $expected = array( '2: mariano.iglesias' => 'Mariano Iglesias', '14: jperras' => 'Joel Perras', '25: gwoo' => 'The Gwoo' ); $this->assertIdentical($expected, $result); $b = new stdClass(); $b->users = array( array('User' => array( 'id' => 2, 'group_id' => 1, 'Data' => array( 'user' => 'mariano.iglesias','name' => 'Mariano Iglesias' ) )), array('User' => array('id' => 14, 'group_id' => 2, 'Data' => array( 'user' => 'jperras', 'name' => 'Joel Perras' ))), array('User' => array('id' => 25, 'group_id' => 1, 'Data' => array( 'user' => 'gwoo','name' => 'The Gwoo' ))) ); $result = Set::combine($b, '/users/User/id'); $expected = array(2 => null, 14 => null, 25 => null); $this->assertIdentical($expected, $result); $result = Set::combine($b, '/users/User/id', '/users/User/non-existant'); $expected = array(2 => null, 14 => null, 25 => null); $this->assertIdentical($expected, $result); }
/** * */ public function fields($fields, $query) { $columns = array(); if (!empty($fields)) { $columns = $this->schema($query); if (!empty($columns)) { $belongsToFields = array(); foreach ($columns as $key => $currentFields) { $className = is_string($key) ? $key : $query->model(); $belongsTo = ModelDriver::bindings($className, 'belongsTo'); $belongsToFields = !empty($belongsTo) ? Set::combine(array_values($belongsTo), '/key', '/fieldName') : array(); foreach ($fields as $i => $field) { if (!empty($belongsToFields[$field])) { unset($fields[$i]); $fields[] = $belongsToFields[$field]; } } $columns[$key] = array_unique($fields); } } } return $columns; }