3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\ORM\Query\Filter;
22 use Doctrine\ORM\EntityManager,
23 Doctrine\ORM\Mapping\ClassMetaData,
24 Doctrine\ORM\Query\ParameterTypeInferer;
27 * The base class that user defined filters should extend.
29 * Handles the setting and escaping of parameters.
31 * @author Alexander <iam.asm89@gmail.com>
32 * @author Benjamin Eberlei <kontakt@beberlei.de>
35 abstract class SQLFilter
44 * Parameters for the filter.
50 * Constructs the SQLFilter object.
52 * @param EntityManager $em The EM
54 final public function __construct(EntityManager $em)
60 * Sets a parameter that can be used by the filter.
62 * @param string $name Name of the parameter.
63 * @param string $value Value of the parameter.
64 * @param string $type The parameter type. If specified, the given value will be run through
65 * the type conversion of this type. This is usually not needed for
66 * strings and numeric types.
68 * @return SQLFilter The current SQL filter.
70 final public function setParameter($name, $value, $type = null)
73 $type = ParameterTypeInferer::inferType($value);
76 $this->parameters[$name] = array('value' => $value, 'type' => $type);
78 // Keep the parameters sorted for the hash
79 ksort($this->parameters);
81 // The filter collection of the EM is now dirty
82 $this->em->getFilters()->setFiltersStateDirty();
88 * Gets a parameter to use in a query.
90 * The function is responsible for the right output escaping to use the
93 * @param string $name Name of the parameter.
95 * @return string The SQL escaped parameter to use in a query.
97 final public function getParameter($name)
99 if (!isset($this->parameters[$name])) {
100 throw new \InvalidArgumentException("Parameter '" . $name . "' does not exist.");
103 return $this->em->getConnection()->quote($this->parameters[$name]['value'], $this->parameters[$name]['type']);
107 * Returns as string representation of the SQLFilter parameters (the state).
109 * @return string String representation of the SQLFilter.
111 final public function __toString()
113 return serialize($this->parameters);
117 * Gets the SQL query part to add to a query.
119 * @return string The constraint SQL if there is available, empty string otherwise
121 abstract public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias);