<?php

namespace Eater\Glim\Model\Base;

use \Exception;
use Eater\Glim\Model\EmailMessage as ChildEmailMessage;
use Eater\Glim\Model\EmailMessageQuery as ChildEmailMessageQuery;
use Eater\Glim\Model\Map\EmailMessageTableMap;
use Propel\Runtime\Propel;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Propel\Runtime\ActiveQuery\ModelJoin;
use Propel\Runtime\Collection\ObjectCollection;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\LogicException;
use Propel\Runtime\Exception\PropelException;

/**
 * Base class that represents a query for the 'email_message' table.
 *
 *
 *
 * @method     ChildEmailMessageQuery orderByRecipient($order = Criteria::ASC) Order by the recipient column
 * @method     ChildEmailMessageQuery orderBySender($order = Criteria::ASC) Order by the sender column
 * @method     ChildEmailMessageQuery orderByContents($order = Criteria::ASC) Order by the contents column
 * @method     ChildEmailMessageQuery orderBySubject($order = Criteria::ASC) Order by the subject column
 *
 * @method     ChildEmailMessageQuery groupByRecipient() Group by the recipient column
 * @method     ChildEmailMessageQuery groupBySender() Group by the sender column
 * @method     ChildEmailMessageQuery groupByContents() Group by the contents column
 * @method     ChildEmailMessageQuery groupBySubject() Group by the subject column
 *
 * @method     ChildEmailMessageQuery leftJoin($relation) Adds a LEFT JOIN clause to the query
 * @method     ChildEmailMessageQuery rightJoin($relation) Adds a RIGHT JOIN clause to the query
 * @method     ChildEmailMessageQuery innerJoin($relation) Adds a INNER JOIN clause to the query
 *
 * @method     ChildEmailMessageQuery leftJoinRecipientEmailAddress($relationAlias = null) Adds a LEFT JOIN clause to the query using the RecipientEmailAddress relation
 * @method     ChildEmailMessageQuery rightJoinRecipientEmailAddress($relationAlias = null) Adds a RIGHT JOIN clause to the query using the RecipientEmailAddress relation
 * @method     ChildEmailMessageQuery innerJoinRecipientEmailAddress($relationAlias = null) Adds a INNER JOIN clause to the query using the RecipientEmailAddress relation
 *
 * @method     ChildEmailMessageQuery leftJoinSenderEmailAddress($relationAlias = null) Adds a LEFT JOIN clause to the query using the SenderEmailAddress relation
 * @method     ChildEmailMessageQuery rightJoinSenderEmailAddress($relationAlias = null) Adds a RIGHT JOIN clause to the query using the SenderEmailAddress relation
 * @method     ChildEmailMessageQuery innerJoinSenderEmailAddress($relationAlias = null) Adds a INNER JOIN clause to the query using the SenderEmailAddress relation
 *
 * @method     \Eater\Glim\Model\EmailAddressQuery endUse() Finalizes a secondary criteria and merges it with its primary Criteria
 *
 * @method     ChildEmailMessage findOne(ConnectionInterface $con = null) Return the first ChildEmailMessage matching the query
 * @method     ChildEmailMessage findOneOrCreate(ConnectionInterface $con = null) Return the first ChildEmailMessage matching the query, or a new ChildEmailMessage object populated from the query conditions when no match is found
 *
 * @method     ChildEmailMessage findOneByRecipient(int $recipient) Return the first ChildEmailMessage filtered by the recipient column
 * @method     ChildEmailMessage findOneBySender(int $sender) Return the first ChildEmailMessage filtered by the sender column
 * @method     ChildEmailMessage findOneByContents(string $contents) Return the first ChildEmailMessage filtered by the contents column
 * @method     ChildEmailMessage findOneBySubject(string $subject) Return the first ChildEmailMessage filtered by the subject column *

 * @method     ChildEmailMessage requirePk($key, ConnectionInterface $con = null) Return the ChildEmailMessage by primary key and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
 * @method     ChildEmailMessage requireOne(ConnectionInterface $con = null) Return the first ChildEmailMessage matching the query and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
 *
 * @method     ChildEmailMessage requireOneByRecipient(int $recipient) Return the first ChildEmailMessage filtered by the recipient column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
 * @method     ChildEmailMessage requireOneBySender(int $sender) Return the first ChildEmailMessage filtered by the sender column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
 * @method     ChildEmailMessage requireOneByContents(string $contents) Return the first ChildEmailMessage filtered by the contents column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
 * @method     ChildEmailMessage requireOneBySubject(string $subject) Return the first ChildEmailMessage filtered by the subject column and throws \Propel\Runtime\Exception\EntityNotFoundException when not found
 *
 * @method     ChildEmailMessage[]|ObjectCollection find(ConnectionInterface $con = null) Return ChildEmailMessage objects based on current ModelCriteria
 * @method     ChildEmailMessage[]|ObjectCollection findByRecipient(int $recipient) Return ChildEmailMessage objects filtered by the recipient column
 * @method     ChildEmailMessage[]|ObjectCollection findBySender(int $sender) Return ChildEmailMessage objects filtered by the sender column
 * @method     ChildEmailMessage[]|ObjectCollection findByContents(string $contents) Return ChildEmailMessage objects filtered by the contents column
 * @method     ChildEmailMessage[]|ObjectCollection findBySubject(string $subject) Return ChildEmailMessage objects filtered by the subject column
 * @method     ChildEmailMessage[]|\Propel\Runtime\Util\PropelModelPager paginate($page = 1, $maxPerPage = 10, ConnectionInterface $con = null) Issue a SELECT query based on the current ModelCriteria and uses a page and a maximum number of results per page to compute an offset and a limit
 *
 */
abstract class EmailMessageQuery extends ModelCriteria
{
    protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException';

    /**
     * Initializes internal state of \Eater\Glim\Model\Base\EmailMessageQuery object.
     *
     * @param     string $dbName The database name
     * @param     string $modelName The phpName of a model, e.g. 'Book'
     * @param     string $modelAlias The alias for the model in this query, e.g. 'b'
     */
    public function __construct($dbName = 'default', $modelName = '\\Eater\\Glim\\Model\\EmailMessage', $modelAlias = null)
    {
        parent::__construct($dbName, $modelName, $modelAlias);
    }

    /**
     * Returns a new ChildEmailMessageQuery object.
     *
     * @param     string $modelAlias The alias of a model in the query
     * @param     Criteria $criteria Optional Criteria to build the query from
     *
     * @return ChildEmailMessageQuery
     */
    public static function create($modelAlias = null, Criteria $criteria = null)
    {
        if ($criteria instanceof ChildEmailMessageQuery) {
            return $criteria;
        }
        $query = new ChildEmailMessageQuery();
        if (null !== $modelAlias) {
            $query->setModelAlias($modelAlias);
        }
        if ($criteria instanceof Criteria) {
            $query->mergeWith($criteria);
        }

        return $query;
    }

    /**
     * Find object by primary key.
     * Propel uses the instance pool to skip the database if the object exists.
     * Go fast if the query is untouched.
     *
     * <code>
     * $obj  = $c->findPk(12, $con);
     * </code>
     *
     * @param mixed $key Primary key to use for the query
     * @param ConnectionInterface $con an optional connection object
     *
     * @return ChildEmailMessage|array|mixed the result, formatted by the current formatter
     */
    public function findPk($key, ConnectionInterface $con = null)
    {
        throw new LogicException('The EmailMessage object has no primary key');
    }

    /**
     * Find objects by primary key
     * <code>
     * $objs = $c->findPks(array(array(12, 56), array(832, 123), array(123, 456)), $con);
     * </code>
     * @param     array $keys Primary keys to use for the query
     * @param     ConnectionInterface $con an optional connection object
     *
     * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter
     */
    public function findPks($keys, ConnectionInterface $con = null)
    {
        throw new LogicException('The EmailMessage object has no primary key');
    }

    /**
     * Filter the query by primary key
     *
     * @param     mixed $key Primary key to use for the query
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function filterByPrimaryKey($key)
    {
        throw new LogicException('The EmailMessage object has no primary key');
    }

    /**
     * Filter the query by a list of primary keys
     *
     * @param     array $keys The list of primary key to use for the query
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function filterByPrimaryKeys($keys)
    {
        throw new LogicException('The EmailMessage object has no primary key');
    }

    /**
     * Filter the query on the recipient column
     *
     * Example usage:
     * <code>
     * $query->filterByRecipient(1234); // WHERE recipient = 1234
     * $query->filterByRecipient(array(12, 34)); // WHERE recipient IN (12, 34)
     * $query->filterByRecipient(array('min' => 12)); // WHERE recipient > 12
     * </code>
     *
     * @see       filterByRecipientEmailAddress()
     *
     * @param     mixed $recipient The value to use as filter.
     *              Use scalar values for equality.
     *              Use array values for in_array() equivalent.
     *              Use associative array('min' => $minValue, 'max' => $maxValue) for intervals.
     * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function filterByRecipient($recipient = null, $comparison = null)
    {
        if (is_array($recipient)) {
            $useMinMax = false;
            if (isset($recipient['min'])) {
                $this->addUsingAlias(EmailMessageTableMap::COL_RECIPIENT, $recipient['min'], Criteria::GREATER_EQUAL);
                $useMinMax = true;
            }
            if (isset($recipient['max'])) {
                $this->addUsingAlias(EmailMessageTableMap::COL_RECIPIENT, $recipient['max'], Criteria::LESS_EQUAL);
                $useMinMax = true;
            }
            if ($useMinMax) {
                return $this;
            }
            if (null === $comparison) {
                $comparison = Criteria::IN;
            }
        }

        return $this->addUsingAlias(EmailMessageTableMap::COL_RECIPIENT, $recipient, $comparison);
    }

    /**
     * Filter the query on the sender column
     *
     * Example usage:
     * <code>
     * $query->filterBySender(1234); // WHERE sender = 1234
     * $query->filterBySender(array(12, 34)); // WHERE sender IN (12, 34)
     * $query->filterBySender(array('min' => 12)); // WHERE sender > 12
     * </code>
     *
     * @see       filterBySenderEmailAddress()
     *
     * @param     mixed $sender The value to use as filter.
     *              Use scalar values for equality.
     *              Use array values for in_array() equivalent.
     *              Use associative array('min' => $minValue, 'max' => $maxValue) for intervals.
     * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function filterBySender($sender = null, $comparison = null)
    {
        if (is_array($sender)) {
            $useMinMax = false;
            if (isset($sender['min'])) {
                $this->addUsingAlias(EmailMessageTableMap::COL_SENDER, $sender['min'], Criteria::GREATER_EQUAL);
                $useMinMax = true;
            }
            if (isset($sender['max'])) {
                $this->addUsingAlias(EmailMessageTableMap::COL_SENDER, $sender['max'], Criteria::LESS_EQUAL);
                $useMinMax = true;
            }
            if ($useMinMax) {
                return $this;
            }
            if (null === $comparison) {
                $comparison = Criteria::IN;
            }
        }

        return $this->addUsingAlias(EmailMessageTableMap::COL_SENDER, $sender, $comparison);
    }

    /**
     * Filter the query on the contents column
     *
     * Example usage:
     * <code>
     * $query->filterByContents('fooValue');   // WHERE contents = 'fooValue'
     * $query->filterByContents('%fooValue%'); // WHERE contents LIKE '%fooValue%'
     * </code>
     *
     * @param     string $contents The value to use as filter.
     *              Accepts wildcards (* and % trigger a LIKE)
     * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function filterByContents($contents = null, $comparison = null)
    {
        if (null === $comparison) {
            if (is_array($contents)) {
                $comparison = Criteria::IN;
            } elseif (preg_match('/[\%\*]/', $contents)) {
                $contents = str_replace('*', '%', $contents);
                $comparison = Criteria::LIKE;
            }
        }

        return $this->addUsingAlias(EmailMessageTableMap::COL_CONTENTS, $contents, $comparison);
    }

    /**
     * Filter the query on the subject column
     *
     * Example usage:
     * <code>
     * $query->filterBySubject('fooValue');   // WHERE subject = 'fooValue'
     * $query->filterBySubject('%fooValue%'); // WHERE subject LIKE '%fooValue%'
     * </code>
     *
     * @param     string $subject The value to use as filter.
     *              Accepts wildcards (* and % trigger a LIKE)
     * @param     string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function filterBySubject($subject = null, $comparison = null)
    {
        if (null === $comparison) {
            if (is_array($subject)) {
                $comparison = Criteria::IN;
            } elseif (preg_match('/[\%\*]/', $subject)) {
                $subject = str_replace('*', '%', $subject);
                $comparison = Criteria::LIKE;
            }
        }

        return $this->addUsingAlias(EmailMessageTableMap::COL_SUBJECT, $subject, $comparison);
    }

    /**
     * Filter the query by a related \Eater\Glim\Model\EmailAddress object
     *
     * @param \Eater\Glim\Model\EmailAddress|ObjectCollection $emailAddress The related object(s) to use as filter
     * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
     *
     * @throws \Propel\Runtime\Exception\PropelException
     *
     * @return ChildEmailMessageQuery The current query, for fluid interface
     */
    public function filterByRecipientEmailAddress($emailAddress, $comparison = null)
    {
        if ($emailAddress instanceof \Eater\Glim\Model\EmailAddress) {
            return $this
                ->addUsingAlias(EmailMessageTableMap::COL_RECIPIENT, $emailAddress->getId(), $comparison);
        } elseif ($emailAddress instanceof ObjectCollection) {
            if (null === $comparison) {
                $comparison = Criteria::IN;
            }

            return $this
                ->addUsingAlias(EmailMessageTableMap::COL_RECIPIENT, $emailAddress->toKeyValue('PrimaryKey', 'Id'), $comparison);
        } else {
            throw new PropelException('filterByRecipientEmailAddress() only accepts arguments of type \Eater\Glim\Model\EmailAddress or Collection');
        }
    }

