groupBy() public method

Groups the collection by a given field
public groupBy ( string $field, $i = true ) : object
$field string
return object A new collection with an item for each group and a subcollection in each group
 public function testGroupBy()
 {
     $collection = new Collection(['first' => new Bar('a'), 'third' => new Bar('c'), 'fourth' => new Bar('c'), 'second' => new Bar('b')]);
     $collections = $collection->groupBy(function (Bar $bar) {
         return $bar->foo;
     });
     $this->assertCount(3, $collections);
     $this->assertSame($collection['first'], $collections['a']['first']);
     $this->assertSame($collection['second'], $collections['b']['second']);
     $this->assertSame($collection['third'], $collections['c']['third']);
     $this->assertSame($collection['fourth'], $collections['c']['fourth']);
 }
 public function getDoctorsListCount($latitude = 0, $longitude = 0, $query = array())
 {
     $this->data = DB::table('doctors');
     $this->data->leftJoin('doctors_details AS dhospital', 'doctors.id', '=', 'dhospital.doctor_id');
     $this->data->leftJoin('listing_hospitals', 'dhospital.detail_id', '=', 'listing_hospitals.id');
     $this->data->leftJoin('doctors_details AS dhmo', 'doctors.id', '=', 'dhmo.doctor_id');
     $this->data->leftJoin('listing_hmo', 'dhmo.detail_id', '=', 'listing_hmo.id');
     $this->data->leftJoin('doctors_specialization', 'doctors.id', '=', 'doctors_specialization.doctor_id');
     $this->data->leftJoin('location_cities', 'listing_hospitals.city', '=', 'location_cities.id');
     $this->data->leftJoin('location_provinces', 'listing_hospitals.province', '=', 'location_provinces.id');
     $this->data->select(DB::raw("doctors.*, dhospital.detail_id as hospital_id, listing_hospitals.listing_title as hospital_title, listing_hospitals.latitude, listing_hospitals.longitude, listing_hospitals.street, location_cities.city_name, location_provinces.province_name, dhmo.detail_id as hmo_id, listing_hmo.listing_title as hmo_title, doctors_specialization.specialization_id, (6371 * acos (cos ( radians(?) ) * cos( radians( listing_hospitals.latitude ) ) * cos( radians( listing_hospitals.longitude ) - radians(?) ) + sin ( radians(?) ) * sin( radians( listing_hospitals.latitude ) ))) AS distance"))->setBindings([$latitude, $longitude, $latitude]);
     $this->data->where('dhospital.detail_type', '=', 1);
     $this->data->where('dhmo.detail_type', '=', 4);
     if (isset($query['specializations'])) {
         $this->data->whereIn('doctors_specialization.specialization_id', $query['specializations']);
     }
     if (isset($query['locations'])) {
         $this->data->whereIn('listing_hospitals.city', $query['locations']);
     }
     $this->data->groupBy('doctors.id');
     // }
     return $this->data->get();
 }
 public function testGroup()
 {
     $collection = new Collection();
     $collection->user1 = array('username' => 'peter', 'group' => 'admin');
     $collection->user2 = array('username' => 'paul', 'group' => 'admin');
     $collection->user3 = array('username' => 'mary', 'group' => 'client');
     $groups = $collection->groupBy('group');
     $this->assertEquals(2, $groups->admin()->count());
     $this->assertEquals(1, $groups->client()->count());
     $firstAdmin = $groups->admin()->first();
     $this->assertEquals('peter', $firstAdmin['username']);
 }
 /**
  * For a given product, retrieves its suppliers
  *
  * @param int $id_product
  * @param int $group_by_supplier
  * @return Collection
  */
 public static function getSupplierCollection($id_product, $group_by_supplier = true)
 {
     $suppliers = new Collection('ProductSupplier');
     $suppliers->where('id_product', '=', (int) $id_product);
     if ($group_by_supplier) {
         $suppliers->groupBy('id_supplier');
     }
     return $suppliers;
 }
    public function getDataGeneration()
    {
        $datas = array();
        $order_to_change_state = array();
        $saved_product = array();
        $datas_quantity = array();
        $customer_concerned = array();
        $concerned_id_order_detail = array();
        $orders = Db::getInstance()->executeS('
		SELECT a.id_order
		FROM `' . _DB_PREFIX_ . 'orders` a
		LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON (c.`id_customer` = a.`id_customer`)
		WHERE a.current_state = ' . (int) $this->generate_order_state . '
		ORDER BY a.`id_order` DESC');
        $unselected_orders_array = array();
        /* if (isset( $this->context->cookie->unselected_orders) && !empty( $this->context->cookie->unselected_orders))
        		{
        			  $unselected_orders_array = Tools::unSerialize( $this->context->cookie->unselected_orders);
        		} */
        if (Tools::getValue('unselected_orders_list', false)) {
            $unselected_orders_array = explode(',', Tools::getValue('unselected_orders_list'));
            $unselected_orders_array = array_map('trim', $unselected_orders_array);
        }
        if (!empty($orders)) {
            foreach ($orders as $order) {
                $id_order = (int) $order['id_order'];
                if (!in_array($id_order, $unselected_orders_array)) {
                    $order = new Order((int) $id_order);
                    if (!Validate::isLoadedObject($order)) {
                        throw new PrestaShopException('object oder simulate can\'t be loaded');
                    }
                    $order_details = $order->getOrderDetailList();
                    if (!empty($order_details)) {
                        $order_to_change_state[] = (int) $id_order;
                        foreach ($order_details as $order_detail) {
                            $id_supplier = 0;
                            $product_key = $order_detail['product_id'] . '_' . $order_detail['product_attribute_id'];
                            $product = new Product($order_detail['product_id'], $order_detail['product_attribute_id']);
                            // update selling price to purchase price
                            $order_detail['unit_price_tax_excl'] = ErpStock::getWholesalePrice($order_detail['product_id'], $order_detail['product_attribute_id']);
                            $order_detail['tax_rate'] = Tax::getProductTaxRate($order_detail['product_id']);
                            $order_detail['unit_price_tax_incl'] = $order_detail['unit_price_tax_excl'] * (1 + (double) $order_detail['tax_rate'] / 100);
                            if (empty($product->id_supplier)) {
                                // Get already associated suppliers
                                $associated_suppliers = new Collection('ProductSupplierCore');
                                $associated_suppliers->where('id_product', '=', (int) $product->id);
                                $associated_suppliers->groupBy('id_supplier');
                                foreach ($associated_suppliers as $associated_supplier) {
                                    $id_supplier = $associated_supplier->id_supplier;
                                }
                            } else {
                                $id_supplier = $product->id_supplier;
                            }
                            if (isset($saved_product[$product_key])) {
                                $datas_quantity[$product_key] += $order_detail['product_quantity'];
                                $customer_concerned[$product_key][] = $order->id_customer;
                                $concerned_id_order_detail[$product_key][] = array('id_order_detail' => $order_detail['id_order_detail'], 'id_customer' => $order->id_customer);
                                $order_detail['total_product_quantity'] = $datas_quantity[$product_key];
                                $order_detail['customer_concerned'] = $customer_concerned[$product_key];
                                $order_detail['concerned_id_order_detail'] = $concerned_id_order_detail[$product_key];
                                $datas[$id_supplier][$product_key] = $order_detail;
                            } else {
                                $product_quantity = $order_detail['product_quantity'];
                                $order_detail['total_product_quantity'] = $product_quantity;
                                $order_detail['customer_concerned'][] = $order->id_customer;
                                $order_detail['concerned_id_order_detail'][] = array('id_order_detail' => $order_detail['id_order_detail'], 'id_customer' => $order->id_customer);
                                $datas[$id_supplier][$product_key] = $order_detail;
                                $datas_quantity[$product_key] = $product_quantity;
                                $customer_concerned[$product_key][] = $order->id_customer;
                                $concerned_id_order_detail[$product_key][] = array('id_order_detail' => $order_detail['id_order_detail'], 'id_customer' => $order->id_customer);
                                $saved_product[$product_key] = true;
                            }
                        }
                    }
                }
            }
        }
        $data_return = array();
        foreach ($datas as $id_supplier => $data) {
            if ($id_supplier > 0) {
                $product_list = array();
                foreach ($data as $product_key => $product_info) {
                    $customer_concerned = '';
                    $customer_concerned_arr = array_unique($product_info['customer_concerned']);
                    foreach ($customer_concerned_arr as $id_customer) {
                        $customer = new Customer($id_customer);
                        $customer_concerned .= $customer->lastname . ' ' . $customer->firstname . ', ';
                    }
                    $total_te = $product_info['unit_price_tax_excl'] * $product_info['total_product_quantity'];
                    $total_ti = $product_info['unit_price_tax_incl'] * $product_info['total_product_quantity'];
                    $product_info['total_price_tax_excl'] = Tools::displayPrice($total_te);
                    $product_info['total_price_tax_incl'] = Tools::displayPrice($total_ti);
                    $product_info['unit_price_tax_excl'] = $product_info['unit_price_tax_excl'];
                    $product_info['customer_concerned'] = Tools::substr($customer_concerned, 0, Tools::strlen($customer_concerned) - 2);
                    $product_info['customer_id'] = $customer_concerned_arr;
                    $product_list[] = $product_info;
                }
                $data_return[$id_supplier] = $product_list;
            }
        }
        // for the order, get the orders list that the statuts should be update in provider order
        if ($this->display == 'ordering') {
            return array('data_return' => $data_return, 'order_to_change_state' => $order_to_change_state);
        } else {
            return $data_return;
        }
    }
 public static function getTermsUsedForTaxonomy($taxonomy)
 {
     $terms = self::getTermsUsed();
     $terms = Collection::groupBy($terms, 'taxonomy');
     return array_key_exists($taxonomy, $terms) ? $terms[$taxonomy] : null;
 }