summaryrefslogtreecommitdiffstats
path: root/library/Cube/Ido/ZfSelectWrapper.php
blob: 745d7a55b86b929ebfd591454d3406a0d13e093a (plain)
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
70
71
72
73
74
75
76
77
<?php

namespace Icinga\Module\Cube\Ido;

/**
 * Since version 1.1.0 we're using the monitoring module's queries as the cubes' base queries.
 * Before, the host object table was available using the alias 'o'. Now it's 'ho'.
 * Without this wrapper, the action link hook provided by the director would fail because it relies on the alias 'o'.
 */
class ZfSelectWrapper
{
    /** @var \Zend_Db_Select */
    protected $select;

    public function __construct(\Zend_Db_Select $select)
    {
        $this->select = $select;
    }

    /**
     * Get the underlying Zend_Db_Select query
     *
     * @return \Zend_Db_Select
     */
    public function unwrap()
    {
        return $this->select;
    }

    /**
     * {@see \Zend_Db_Select::reset()}
     */
    public function reset($part = null)
    {
        $this->select->reset($part);

        return $this;
    }

    /**
     * {@see \Zend_Db_Select::columns()}
     */
    public function columns($cols = '*', $correlationName = null)
    {
        if (is_array($cols)) {
            foreach ($cols as $alias => &$col) {
                if (substr($col, 0, 2) === 'o.') {
                    $col = 'ho.' . substr($col, 2);
                }
            }
        }

        return $this->select->columns($cols, $correlationName);
    }

    /**
     * Proxy Zend_Db_Select method calls
     *
     * @param string $name      The name of the method to call
     * @param array  $arguments Arguments for the method to call
     *
     * @return mixed
     *
     * @throws \BadMethodCallException If the called method does not exist
     */
    public function __call($name, array $arguments)
    {
        if (! method_exists($this->select, $name)) {
            $class = get_class($this);
            $message = "Call to undefined method $class::$name";

            throw new \BadMethodCallException($message);
        }

        return call_user_func_array([$this->select, $name], $arguments);
    }
}