    /**
     * Adds a JOIN clause to the query using the RecipientEmailAddress relation
     *
     * @param     string $relationAlias optional alias for the relation
     * @param     string $joinType Accepted values are null, 'left join', 'right join', 'inner join'
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function joinRecipientEmailAddress($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
    {
        $tableMap = $this->getTableMap();
        $relationMap = $tableMap->getRelation('RecipientEmailAddress');

        // create a ModelJoin object for this join
        $join = new ModelJoin();
        $join->setJoinType($joinType);
        $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias);
        if ($previousJoin = $this->getPreviousJoin()) {
            $join->setPreviousJoin($previousJoin);
        }

        // add the ModelJoin to the current object
        if ($relationAlias) {
            $this->addAlias($relationAlias, $relationMap->getRightTable()->getName());
            $this->addJoinObject($join, $relationAlias);
        } else {
            $this->addJoinObject($join, 'RecipientEmailAddress');
        }

        return $this;
    }

    /**
     * Use the RecipientEmailAddress relation EmailAddress object
     *
     * @see useQuery()
     *
     * @param     string $relationAlias optional alias for the relation,
     *                                   to be used as main alias in the secondary query
     * @param     string $joinType Accepted values are null, 'left join', 'right join', 'inner join'
     *
     * @return \Eater\Glim\Model\EmailAddressQuery A secondary query class using the current class as primary query
     */
    public function useRecipientEmailAddressQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
    {
        return $this
            ->joinRecipientEmailAddress($relationAlias, $joinType)
            ->useQuery($relationAlias ? $relationAlias : 'RecipientEmailAddress', '\Eater\Glim\Model\EmailAddressQuery');
    }

    /**
     * Filter the query by a related \Eater\Glim\Model\EmailAddress object
     *
     * @param \Eater\Glim\Model\EmailAddress|ObjectCollection $emailAddress The related object(s) to use as filter
     * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
     *
     * @throws \Propel\Runtime\Exception\PropelException
     *
     * @return ChildEmailMessageQuery The current query, for fluid interface
     */
    public function filterBySenderEmailAddress($emailAddress, $comparison = null)
    {
        if ($emailAddress instanceof \Eater\Glim\Model\EmailAddress) {
            return $this
                ->addUsingAlias(EmailMessageTableMap::COL_SENDER, $emailAddress->getId(), $comparison);
        } elseif ($emailAddress instanceof ObjectCollection) {
            if (null === $comparison) {
                $comparison = Criteria::IN;
            }

            return $this
                ->addUsingAlias(EmailMessageTableMap::COL_SENDER, $emailAddress->toKeyValue('PrimaryKey', 'Id'), $comparison);
        } else {
            throw new PropelException('filterBySenderEmailAddress() only accepts arguments of type \Eater\Glim\Model\EmailAddress or Collection');
        }
    }

