Skip to content

Commit

Permalink
Code refactoring for Database
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandra Nantel committed Jun 13, 2018
1 parent 1f5602b commit eba40c2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 38 deletions.
1 change: 1 addition & 0 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<releases>
<release version="3.0.0" date="TBA" min="4.0.0" max="4.x.x" php-min="5.6.x" php-max="7.x.x">
- Update for Symphony 4.x
- Code refactoring for Database and EQFA
</release>
<release version="2.3.3" date="2017-03-22" min="2.3" max="2.x.x">
- Supported on PHP 7
Expand Down
133 changes: 95 additions & 38 deletions fields/field.image_preview_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,19 @@ public function commit()
$settings['entry-absolute'] = empty($e_absolute) ? 'no' : $e_absolute;

// DB
$tbl = self::FIELD_TBL_NAME;

Symphony::Database()->query("DELETE FROM `$tbl` WHERE `field_id` = '$id' LIMIT 1");
Symphony::Database()
->delete(self::FIELD_TBL_NAME)
->where(['field_id' => $id])
->limit(1)
->execute()
->success();

// return if the SQL command was successful
return Symphony::Database()->insert($settings, $tbl);
return Symphony::Database()
->insert(self::FIELD_TBL_NAME)
->values($settings)
->execute()
->success();
}


Expand Down Expand Up @@ -490,42 +497,92 @@ public function tearDown()
*/
public function createTable()
{
return Symphony::Database()->query(
"CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`entry_id` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
KEY `entry_id` (`entry_id`)
) TYPE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
);
return Symphony::Database()
->create('tbl_entries_data_' . $this->get('id'))
->ifNotExists()
->charset('utf8')
->collate('utf8_unicode_ci')
->fields([
'id' => [
'type' => 'int(11)',
'auto' => true,
],
'entry_id' => 'int(11)',
])
->keys([
'id' => 'primary',
'entry_id' => 'key',
])
->execute()
->success();
}

/**
* Creates the table needed for the settings of the field
*/
public static function createFieldTable()
{
$tbl = self::FIELD_TBL_NAME;

return Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `$tbl` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`field_id` INT(11) UNSIGNED NOT NULL,
`field-handles` VARCHAR(255) NOT NULL,
`table-width` INT(11) UNSIGNED NULL,
`table-height` INT(11) UNSIGNED NULL,
`table-resize` INT(11) UNSIGNED NULL,
`table-position` INT(11) UNSIGNED NULL,
`table-absolute` ENUM('yes','no') NOT NULL DEFAULT 'no',
`entry-width` INT(11) UNSIGNED NULL,
`entry-height` INT(11) UNSIGNED NULL,
`entry-resize` INT(11) UNSIGNED NULL,
`entry-position` INT(11) UNSIGNED NULL,
`entry-absolute` ENUM('yes','no') NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
return Symphony::Database()
->create(self::FIELD_TBL_NAME)
->ifNotExists()
->charset('utf8')
->collate('utf8_unicode_ci')
->fields([
'id' => [
'type' => 'int(11)',
'auto' => true,
],
'field_id' => 'int(11)',
'field-handles' => 'varchar(255)',
'table-width' => [
'type' => 'int(11)',
'null' => true,
],
'table-height' => [
'type' => 'int(11)',
'null' => true,
],
'table-resize' => [
'type' => 'int(11)',
'null' => true,
],
'table-position' => [
'type' => 'int(11)',
'null' => true,
],
'table-absolute' => [
'type' => 'enum',
'values' => ['yes','no'],
'default' => 'no',
],
'entry-width' => [
'type' => 'int(11)',
'null' => true,
],
'entry-height' => [
'type' => 'int(11)',
'null' => true,
],
'entry-resize' => [
'type' => 'int(11)',
'null' => true,
],
'entry-position' => [
'type' => 'int(11)',
'null' => true,
],
'entry-absolute' => [
'type' => 'enum',
'values' => ['yes','no'],
'default' => 'no',
],
])
->keys([
'id' => 'primary',
'field_id' => 'key',
])
->execute()
->success();
}


Expand All @@ -535,10 +592,10 @@ public static function createFieldTable()
*/
public static function deleteFieldTable()
{
$tbl = self::FIELD_TBL_NAME;

return Symphony::Database()->query("
DROP TABLE IF EXISTS `$tbl`
");
return Symphony::Database()
->drop(self::FIELD_TBL_NAME)
->ifExists()
->execute()
->success();
}
}

0 comments on commit eba40c2

Please sign in to comment.