DoctrineHelper::dropTables()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace kujaff\VersionsBundle\Model;
4
5
/**
6
 * Helps your using Doctrine
7
 * Just needs a property ContainerInterface $container
8
 */
9
trait DoctrineHelper
10
{
11
    /**
12
     * Execute a DQL query (only for SELECT, UPDATE or DELETE)
13
     *
14
     * @param string $dql
15
     * @param array $parameters
16
     * @return mixed
17
     */
18
    protected function executeDQL($dql, array $parameters = array())
19
    {
20
        $em = $this->container->get('doctrine')->getManager();
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
        $query = $em->createQuery($dql);
22
        foreach ($parameters as $name => $value) {
23
            $query->setParameter($name, $value);
24
        }
25
        return $query->getResult();
26
    }
27
28
    /**
29
     * Execute raw SQL
30
     *
31
     * @param string $sql
32
     * @param array $parameters
33
     * @return \Doctrine\DBAL\Statement
34
     */
35
    protected function executeSQL($sql, array $parameters = array())
36
    {
37
        $em = $this->container->get('doctrine')->getManager();
38
        $stmt = $em->getConnection()->prepare($sql);
39
        foreach ($parameters as $name => $value) {
40
            $stmt->bindValue($name, $value);
41
        }
42
        $stmt->execute();
43
        return $stmt;
44
    }
45
46
    /**
47
     * Drop tables if exists
48
     *
49
     * @param array $tables
50
     */
51
    protected function dropTables(array $tables)
52
    {
53
        foreach ($tables as $table) {
54
            $this->executeSQL('DROP TABLE IF EXISTS ' . $table);
55
        }
56
    }
57
}
58