/**
  * @dataProvider providerTestSlice
  */
 public function testSlice($array, $a, $b, $result)
 {
     $collection = new Collection($array);
     $this->assertInstanceOf(Collection::class, $collection->slice($a, $b));
     $this->assertNotSame($collection, $collection->reverse());
     $this->assertEquals($result, $collection->slice($a, $b)->toArray());
 }
 public function resolve($var = null)
 {
     if (!$this->resolution) {
         // Resolve down to a real Model object, then call __get on it.
         $ids = $this->resolveState();
         $results = Model::factoryObjectCache($ids, $this->currentTable, $this->database);
         if ($this->multiresult && !$results instanceof Collection) {
             $results = new Collection([$results]);
         }
         if (!$this->multiresult && $results instanceof Collection && $results->count() == 1) {
             $results = $results[0];
         }
         $this->resolution = $results;
     }
     if (!is_null($var)) {
         return $this->resolution->{$var};
     }
     return $this->resolution;
 }
Exemple #3
0
 public static function groupJoinCount(Collection $collection, $var, $where = [])
 {
     if (!$collection->count()) {
         return $collection;
     }
     $proto = $collection[0]->_data;
     $results = new Collection();
     /* FOREIGN KEYS */
     if (key_exists($var, (array) $proto->__model['one-to-one'])) {
         $ids = $collection->id->toArray();
         /* Call Tablename::factory(foreign key id) to get the object we want */
         $table = $proto->__model['many-to-one'][$var];
         list($data) = Model::factoryDataCount(['id' => $ids] + $where, $table, $proto->__database);
         return $data['count'];
     }
     if (key_exists($var, (array) $proto->__model['many-to-one'])) {
         // Remove duplicates from the group
         $ids = array_unique($collection->{$var . '_id'}->toArray());
         /* Call Tablename::factory(foreign key id) to get the object we want */
         $table = $proto->__model['many-to-one'][$var];
         list($data) = Model::factoryDataCount(['id' => $ids] + $where, $table, $proto->__database);
         return $data['count'];
     }
     /* Look for lists of objects in other tables referencing this one */
     if (key_exists($var, (array) $proto->__model['one-to-many'])) {
         $table = $proto->__model['one-to-many'][$var]['table'];
         $column = $proto->__model['one-to-many'][$var]['column_name'];
         $ids = $collection->id->toArray();
         // Use the model factory to find the relevant items
         list($data) = Model::factoryDataCount([$column => $ids] + $where, $table, $proto->__database);
         return $data['count'];
     }
     if (key_exists($var, (array) $proto->__model['many-to-many'])) {
         // Get pivot schema
         $pivot = $proto->__model['many-to-many'][$var];
         $ids = $collection->id->toArray();
         // We can only support simple connection access for 2 key pivots.
         if (count($pivot['connections']) != 1) {
             throw new Exception\Model('MODEL_DATA:CANNOT_CALL_MULTIPIVOT_AS_PROPERTY', array($var));
         }
         // Get a list of ids linked to this object (i.e. the tablename_id stored in the pivot table)
         $pivot_schema = $proto->__schema->getTable($pivot['pivot']);
         $pivot_tablename = $pivot_schema['table_name'];
         $q = QueryBuilder::select([$pivot_tablename => 'pivot'], ['pivot.*'])->where(['`pivot`.' . $pivot['id'] => $ids])->join([Schema::underscoreCase($pivot['connections'][0]['table']) => 'pivotjoin'])->joinOn(['pivotjoin.id' => "`pivot`.`{$pivot['connections'][0]['column']}`"])->joinWhere($where);
         $query = new Query($proto->__database);
         list($raw) = $query->sql($q)->execute();
         // Rearrange the list of ids into a flat array and an id grouped array
         $flat_ids = [];
         foreach ($raw as $raw_id) {
             $flat_ids[] = $raw_id[$pivot['connections'][0]['column']];
         }
         // Remove duplicates to make sql call smaller.
         $flat_ids = array_unique($flat_ids);
         // Use the model factory to retrieve the objects from the list of ids (using cache first)
         list($data) = Model::factoryDataCount(['id' => $flat_ids] + $where, $pivot['connections'][0]['table'], $proto->__database);
         return $data['count'];
     }
 }