function _insertRelated(&$products,$type='related'){
$values = array();
$totalValid=0;
$insert = 'INSERT IGNORE INTO '.hikashop_table('product_related').' (`product_related_id`,`product_related_type`,`product_id`) VALUES (';
$ids=array();
foreach($products as $product){
if(!isset($product->$type) && empty($product->hikashop_update)){
//copy files from template if any
if(@$product->product_type!='variant' && !empty($this->template->$type)){
foreach($this->template->$type as $id){
$value = array((int)$id,$this->db->Quote($type),$product->product_id);
$values[] = implode(',',$value);
$totalValid++;
if( $totalValid && $totalValid%$this->perBatch == 0){
$this->db->setQuery($insert.implode('),(',$values).')');
$this->db->query();
$totalValid=0;
$values=array();
}
}
}
}elseif(isset($product->$type)&&is_array($product->$type)){
$ids[] = (int)$product->product_id;
foreach($product->$type as $k => $id){
if(!empty($id)){
$id = $this->_getRelated($id);
$product->{$type}[$k] = $id;
$value = array((int)$id,$this->db->Quote($type),$product->product_id);
$values[] = implode(',',$value);
$totalValid++;
}
if( $totalValid && $totalValid%$this->perBatch == 0){
if(!empty($ids)){
$this->db->setQuery('DELETE FROM '.hikashop_table('product_related').' WHERE product_id IN ('.implode(',',$ids).') AND product_related_type='.$this->db->Quote($type));
$this->db->query();
$ids=array();
}
if(!empty($id)){
$this->db->setQuery($insert.implode('),(',$values).')');
$this->db->query();
}
$totalValid=0;
$values=array();
}
}
}
}
if(!empty($ids)){
$this->db->setQuery('DELETE FROM '.hikashop_table('product_related').' WHERE product_id IN ('.implode(',',$ids).') AND product_related_type='.$this->db->Quote($type));
$this->db->query();
}
if(count($values)){
$this->db->setQuery($insert.implode('),(',$values).')');
$this->db->query();
}
}