/**
  * Retrieve all objects matching array of passed property/value pairs
  *
  * @param PDO object $db Optional PDO object for recycling existing connections
  *
  * @param array $properties Array of class property names and values to filter on
  *
  * @param array $order Array of class property names to order results by
  *
  * @param array $direction String Order 'asc' or 'desc' - Only does something if an array
  *						was passed for $order parameter.
  *
  * @param array $limit Array containing one or two elements. If a single element is passed,
  *					   it will return a dataset of that size or less. If two elements are
  *					   set, the first will be an offset and the second a range
  *        examples: The following would return a thousand records following the first 100:
  *        		    array(0 => 100, 1 => 1000)
  *
  *					The following would simply return 700 records:
  *					array(0 => 700)
  *
  *
  * @return Array of matching objects or false if not found
  */
 public static function getWith($db = null, $properties, $order = array(), $direction = 'asc', $limit = array())
 {
     //Dynamically find class name and ensure it exists
     $peer_class = preg_replace('/Collection$/', '', get_called_class());
     if (class_exists($peer_class) && $properties != array()) {
         //Build the basic select query
         $peer_object = new $peer_class();
         if ($db == null) {
             $db = self::fetchDB();
         }
         $cols = array();
         foreach ($properties as $property => $value) {
             $col_name = Dinkly::convertFromCamelCase($property);
             if (array_key_exists($col_name, $peer_object->registry)) {
                 $cols[$col_name] = $value;
             }
         }
         $where = '';
         $is_valid = false;
         foreach ($cols as $col => $value) {
             if (is_array($value) && count($value) > 1) {
                 $in_string = '';
                 foreach ($value as $temp) {
                     $in_string .= $db->quote($temp) . ',';
                 }
                 $in_string = trim($in_string, ',');
                 $where .= ' AND `' . $col . '` IN (' . $in_string . ')';
                 $is_valid = true;
             } elseif (is_array($value) && count($value) === 1) {
                 $where .= ' AND `' . $col . '` = ' . $db->quote($value[0]);
                 $is_valid = true;
             } elseif (!is_array($value)) {
                 $where .= ' AND `' . $col . '` = ' . $db->quote($value);
                 $is_valid = true;
             }
         }
         if ($is_valid) {
             $where = ' where ' . trim($where, ' AND');
         }
         //Enforce an order on the results
         $is_valid = false;
         if ($order != array()) {
             $chunk = ' order by ';
             foreach ($order as $p) {
                 $col_name = Dinkly::convertFromCamelCase($p);
                 if (array_key_exists($col_name, $peer_object->registry)) {
                     $chunk .= $col_name . ', ';
                     $is_valid = true;
                 }
             }
             if ($is_valid) {
                 $where .= rtrim($chunk, ', ');
                 if ($direction == 'asc') {
                     $where .= ' ASC';
                 } else {
                     if ($direction == 'desc') {
                         $where .= ' DESC';
                     }
                 }
             }
         }
         //Enforce a limit on the results
         $is_valid = false;
         if ($limit != array()) {
             $chunk = " limit ";
             if (is_numeric($limit[0])) {
                 $chunk .= $limit[0];
                 $is_valid = true;
             }
             if (isset($limit[1])) {
                 if (is_numeric($limit[1])) {
                     $chunk .= ', ' . $limit[1];
                 }
             }
             if ($is_valid) {
                 $where .= $chunk;
             }
         }
         return self::getCollection($peer_object, $peer_object->getSelectQuery() . $where, $db);
     }
 }
Пример #2
0
 /**
  * Initialize object with values other than id
  *
  * @param array $properties Array of class property names and values to filter on 
  * 
  * @return hydrates object and returns true if exist in DB or returns false if not found
  */
 public function initWith($properties = array())
 {
     if (!$this->db) {
         throw new Exception("Unable to perform init without a database object");
     }
     if ($properties != array()) {
         $cols = array();
         foreach ($properties as $property => $value) {
             $col_name = Dinkly::convertFromCamelCase($property);
             if (array_key_exists($col_name, $this->registry)) {
                 $cols[$col_name] = $value;
             }
         }
         $where = '';
         foreach ($cols as $col => $value) {
             $where .= ' AND `' . $col . '` = ' . $this->db->quote($value);
         }
         $where = ' where ' . trim($where, ' AND');
         $query = $this->getSelectQuery() . $where;
         $result = $this->db->query($query)->fetchAll();
         if ($result != array()) {
             $this->hydrate($result, true);
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
Пример #3
0
 /**
  *  Drop table from database completely if it exists
  *
  * @param string $schema String name of schema containing model
  * @param string $model_name String name of model yaml file 
  *
  * @return bool false if table does not exist, true if table dropped
  */
 public static function dropTable($schema, $model_name, $override_database_name = null)
 {
     if (!DinklyDataConfig::setActiveConnection($schema)) {
         return false;
     }
     //Use the proper DB credentials, or apply a passed-in override
     $creds = DinklyDataConfig::getDBCreds();
     $name = $creds['name'];
     //Connect to the target db
     $creds['name'] = $name;
     $db = self::fetchDB($creds);
     //Craft the sql
     $table_name = self::sanitize($db, Dinkly::convertFromCamelCase($model_name));
     $sql = "DROP TABLE IF EXISTS " . $table_name;
     //Drop the table
     $db->exec($sql);
     return true;
 }