Skip to content

Commit

Permalink
busueda super eficiente y resultados agrupados
Browse files Browse the repository at this point in the history
  • Loading branch information
Lay committed Nov 9, 2013
1 parent 8bae829 commit 6911502
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 147 deletions.
4 changes: 2 additions & 2 deletions app/Controller/ProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public function products_search_by_attributes()

$productSearch = new ProductSearch(
$product_description[0],
'0',
$product_description[1]
$product_description[1],
$product_description[2]
);

$result = $this->Product->search_by_attributes($productSearch);
Expand Down
18 changes: 18 additions & 0 deletions app/Controller/SupplierServicesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,22 @@ public function beforeFilter()
$this->Auth->allow('newOnlineRequest');
}

/**
* newOnlineRequest method
*
* @return void
*/
public function search_by_product_type()
{
$this->autoRender = false;

$product_description = $this->request->data;

$category = $this->request->data[0];
$product_type = $this->request->data[1];

$result = $this->Supplier->search_by_product_type($category, $product_type);
echo json_encode($result);
}

}
15 changes: 15 additions & 0 deletions app/Lib/ProductResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
class ProductResult {
var $id;
var $manufacturer_id;
var $attributes; //[i][0] id del tipo del atributo, [i][1] nombre del atributo, [i][2] valor del atributo

function ProductResult($id, $manufacturer_id)
{
$this->id = $id;
$this->manufacturer_id = $manufacturer_id;
$this->attributes = array();
}

}
?>
157 changes: 116 additions & 41 deletions app/Model/Product.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
App::uses('AppModel', 'Model');
App::uses('ProductSearch', 'Lib');
App::uses('ProductResult', 'Lib');
/**
* Product Model
*
Expand Down Expand Up @@ -179,7 +180,31 @@ public function search_by_attributes($search)
$preparation = $this->search_by_attributes_preparation($search);
$db = $this->getDataSource();
// return $preparation['query'];
return $db->fetchAll($preparation['query'], $preparation['values']);
$result = $db->fetchAll($preparation['query'], $preparation['values']);
$results = array();
$current_product_id = 0;
$aux = null;
// $line = $result[0][0];
// return array($line['data_type_id'], $line['name'], $line['value']);
for($i = 0; $i < count($result); $i++)
{
$line = $result[$i][0];
if($current_product_id == 0 || $current_product_id != $line['id'])
{
if($current_product_id != 0)
{
array_push($results, $aux);
}
$current_product_id = $line['id'];
$aux = new ProductResult($line['id'], $line['manufacturer_id']);
}
array_push($aux->attributes, array($line['data_type_id'], $line['name'], $line['value']));
}
if($aux != null)
{
array_push($results, $aux);
}
return $results;
}

public function search_by_attributes_preparation($productSearch)
Expand All @@ -195,73 +220,123 @@ public function search_by_attributes_preparation($productSearch)

if($productSearch->category == '')
{
return $this->attributes_no_category_preparation($not_empty_attributes);
return $this->attributes_no_category_preparation($productSearch->type, $not_empty_attributes);
}else
{
return $this->attributes_category_preparation($productSearch->category, $not_empty_attributes);
return $this->attributes_category_preparation($productSearch->category, $productSearch->type, $not_empty_attributes);
}
}

public function attributes_category_preparation($category, $not_empty_attributes)
public function attributes_category_preparation($category, $product_type, $not_empty_attributes)
{
$query = "select p.id, p.manufacturer_id ";
$query .= "from products as p, products_suppliers as ps, categories_suppliers as c_s ";
$query = "select p.id, p.manufacturer_id, name, data_type_id, value ";
$query .= "from products as p, attributes as a, attributes_products as ap ";
$query .= "WHERE p.id in ";
$query .= "(select suppliers_filter.p_id ";
$query .= "from ";
$query .= "(select attributes_filter.p_id as p_id, ps.supplier_id as s_id ";
$query .= "from (";
$query .= "select ap.product_id as p_id ";
$query .= "from ";
$query .= "( ";
$query .= "select * ";
$query .= "from attributes ";
$query .= $this->list_of_ids_place_holders($not_empty_attributes);;
$query .= ") as a ";
$query .= "inner join ";
$query .= "attributes_products as ap ";
$query .= "on a.id = ap.attribute_id ";
$query .= $this->list_of_values_place_holders($not_empty_attributes) . " ";
$query .= "group by p_id ";
$query .= "Having Count(ap.product_id) >= ". count($not_empty_attributes);
$query .= ") as attributes_filter, ";
$query .= "products_suppliers as ps ";
$query .= "where attributes_filter.p_id = ps.product_id ";
$query .= ") as suppliers_filter, ";
$query .= "categories_suppliers as cs ";
$query .= "where ";
$query .= "p.id = ps.product_id AND ";
$query .= "c_s.supplier_id = ps.supplier_id AND ";
$query .= "c_s.category_id = ? ";
$query .= "cs.supplier_id = suppliers_filter.s_id AND ";
$query .= "cs.category_id = ? ";
$query .= ") ";
$query .= "AND p.type_id = ? AND ap.product_id = p.id AND ap.attribute_id = a.id ";
$query .= "ORDER BY p.id, attribute_id";

foreach ($not_empty_attributes as $key => $attribute)
{
$query .= "AND ";
$query .= "exists ( ";
$query .= "select * from attributes_products ";
$query .= "where ";
$query .= "attribute_id = ? AND ";
$query .= "product_id = p.id AND ";
$query .= "value = ? ";
$query .= ") ";
}
$values = $this->attributes_search_values($category, $product_type, $not_empty_attributes);
return array('query' => $query, 'values' => $values);
}

public function attributes_no_category_preparation($product_type, $not_empty_attributes)
{
$query = "select p.id, p.manufacturer_id, name, data_type_id, value ";
$query .= "from products as p, attributes as a, attributes_products as ap ";
$query .= "WHERE p.id in ";

$values = $this->attributes_search_values($category, $not_empty_attributes);
$query .= "(select attributes_filter.p_id as p_id ";
$query .= "from (";
$query .= "select ap.product_id as p_id ";
$query .= "from ";
$query .= "( ";
$query .= "select * ";
$query .= "from attributes ";
$query .= $this->list_of_ids_place_holders($not_empty_attributes);;
$query .= ") as a ";
$query .= "inner join ";
$query .= "attributes_products as ap ";
$query .= "on a.id = ap.attribute_id ";
$query .= $this->list_of_values_place_holders($not_empty_attributes) . " ";
$query .= "group by p_id ";
$query .= "Having Count(ap.product_id) >= ". count($not_empty_attributes);
$query .= ") as attributes_filter ";
$query .= ") ";

$query .= "AND p.type_id = ? AND ap.product_id = p.id AND ap.attribute_id = a.id ";
$query .= "ORDER BY p.id, attribute_id";

$values = $this->attributes_search_values('', $product_type, $not_empty_attributes);
return array('query' => $query, 'values' => $values);
}

public function attributes_no_category_preparation($not_empty_attributes)
public function list_of_ids_place_holders($attributes)
{
$query = "select p.id, p.manufacturer_id ";
$query .= "from products as p, products_suppliers as ps ";
$query .= "where ";
$query .= "p.id = ps.product_id ";
if (count($attributes) == 0) return '';

foreach ($not_empty_attributes as $key => $attribute)
$result = "where attributes.id in ( ?";
for($i = 0; $i < count($attributes)-1; $i++)
{
$query .= "AND ";
$query .= "exists ( ";
$query .= "select * from attributes_products ";
$query .= "where ";
$query .= "attribute_id = ? AND ";
$query .= "product_id = p.id AND ";
$query .= "value = ? ";
$query .= ") ";
$result .= ", ?";
}
$result .= ") ";
return $result;
}

$values = $this->attributes_search_values('', $not_empty_attributes);
return array('query' => $query, 'values' => $values);
public function list_of_values_place_holders($attributes)
{
if (count($attributes) == 0) return '';

$result = "WHERE ap.value = ?";
for($i = 0; $i < count($attributes)-1; $i++)
{
$result .= " OR ap.value = ?";
}
return $result;
}

public function attributes_search_values($category, $not_empty_attributes)
public function attributes_search_values($category, $type, $not_empty_attributes)
{
$values = array();
if($category != '')
foreach ($not_empty_attributes as $key => $value)
{
array_push($values, $category);
array_push($values, $key);
}
foreach ($not_empty_attributes as $key => $value)
{
array_push($values, $key);
array_push($values, $value);
}
if($category != '')
{
array_push($values, $category);
}
array_push($values, $type);
return $values;
}

Expand Down
16 changes: 15 additions & 1 deletion app/Model/Supplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,18 @@ class Supplier extends AppModel {
)
);

}
public function search_by_products($equivalencies, $products)
{
{
$preparation = $this->search_by_attributes_preparation($equivalencies, $products);
$db = $this->getDataSource();
// return $preparation['query'];
return $db->fetchAll($preparation['query'], $preparation['values']);
}
}

public function search_by_products_preparation($equivalencies, $products)
{

}
}

0 comments on commit 6911502

Please sign in to comment.