Help with Where Clause Checking if an Upload Exists

2 posts by 2 authors in: Forums > CMS Builder
Last Post: June 24, 2013   (RSS)

By Jason - June 24, 2013

Hi,

In this case, the "images" column is a pseudo field, as it does not actually exist in the products table.  What we can do is figure out which products have uploads by querying the uploads table and then  using this list of numbers to retrieve a product record.

First, we can get all the products num with uploads like this:

// load 'categories'
  list($categoriesRecords, $categoriesMetaData) = getRecords(array(
    'tableName'   => 'categories',
    'loadUploads' => false,
    'allowSearch' => false,
  ));
  
  $productNumsWithUploads = array_pluck(mysql_select("uploads", "tableName = 'products' AND fieldName = 'images'"), 'recordNum');

Next, we can use this in our WHERE clause like this:

list($productsRecords, $productsMetaData) = getRecords(array(
        'tableName'   => 'products',
        'limit'       => '1',
        'orderBy'     => 'RAND()',
        'loadUploads' => true,
        'allowSearch' => false,
        'where'     => "num IN (".mysql_escapeCSV($productNumsWithUploads).")  AND category LIKE '%\t".$cat."%\t'",
         'debugSql'      => '1',
    ));

This should get you the results you are looking for. Let me know if you have any other questions.

Thanks

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/