    /**
     * Adds a JOIN clause to the query using the SenderEmailAddress relation
     *
     * @param     string $relationAlias optional alias for the relation
     * @param     string $joinType Accepted values are null, 'left join', 'right join', 'inner join'
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function joinSenderEmailAddress($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
    {
        $tableMap = $this->getTableMap();
        $relationMap = $tableMap->getRelation('SenderEmailAddress');

        // create a ModelJoin object for this join
        $join = new ModelJoin();
        $join->setJoinType($joinType);
        $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias);
        if ($previousJoin = $this->getPreviousJoin()) {
            $join->setPreviousJoin($previousJoin);
        }

        // add the ModelJoin to the current object
        if ($relationAlias) {
            $this->addAlias($relationAlias, $relationMap->getRightTable()->getName());
            $this->addJoinObject($join, $relationAlias);
        } else {
            $this->addJoinObject($join, 'SenderEmailAddress');
        }

        return $this;
    }

    /**
     * Use the SenderEmailAddress relation EmailAddress object
     *
     * @see useQuery()
     *
     * @param     string $relationAlias optional alias for the relation,
     *                                   to be used as main alias in the secondary query
     * @param     string $joinType Accepted values are null, 'left join', 'right join', 'inner join'
     *
     * @return \Eater\Glim\Model\EmailAddressQuery A secondary query class using the current class as primary query
     */
    public function useSenderEmailAddressQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
    {
        return $this
            ->joinSenderEmailAddress($relationAlias, $joinType)
            ->useQuery($relationAlias ? $relationAlias : 'SenderEmailAddress', '\Eater\Glim\Model\EmailAddressQuery');
    }

    /**
     * Exclude object from result
     *
     * @param   ChildEmailMessage $emailMessage Object to remove from the list of results
     *
     * @return $this|ChildEmailMessageQuery The current query, for fluid interface
     */
    public function prune($emailMessage = null)
    {
        if ($emailMessage) {
            throw new LogicException('EmailMessage object has no primary key');

        }

        return $this;
    }

    /**
     * Deletes all rows from the email_message table.
     *
     * @param ConnectionInterface $con the connection to use
     * @return int The number of affected rows (if supported by underlying database driver).
     */
    public function doDeleteAll(ConnectionInterface $con = null)
    {
        if (null === $con) {
            $con = Propel::getServiceContainer()->getWriteConnection(EmailMessageTableMap::DATABASE_NAME);
        }

        // use transaction because $criteria could contain info
        // for more than one table or we could emulating ON DELETE CASCADE, etc.
        return $con->transaction(function () use ($con) {
            $affectedRows = 0; // initialize var to track total num of affected rows
            $affectedRows += parent::doDeleteAll($con);
            // Because this db requires some delete cascade/set null emulation, we have to
            // clear the cached instance *after* the emulation has happened (since
            // instances get re-added by the select statement contained therein).
            EmailMessageTableMap::clearInstancePool();
            EmailMessageTableMap::clearRelatedInstancePool();

            return $affectedRows;
        });
    }

    /**
     * Performs a DELETE on the database based on the current ModelCriteria
     *
     * @param ConnectionInterface $con the connection to use
     * @return int             The number of affected rows (if supported by underlying database driver).  This includes CASCADE-related rows
     *                         if supported by native driver or if emulated using Propel.
     * @throws PropelException Any exceptions caught during processing will be
     *                         rethrown wrapped into a PropelException.
     */
    public function delete(ConnectionInterface $con = null)
    {
        if (null === $con) {
            $con = Propel::getServiceContainer()->getWriteConnection(EmailMessageTableMap::DATABASE_NAME);
        }

        $criteria = $this;

        // Set the correct dbName
        $criteria->setDbName(EmailMessageTableMap::DATABASE_NAME);

        // use transaction because $criteria could contain info
        // for more than one table or we could emulating ON DELETE CASCADE, etc.
        return $con->transaction(function () use ($con, $criteria) {
            $affectedRows = 0; // initialize var to track total num of affected rows

            EmailMessageTableMap::removeInstanceFromPool($criteria);

            $affectedRows += ModelCriteria::delete($con);
            EmailMessageTableMap::clearRelatedInstancePool();

            return $affectedRows;
        });
    }

} // EmailMessageQuery
