src/Tasks/CalculateProductPopularity.php
public function viaphp()
    {
        $ps = singleton(Product::class);
        $q = $ps->buildSQL('"SilverShop_Product"."AllowPurchase" = 1');
        $select = $q->select;
        $select['NewPopularity'] =
            self::config()->number_sold_calculation_type . '("SilverShop_OrderItem"."Quantity") AS "NewPopularity"';
        $q->select($select);
        $q->groupby('"Product"."ID"');
        $q->orderby('"NewPopularity" DESC');
        $q->leftJoin('SilverShop_Product_OrderItem', '"SilverShop_Product"."ID" = "SilverShop_Product_OrderItem"."ProductID"');
        $q->leftJoin('SilverShop_OrderItem', '"SilverShop_Product_OrderItem"."ID" = "SilverShop_OrderItem"."ID"');
        $records = $q->execute();
        $productssold = $ps->buildDataObjectSet($records, "DataObjectSet", $q, Product::class);
        //TODO: this could be done faster with an UPDATE query (SQLQuery doesn't support this yet @11/06/2010)
        foreach ($productssold as $product) {
            if ($product->NewPopularity != $product->Popularity) {
                $product->Popularity = $product->NewPopularity;
                $product->writeToStage('Stage');
                $product->publishSingle();
            }
        }
    }
public function viaphp()
    {
        $ps = singleton(Product::class);
        $q = $ps->buildSQL('"SilverShop_Product"."AllowPurchase" = 1');
        $select = $q->select;
        $select['NewPopularity'] =
            self::config()->number_sold_calculation_type . '("SilverShop_OrderItem"."Quantity") AS "NewPopularity"';
        $q->select($select);
        $q->groupby('"Product"."ID"');
        $q->orderby('"NewPopularity" DESC');
        $q->leftJoin('SilverShop_Product_OrderItem', '"SilverShop_Product"."ID" = "SilverShop_Product_OrderItem"."ProductID"');
        $q->leftJoin('SilverShop_OrderItem', '"SilverShop_Product_OrderItem"."ID" = "SilverShop_OrderItem"."ID"');
        $records = $q->execute();
        $productssold = $ps->buildDataObjectSet($records, "DataObjectSet", $q, Product::class);
        //TODO: this could be done faster with an UPDATE query (SQLQuery doesn't support this yet @11/06/2010)
        foreach ($productssold as $product) {
            if ($product->NewPopularity != $product->Popularity) {
                $product->Popularity = $product->NewPopularity;
                $product->writeToStage('Stage');
                $product->publishSingle();
            }
        }
    }
legacy function  for working out popularity