-
Notifications
You must be signed in to change notification settings - Fork 7
/
DatabaseTableTrait.php
69 lines (63 loc) · 1.64 KB
/
DatabaseTableTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace luya\testsuite\traits;
/**
* Base Trait for Database Actions.
*
* @author Basil Suter <basil@nadar.io>
* @since 1.0.21
*/
trait DatabaseTableTrait
{
/**
* @return \yii\db\Connection
*/
public function getDatabaseComponent()
{
return $this->app->db;
}
/**
* Create a table if not exists
*
* @param string $table
* @param array $columns
*/
public function createTableIfNotExists($table, array $columns)
{
if ($this->getDatabaseComponent()->getTableSchema($table, true) === null) {
$this->getDatabaseComponent()->createCommand()->createTable($table, $columns)->execute();
}
}
/**
* Drop a table if not exists.
*
* @param string $table
*/
public function dropTableIfExists($table)
{
if ($this->getDatabaseComponent()->getTableSchema($table, true) !== null) {
$this->getDatabaseComponent()->createCommand()->dropTable($table)->execute();
}
}
/**
* Insert row
*
* @param string $table
* @param array $values
* @return integer returns the number of rows inserted.
*/
public function insertRow($table, array $values)
{
return $this->getDatabaseComponent()->createCommand()->insert($table, $values)->execute();
}
/**
* Delete row
*
* @param string $table
* @param array $condition
* @return integer returns the number of rows deleted.
*/
public function deleteRow($table, array $condition)
{
return $this->getDatabaseComponent()->createCommand()->delete($table, $condition)->execute();
}
}