Determines what tables need to be included in the query
based on the mapping
@param Criteria $criteria
@return string "from" sql
966505
private function GetTableJoinSQL($criteria)
	{
		$sql = "";
		
		$tablenames = array_keys($this->Tables);
		
		if (count($tablenames) > 1)
		{
			// we're selecting from multiple tables so we have to do an outer join
			$sql .= " from `" . $tablenames[0] . "`";
				
			// WARNING: if tables are being joined in the incorrect order, it is likely
			// caused by a query that goes across more than one foreign relationship.
			// you can force tables to be joined in a specific order by adding a field
			// reference in the outermost Phreezable FieldMap.  QueryBuilder will
			// include tables in whatever order they are references, so you can control
			// this by simply adding in referenced to foreign field in the order you want
			// the tables to be joined
			//for ($i = count($tablenames) -1; $i > 0 ; $i--) // this iterates backwards
			for ($i = 1; $i < count($tablenames); $i++)      // this iterates forwards
			{
				try
				{
					$sql .= $this->Joins[$tablenames[$i]];
				}
				catch (Exception $ex)
				{
					// if 'undefined index' occurs here, there is likely a foreign field in the fieldmap that does not have it's related keymap set to KM_LOAD_EAGER
					throw new Exception("An invalid join was attempted from table '" . $tablenames[$i] 
						. "'. Please verify that the KeyMap fetching strategy for table '" . $tablenames[0]
						. "' has been properly configured.");
				}
			}
		}
		else
		{
			// we are only selecting from one table
			// (LL) added backticks here
			$sql .= " from `" . $tablenames[0] . "` ";
		}
		
		return $sql;
	}
1179061
public static function findByCriteria(array $criteria): ?Model\Collection
    {
        try {
            list($columns, $values, $options) = static::parseCriteria($criteria);
        } catch (NoTagsException $e) {
            return null;
        }

        if (\count($columns) < 1) {
            return static::findAll($options);
        }

        return static::findBy($columns, $values, $options);
    }
737697
public function findRecords(string $table, array $criteria = []): array
    {
        $url = $this->getEndpoint($table);

        if (count($criteria) > 0) {
            $formulas = [];
            foreach ($criteria as $field => $value) {
                $formulas[] = sprintf("%s='%s'", $field, $value);
            }

            $url .= sprintf(
                '?filterByFormula=(%s)',
                implode(' AND ', $formulas)
            );
        }

        /** @var Response $response */
        $response = $this->browser->get(
            $url,
            [
                'content-type' => 'application/json',
            ]
        );

        $data = json_decode($response->getContent(), true);

        return array_map(function (array $value) {
            return new Record($value['id'], $value['fields']);
        }, $data['records']);
    }
963548
public function findFirst($criteria = null)
    {
        $this->getQuery()->setLimit(1);
        
        $resultSet = $this->findBy($criteria);
        $resultSet->rewind();
        
        return $resultSet->valid() ? $resultSet->current() : null;
    }
619823
public function search($criteria)
    {
        $this->loadModel('Content.Contents');

        try {
            $contents = $this->Contents->search($criteria);
            if ($contents->clause('limit')) {
                $this->paginate['limit'] = $contents->clause('limit');
            }

            // TO-DO: ask search-engine for operator presence
            if (strpos($criteria, 'language:') === false) {
                $contents = $contents->where([
                    'Contents.status >' => 0,
                    'Contents.language IN' => ['', null, I18n::locale()] // any or concrete
                ]);
            }

            $contents = $this->paginate($contents);
        } catch (\Exception $e) {
            $contents = new Collection([]);
        }

        $this->set(compact('contents', 'criteria'));
        $this->viewMode('search-result');
    }
846064
public function criteria(Criteria\Criteria $cri = null)
    {
        if (! $cri) $cri = new Criteria\Criteria($this);
        $cri->setTable($this);

        return $cri;
    }
602043
public function matching(Criteria $criteria)
    {
        $this->guardAgainstInvalidGivenType();
        $elements = $this->collection->matching($criteria)->toArray();

        return $this->create($elements);
    }
453824
public function getQuery($criteria = [])
    {

        /** @var ElementInterface $elementClass */
        $elementClass = static::elementClass();

        /** @var ElementQueryInterface $query */
        $query = $elementClass::find();

        // Configure it
        QueryHelper::configure(
            $query,
            $criteria
        );

        return $query;
    }
17278
public function countRecordsInTableMatching($table, $criteria = []): int
    {
        $where = '';
        foreach ($criteria as $field => $vale) {
            if ($where) {
                $where .= ' AND ';
            }
            if ($vale === null) {
                $where .= sprintf('%s IS NULL', $field);
            } else {
                $where .= sprintf('%s = :%s', $field, $field);
            }
        }
        $query = sprintf('SELECT count(*) AS records FROM %s WHERE %s', $table, $where);

        /** @var EntityManager $manager */
        $manager = $this->client->getContainer()->get('doctrine')->getManager();
        $rsm = new ResultSetMapping();
        $rsm->addScalarResult('records', 'records', 'integer');
        $query = $manager->createNativeQuery($query, $rsm);
        $query->setParameters($criteria);

        return (int) $query->getSingleScalarResult();
    }
559187
public function getDeleteFromClause($criteria, $tableName)
    {
        $sql = 'DELETE ';
        if ($queryComment = $criteria->getComment()) {
            $sql .= '/* ' . $queryComment . ' */ ';
        }
        if ($realTableName = $criteria->getTableForAlias($tableName)) {
            if ($this->useQuoteIdentifier()) {
                $realTableName = $this->quoteIdentifierTable($realTableName);
            }
            $sql .= $tableName . ' FROM ' . $realTableName . ' AS ' . $tableName;
        } else {
            if ($this->useQuoteIdentifier()) {
                $tableName = $this->quoteIdentifierTable($tableName);
            }
            $sql .= 'FROM ' . $tableName;
        }

        return $sql;
    }
1189309
public function get($criteria)
    {
        /** @var AssemblerMatcher $candidate */
        foreach (array_merge($this->assemblers, $this->fallbackAssemblers) as $candidate) {
            if ($candidate->match($criteria) === true) {
                return $candidate->getAssembler();
            }
        }

        return null;
    }
332824
public function createDeleteCommand($table,$criteria)
	{
		$this->ensureTable($table);
		$sql="DELETE FROM {$table->rawName}";
		$sql=$this->applyJoin($sql,$criteria->join);
		$sql=$this->applyCondition($sql,$criteria->condition);
		$sql=$this->applyGroup($sql,$criteria->group);
		$sql=$this->applyHaving($sql,$criteria->having);
		$sql=$this->applyOrder($sql,$criteria->order);
		$sql=$this->applyLimit($sql,$criteria->limit,$criteria->offset);
		$command=$this->_connection->createCommand($sql);
		$this->bindValues($command,$criteria->params);
		return $command;
	}
862534
public function getDeleteFromClause(Criteria $criteria, $tableName)
    {
        $sql = 'DELETE ';
        if ($queryComment = $criteria->getComment()) {
            $sql .= '/* ' . $queryComment . ' */ ';
        }

        if ($realTableName = $criteria->getTableForAlias($tableName)) {
            $realTableName = $criteria->quoteIdentifierTable($realTableName);
            $sql .= $tableName . ' FROM ' . $realTableName . ' AS ' . $tableName;
        } else {
            $tableName = $criteria->quoteIdentifierTable($tableName);
            $sql .= 'FROM ' . $tableName;
        }

        return $sql;
    }
435836
public function countByCriteria(Criteria $criteria)
    {
        $qb = $this->createCriteriaQueryBuilder($criteria, 'm');

        $qb->select('COUNT(m.id)');

        return $qb->getQuery()->getSingleScalarResult();
    }
68062
public function query(Criteria $criteria=null, $alias=null)
    {
        $query = new Query($this->_connection);

        if($alias) {
            $query->from('`'.$this->_tableName."` AS `".$alias."`");
        } else {
            $query->from('`'.$this->_tableName.'`');
        }

        // add optional where clause
        if($criteria) $query->where($criteria->toSql());

        return $query;
    }
496933
public function query($criteria,$all=false)
	{
		$this->joinAll=$criteria->together===true;

		if($criteria->alias!='')
		{
			$this->_joinTree->tableAlias=$criteria->alias;
			$this->_joinTree->rawTableAlias=$this->_builder->getSchema()->quoteTableName($criteria->alias);
		}

		$this->_joinTree->find($criteria);
		$this->_joinTree->afterFind();

		if($all)
		{
			$result = array_values($this->_joinTree->records);
			if ($criteria->index!==null)
			{
				$index=$criteria->index;
				$array=array();
				foreach($result as $object)
					$array[$object->$index]=$object;
				$result=$array;
			}
		}
		elseif(count($this->_joinTree->records))
			$result = reset($this->_joinTree->records);
		else
			$result = null;

		$this->destroyJoinTree();
		return $result;
	}
913504
public function findAll($criteria = [])
    {
        is_array($criteria) || ($criteria = []);
        $query = 'SELECT * FROM ' . static::TABLE;

        if (isset($criteria['orderby'])) {
            $query .= ' ORDER BY ' . $criteria['orderby'];
        }

        $stmt = AbstractApp::getDbal()->query($query);
        $list = [];

        $primary = $this->getPrimaryKey();

        if ($primary) {
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $list[$row[$primary]] = $row;
            }
        }
        else {
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $list[] = $row;
            }
        }

        return $this->extend($list, true);
    }