<?php

include_once("../public/config.php");



//include all DAO files
require_once('../models/sql/Connection.class.php');
require_once('../models/sql/ConnectionFactory.class.php');
require_once('../models/sql/ConnectionProperty.class.php');
require_once('../models/sql/QueryExecutor.class.php');
require_once('../models/sql/Transaction.class.php');
require_once('../models/sql/SqlQuery.class.php');
require_once('../models/core/ArrayList.class.php');
require_once('../models/dao/DAOFactory.class.php');
//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');
//Programsetting
$Programsetting = new Programsetting();
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingEX = new ProgramsettingsMySqlExtDAO();


$newCatId = 1408;
$sql = "SELECT GROUP_CONCAT(distinct currentCat.productCatId SEPARATOR ', ')
        FROM productcat as currentCat
        left join productcat as child on (child.`productCatParent` = currentCat.productCatId and child.productCatId is Null)
        join product on product.productCatId = currentCat.productCatId
        where currentCat.productCatId != $newCatId
        GROUP BY 'all'";
//$results = $ProgramsettingEX->runSqlQueryGetSingleResult($sql);
$cats = R::getCell($sql);
//remove what ever get after last comma it may be uncomplete result
$cats = substr($cats, 0, strripos($cats, ","));
do {
    $myfile = fopen("sql.txt", "a+") or die("Unable to open file!");
    $txt = "update product set productCatId = $newCatId
where productCatId in ($cats)";
    R::exec($txt);


    fwrite($myfile, $txt . ";\r\n");



    fclose($myfile);
    $cats = R::getCell($sql);
    //remove what ever get after last comma it may be uncomplete result
    $cats = substr($cats, 0, strripos($cats, ","));
} while (!empty($cats))
?>