Trait luya\testsuite\traits\DatabaseTableTrait
Available since version | 1.0.21 |
---|---|
Source Code | https://github.com/luyadev/luya-testsuite/blob/master/src/traits/DatabaseTableTrait.php |
Base Trait for Database Actions.
Public Methods
Method | Description | Defined By |
---|---|---|
createTableIfNotExists() | Create a table if not exists | luya\testsuite\traits\DatabaseTableTrait |
deleteRow() | Delete row | luya\testsuite\traits\DatabaseTableTrait |
dropTableIfExists() | Drop a table if not exists. | luya\testsuite\traits\DatabaseTableTrait |
getDatabaseComponent() | luya\testsuite\traits\DatabaseTableTrait | |
insertRow() | Insert row | luya\testsuite\traits\DatabaseTableTrait |
Method Details
Create a table if not exists
public void createTableIfNotExists ( $table, array $columns ) | ||
$table | string | |
$columns | array |
public function createTableIfNotExists($table, array $columns)
{
if ($this->getDatabaseComponent()->getTableSchema($table, true) === null) {
$this->getDatabaseComponent()->createCommand()->createTable($table, $columns)->execute();
}
}
Delete row
public integer deleteRow ( $table, array $condition ) | ||
$table | string | |
$condition | array | |
return | integer |
Returns the number of rows deleted. |
---|
public function deleteRow($table, array $condition)
{
return $this->getDatabaseComponent()->createCommand()->delete($table, $condition)->execute();
}
Drop a table if not exists.
public void dropTableIfExists ( $table ) | ||
$table | string |
public function dropTableIfExists($table)
{
if ($this->getDatabaseComponent()->getTableSchema($table, true) !== null) {
$this->getDatabaseComponent()->createCommand()->dropTable($table)->execute();
}
}
public yii\db\Connection getDatabaseComponent ( ) |
public function getDatabaseComponent()
{
return $this->app->db;
}