Esempio n. 1
0
 /**
  * Generate a unqiue text-identifier for a given table and column from any input string.
  *
  * Will generate a text identifier from the string (lowercase, alphanum-only), search for the value in the given table/column,
  * and keep incrementing a suffixed counter until the value is unique.
  *
  * @param \pdyn\database\DbDriverInterface $DB An active database connection.
  * @param string $input Any input text.
  * @param string $table The table in which to ensure uniqueness.
  * @param string $field The column in which to ensure uniqueness.
  * @param array $restriction_list Array of values the text-identifier cannot match.
  * @return string The generated, unique text identifier.
  */
 public static function generate_slug(\pdyn\database\DbDriverInterface $DB, $input, $table, $field, array $restriction_list = array())
 {
     $i = 0;
     while (true) {
         $slug = \pdyn\datatype\Text::make_slug($input);
         $slug .= $i != 0 ? '_' . $i : '';
         // This is so we don't add the increment on the first loop.
         if (!empty($restriction_list) && in_array($slug, $restriction_list, true)) {
             $i++;
             continue;
         }
         $found = $DB->get_record($table, [$field => $slug]);
         if (empty($found)) {
             break;
         }
         $i++;
     }
     return $slug;
 }
Esempio n. 2
0
 /**
  * Create an instance of the object by an identifying property.
  *
  * @param \pdyn\database\DbDriverInterface $DB A database connection.
  * @param string $property The property to select the object by. This is class properties not database fields.
  * @param mixed  $val The value of the property.
  * @param  boolean $mustexist Require that information for the property/value must exist, otherwise throw exception.
  * @return \pdyn\orm\DataObjectAbstract|bool The instantiated object for the passed information, or false if failure.
  */
 public static function instance_by(\pdyn\database\DbDriverInterface $DB, $property, $val, $mustexist = false)
 {
     $info = [$property => $val];
     $record = $DB->get_record(static::DB_TABLE, $info);
     if (empty($record)) {
         if ($mustexist === true) {
             $errmsg = sprintf('Could not find information for that value (%s/%s/%s)', static::DB_TABLE, $property, $id);
             throw new Exception($errmsg, Exception::ERR_BAD_REQUEST);
         } else {
             return false;
         }
     }
     return static::instance_from_record($DB, $record);
 }