Skip to content

Product model repository :  #1

Description

@Genaker

public function scopeEAVMonster($q, $ids){

$entity_id = 'row_id';
$ids = implode(',', $ids);
$monster = <<<EOD

select ea.attribute_id, ea.attribute_code, eav.value
FROM catalog_product_entity e
JOIN catalog_product_entity_varchar eav
ON e.$entity_id = eav.$entity_id
JOIN eav_attribute ea
ON eav.attribute_id = ea.attribute_id
WHERE e.$entity_id in ($ids)
UNION
SELECT ea.attribute_id, ea.attribute_code, eav.value
FROM catalog_product_entity e
JOIN catalog_product_entity_int eav
ON e.$entity_id = eav.$entity_id
JOIN eav_attribute ea
ON eav.attribute_id = ea.attribute_id
WHERE e.$entity_id in ($ids)
UNION
SELECT ea.attribute_id, ea.attribute_code, eav.value
FROM catalog_product_entity e
JOIN catalog_product_entity_decimal eav
ON e.$entity_id = eav.$entity_id
JOIN eav_attribute ea
ON eav.attribute_id = ea.attribute_id
WHERE e.entity_id in ($ids)
UNION
SELECT ea.attribute_id, ea.attribute_code, eav.value
FROM catalog_product_entity e
JOIN catalog_product_entity_datetime eav
ON e.$entity_id = eav.$entity_id
JOIN eav_attribute ea
ON eav.attribute_id = ea.attribute_id
WHERE e.$entity_id in ($ids)
UNION
SELECT ea.attribute_id, ea.attribute_code, eav.value
FROM catalog_product_entity e
JOIN catalog_product_entity_text eav
ON e.$entity_id = eav.$entity_id
JOIN eav_attribute ea
ON eav.attribute_id = ea.attribute_id
WHERE e.$entity_id in ($ids)
EOD;

echo $monster;

return DB::select(DB::raw($monster));
}

public function scopeSetStoreId($query, $storeId){
	$this->storeId = $storeId;
	echo "\n Set Store ID: $storeId\n";
	return $query;
}

public function scopeWithVarchar($query)
{
	echo "\n Get Store Id: $this->storeId \n";
		return $query->with( ['catalog_product_entity_varchars' => function ($query){
    		$query->leftJoinRelationship('eav_attribute')->addSelect(['eav_attribute.attribute_code'])
	    	->where('store_id', '=', $this->storeId);
	}
	]);
}
public function scopeWithInt($query)
    {
            return $query->with( ['catalog_product_entity_ints' => function ($query){
                    $query->leftJoinRelationship('eav_attribute')->addSelect(['eav_attribute.attribute_code'])
		->where('store_id', '=', $this->storeId);
                    }
            ]);
    }

public function scopeWithText($query)
    {
            return $query->with( ['catalog_product_entity_texts' => function ($query){
                    $query->leftJoinRelationship('eav_attribute')->addSelect(['eav_attribute.attribute_code'])
		->where('store_id', '=', $this->storeId);
                    }
            ]);
    }

public function scopeWithDecimal($query)
    {
            return $query->with( ['catalog_product_entity_decimals' => function ($query){
                    $query->leftJoinRelationship('eav_attribute')->addSelect(['eav_attribute.attribute_code'])
		->where('store_id', '=', $this->storeId);
                    }
            ]);
    }

public function scopeWithGallary($query)
    {
            return $query->with( ['catalog_product_entity_media_gallery_values' => function ($query){
            	$query->where('store_id', '=', $this->storeId);
	/*        
	$query->leftJoinRelationship('catalog_product_entity_media_gallery_values')->addSelect(['catalog_product_entity_media_gallery_values.*']);
              */
	      }
            ]);
    }

public function scopeWithPrice($query, $group = 0, $website = 0){

	return $query->with(['catalog_product_index_prices' =>  function($q) use ($group,$website){ 
		$q->where([
			['customer_group_id', '=', $group],
			['website_id', '=', $website]
			]);
	}
	]);
}

public  function scopeMergeEAV(){
	var_dump($this);
	/*
	$eav = [];

	foreach($collection as $product)
	 {
		var_dump($product);
		$eav[] = $product->catalog_product_entity_decimals;
		$eav[] = $product->catalog_product_entity_ints;
		$eav[] = $product->catalog_product_entity_varchar;
		$eav[] = $product->catalog_product_entity_texts;
	}

	var_dump($eav);*/
}

public function scopeGetEAVCache(){
$sql = <<<EOD

select distinct ea.attribute_id, ea.attribute_code, 'varchar' as type from catalog_product_entity_varchar eav right JOIN eav_attribute ea ON eav.attribute_id = ea.attribute_id
UNION select distinct ea.attribute_id, ea.attribute_code, 'int' as type from catalog_product_entity_int eav right JOIN eav_attribute ea ON eav.attribute_id = ea.attribute_id
EOD;
$attributes = DB::select(DB::raw($sql));

$attr = [];

foreach($attributes as $e) {
 $attr[$e->attribute_code] = $e->attribute_id;
}

$this->attrCache=$attr;
return $attr;
}

public function scopeAttr($q, $attribute_code){

// var_dump($this->catalog_product_entity_varchars->toArray());
$key = $this->catalog_product_entity_varchars->search(function($a) use ($attribute_code){ return $a->attribute_code === $attribute_code; });
return $this->catalog_product_entity_varchars[$key];
}

public function scopeTrans($q){

// $s = microtime(true);
$linarResult = &$this;

/* foreach($this->attributes as $name => $value){
$linarResult[$name] = $value;
}
*/

foreach ($this->attributeTables as $table){
	if (isset($this->$table)) { //echo $this->$o;
		foreach ($this->$table as $attribute){
		$linarResult[$attribute->attribute_code] = $attribute->value;
		}
		unset($this->$table);
	}
}

// $end = microtime(true); echo "Execution time = " . ($end - $s);

return $linarResult;
}

public function scopeToPG($q, $productData){

config(['database.connections.pgsql.host' => 'localhost']);
config(['database.connections.pgsql.port' => '5432']);
config(['database.connections.pgsql.database' => 'magento']);
config(['database.connections.pgsql.username' => 'postgres']);
config(['database.connections.pgsql.password' => '']);
$pg = DB::connection('pgsql');


foreach($productData as $product){
	$pg->table('product')->upsert(
			['id' => $product->row_id,'data' => $product->toJson()], ['id']
	);
}

DB::disconnect('mysql_source');
}

public function scopeProduct($q){

config(['database.connections.pgsql.host' => 'localhost']);
    config(['database.connections.pgsql.port' => '5432']);
    config(['database.connections.pgsql.database' => 'magento']);
    config(['database.connections.pgsql.username' => 'postgres']);
    config(['database.connections.pgsql.password' => '']);
 	$pg = DB::connection('pgsql')->table('product');

//$start = microtime(true); echo "Execution time = " . ($end - $s);

//$result = $pg->table('product')->select()->get();

//DB::disconnect('mysql_source');
    //$end = microtime(true); echo "JSON select Execution time = " . ($end - $start);

return $pg;
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions