简体   繁体   中英

PDO method for constructing complex MySQL queries with many selects and joins?

I have a set of product associated attributes objects that are modeled differently in the MySQL tables, depending on their data type ( varchar , int , etc) and whether the product can have multiple values for that attribute or only a single value.

I'm trying to write code that will allow me to get a product's (or set of products') attributes and values. I'm thinking that the responsibility for creating associated SQL would be the responsibility of the attributes class.

I haven't used much PDO before, except in other people's code where it's been abstracted away by several levels. Here I'm building everything myself, so I'm not sure how it should work.

class attributes {

  public function getSql(PDOStatement $sth) {
     // some code to modify the PDOStatement so that it adds the required "SELECT" and "JOIN" clauses to it
    return $sth;
  }

}

class products {
  protected $attributes = array(); // attribute_name => attributes object
  public function addAttributeForLoad(attributes $attribute) {
    $this->attributes[$attribute->get('name')] = $attribute;
    return $this;
  }
  public function loadAttributeValues() {
    foreach($this->attributes as $attribute) {
      $this->dbh->prepare($attribute->getSql());
    }
  }
}

Obviously the loadAttributeValues function is totally (?) wrong. I'm just trying to communicate the idea that somehow I add to the SQL from each of the user/system-selected attributes. I know Zend has functionality like that but this is plain PHP.

EDIT

I've been asked to provide more detail, so here it is.

The database is an EAV structure, with a main product table that contains product id, sku, and other "core" attributes with a one-to-one relationship to the product.

Other tables contain various attributes that must be joined to that one on product_id in order to get other product data.

Eg

tbl_product_color contains product_id, color_id tbl_product_smell contains product_id, product_smell_id

I may want both attributes, or perhaps just one of them. My SQL would look like:

SELECT t1.product_id, t1.sku, t3.product_smell
FROM tbl_products t1
LEFT JOIN tbl_product_smell t2 ON t1.id = t2.product_id
LEFT JOIN tbl_smells t3 ON t2.product_smell_id = t3.id
WHERE t1.updated BETWEEN 2014-01-20 AND NOW()
AND t2.product_smell_id IN (13, 17, 19, 23, 29)

Now, I know I can just put that query into PDO. But I'm wondering if it has some native methods to add to the "SELECT" part, the "FROM/JOIN" part, and the "WHERE" part.

Ideally, I would have a product collection object, not yet loaded, to which I could add attributes, and it would handle those 3 elements (select, from/join, where).

Eg

$attribute = attributes::getByName('product_smell');
$limit_by = array('value','in',array('strong','medium','extra stinky')); // array containing 1.) compare to value or id, 2.) operator (in, nin, eq, neq, gt, lt, etc), 3.) operand

$product_collection->addAttribute($attribute,$limit_by);

$product_collection->load();

foreach($product_collection as $product) {
  // do stuff like display on screen or whatever
}

The sql needs to be build up in parts. In another application, I've written code that does this "manually", and once the query is build up it is just sent using deprecated mysql_query . I was just wondering if PDO has the ability to add to SELECT, FROM/JOIN, and WHERE clauses in such a way, or at least to make it simpler.

I'm wondering if it has some native methods to add to the "SELECT" part, the "FROM/JOIN" part, and the "WHERE" part.

No.

PDO doesn't construct your queries, it runs them. For the query builder look for Doctrine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM