1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 10:15:28 +02:00

Add a new $database->columnExists() method, plus a couple other unrelated minor adjustments

This commit is contained in:
Ryan Cramer
2020-04-10 12:40:15 -04:00
parent ac7c9da4d1
commit 48bb7c1734
3 changed files with 58 additions and 4 deletions

View File

@@ -61,9 +61,9 @@
* @property int $published Unix timestamp of when the page was published. #pw-group-common #pw-group-date-time #pw-group-system
* @property string $publishedStr Date/time when the page was published (formatted date/time string). #pw-group-date-time
* @property int $created_users_id ID of created user. #pw-group-system
* @property User $createdUser The user that created this page. Returns a User or a NullPage.
* @property User|NullPage $createdUser The user that created this page. Returns a User or a NullPage.
* @property int $modified_users_id ID of last modified user. #pw-group-system
* @property User $modifiedUser The user that last modified this page. Returns a User or a NullPage.
* @property User|NullPage $modifiedUser The user that last modified this page. Returns a User or a NullPage.
* @property PagefilesManager $filesManager The object instance that manages files for this page. #pw-group-files
* @property string $filesPath Get the disk path to store files for this page, creating it if it does not exist. #pw-group-files
* @property string $filesUrl Get the URL to store files for this page, creating it if it does not exist. #pw-group-files

View File

@@ -2970,7 +2970,7 @@ class Sanitizer extends Wire {
*/
public function date($value, $format = null, array $options = array()) {
$defaults = array(
'returnFormat' => $format, // date format to return in, if different from $dateFormat
'returnFormat' => $format, // date format to return in, if different from $format
'min' => '', // Minimum date allowed (in $dateFormat format, or a unix timestamp)
'max' => '', // Maximum date allowed (in $dateFormat format, or a unix timestamp)
'default' => null, // Default value, if date didn't resolve
@@ -2978,13 +2978,14 @@ class Sanitizer extends Wire {
);
$options = array_merge($defaults, $options);
$datetime = $this->wire('datetime');
$iso8601 = 'Y-m-d H:i:s';
$_value = trim($value); // original value string
if(empty($value)) return $options['default'];
if(!is_string($value) && !is_int($value)) $value = $this->string($value);
if(ctype_digit("$value")) {
// value is in unix timestamp format
// make sure it resolves to a valid date
$value = strtotime(date('Y-m-d H:i:s', (int) $value));
$value = strtotime(date($iso8601, (int) $value));
} else {
/** @var WireDateTime $datetime */
$value = $datetime->stringToTimestamp($value, $format);

View File

@@ -627,6 +627,59 @@ class WireDatabasePDO extends Wire implements WireDatabase {
return !empty($result);
}
/**
* Does the given column exist in given table?
*
* ~~~~~
* // Standard usage:
* if($database->columnExists('pages', 'name')) {
* echo "The pages table has a 'name' column";
* }
*
* // You can also bundle table and column together:
* if($database->columnExists('pages.name')) {
* echo "The pages table has a 'name' column";
* }
*
* $exists = $database->columnExists('pages', 'name', true);
* if($exists) {
* // associative array with indexes: Name, Type, Null, Key, Default, Extra
* echo "The pages table has a 'name' column and here is verbose info: ";
* print_r($exists);
* }
* ~~~~~
*
* #pw-group-custom
*
* @param string $table Specify table name (or table and column name in format "table.column").
* @param string $column Specify column name (or omit or blank string if already specified in $table argument).
* @param bool $getInfo Return array of column info (with type info, etc.) rather than boolean?
* @return bool|array
* @since 3.0.154
* @throws WireDatabaseException
*
*/
public function columnExists($table, $column = '', $getInfo = false) {
if(empty($column)) {
if(!strpos($table, '.')) throw new WireDatabaseException('No column specified');
list($table, $column) = explode('.', $table, 2);
}
$exists = false;
$table = $this->escapeTable($table);
try {
$query = $this->prepare("SHOW COLUMNS FROM `$table` WHERE Field=:column");
$query->bindValue(':column', $column, \PDO::PARAM_STR);
$query->execute();
$numRows = (int) $query->rowCount();
if($numRows) $exists = $getInfo ? $query->fetch(\PDO::FETCH_ASSOC) : true;
$query->closeCursor();
} catch(\Exception $e) {
// most likely given table does not exist
$exists = false;
}
return $exists;
}
/**
* Is the given string a database comparison operator?
*