Function checkExists

@author: 713uk13m <dev@nguyenanhung.com>
@time  : 2018-12-02 21:30

@param string $whereValue
@param string $whereField
@param string $select

@return int
436366
public function checkExists($whereValue = '', $whereField = 'id', $select = '*')
    {
        if (is_array($whereValue) && count($whereValue) > 0) {
            foreach ($whereValue as $column => $column_value) {
                if (is_array($column_value)) {
                    $this->db->where($column, $column_value, self::OPERATOR_IS_IN);
                } else {
                    $this->db->where($column, $column_value, self::OPERATOR_EQUAL_TO);
                }
            }
        } else {
            $this->db->where($whereField, $whereValue, self::OPERATOR_EQUAL_TO);
        }
        $this->db->get($this->table, NULL, $select);

        return (int) $this->db->count;
    }
435087
public function isDataFixtureExists($where, array $parameters = [])
    {
        $entityId = $this->createQueryBuilder('m')
            ->select('m.id')
            ->where($where)
            ->setMaxResults(1)
            ->getQuery()
            ->execute($parameters);

        return $entityId ? true : false;
    }
584038
public static function countWhere (string $column, array $operatorValue, string $joinOperator="AND") : int
	{
		$joinOperator 	= static::validateOperator($joinOperator, "AND");
		$expression 	= static::generateValue($column, $operatorValue, $joinOperator);
		$sqlQuery  		= "SELECT COUNT(*) as count FROM ".static::$tableName." WHERE {$column} {$expression}";
		$resultSet 		= Database::getInstance()->query($sqlQuery);
		$row 			= Database::fetchAssoc($resultSet);
		return (int) array_shift($row);
	}
1300095
public function executeSelect($tableName, $columns = '*', $where = null, $afterWhere = '')
    {
        return $this->pdo->executeSelect($tableName, $columns, $where, $afterWhere);
    }
998517
public function checkExistsAndInsertOrUpdateData($dataInsert = [], $dataUpdate = [], $wheres = [])
    {
        $this->connection();
        $db = DB::table($this->table);
        if (is_array($wheres) && count($wheres) > 0) {
            foreach ($wheres as $field => $value) {
                if (is_array($value)) {
                    $db->whereIn($field, $value);
                } else {
                    $db->where($field, self::OPERATOR_EQUAL_TO, $value);
                }
            }
        } else {
            $db->where($this->primaryKey, self::OPERATOR_EQUAL_TO, $wheres);
        }
        $checkExists = $db->count();
        $this->debug->debug(__FUNCTION__, 'Check Exists Data: ' . $checkExists);
        if (!$checkExists) {
            $id = $db->insertGetId($dataInsert);
            $this->debug->debug(__FUNCTION__, 'SQL Queries: ' . $db->toSql());
            $this->debug->debug(__FUNCTION__, 'Result Insert ID: ' . $id);

            return $id;
        } else {
            $resultId = $db->update($dataUpdate);
            $this->debug->debug(__FUNCTION__, 'Result Update Rows: ' . $resultId);

            return $resultId;
        }
    }
162383
public function count($field = '*', $isMulti = false, $useMaster = false)
    {
        $cmd = [
            'count' => $this->getRealTableName(key($this->table)),
            'query' => $this->sql['where']
        ];

        $count = $this->runMongoCommand($cmd, $useMaster);
        return intval($count[0]['n']);
    }
243258
protected function getLookupTableValues($table, array $selectFields, array $whereParts)
    {
        $dbHandle = $this->getDatabase()->getDatabaseConnection();

        $whereCondition = $whereValues = array();
        foreach ($whereParts as $field => $value) {
            $whereCondition[] = '`' . $field . '` = :' . $field;
            $whereValues[':' . $field] = $value;
        }

        $query = 'SELECT `' . implode('`,`', $selectFields) . '`
                  FROM ' . $table . '
                  WHERE ' . implode(' AND ', $whereCondition) . '
                  LIMIT 1';

        $statement = $dbHandle->prepare($query);
        $executeResult = $statement->execute($whereValues);

        $statement = $this->getDatabase()->checkQueryError($statement, $executeResult, $whereValues);
        return $statement->fetch(\PDO::FETCH_ASSOC);
    }
1266563
public function countResult($wheres = [], $selectField = '*')
    {
        if (!is_array($selectField)) {
            $selectField = [$selectField];
        }
        $db = $this->db->select($selectField)->from($this->table);
        if (is_array($wheres) && count($wheres) > 0) {
            foreach ($wheres as $field => $value) {
                if (is_array($value)) {
                    $db->whereIn($field, $value);
                } else {
                    $db->where($field, self::OPERATOR_EQUAL_TO, $value);
                }
            }
        } else {
            $db->where($this->primaryKey, self::OPERATOR_EQUAL_TO, $wheres);
        }
        $totalItem = $db->execute()->rowCount();
        $this->debug->debug(__FUNCTION__, 'Total Item Result => ' . json_encode($totalItem));

        return $totalItem;
    }
1243431
public function select($where = null, $order = null)
    {
        if (!$this->isInitialized) {
            $this->initialize();
        }

        $select = $this->sql->select();

        if ($where instanceof \Closure) {
            $where($select);
        } elseif ($where !== null) {
            $select->where($where);
        }
        if ($order !== null) {
            $select->order($order);
        }

        return $this->selectWith($select);
    }
622470
public function selectOption($select, $option) {
		// Find field
		$field = $this
			->getSession()
			->getPage()
			->findField($this->fixStepArgument($select));

		// If field is visible then select it as per normal
		if($field && $field->isVisible()) {
			parent::selectOption($select, $option);
		} else {
			$this->selectOptionWithJavascript($select, $option);
		}
	}
483162
public function selectValue($tableName, $column, $where = null)
    {
        $stmt = $this->executeSelect($tableName, $column, $where);
        return $stmt->fetchColumn();
    }
1283087
function count($table, $where, $where_params = null)
    {
        $sql = "SELECT COUNT(*) FROM " . $table . $this->buildWhere($where);
        $stmt = $this->executeQuery($sql, $where_params);

        return $stmt->fetchColumn();
    }
74673
protected function whereRowValues(Builder $query, $where)
    {
        $columns = $this->columnize($where['columns']);

        $values = $this->parameterize($where['values']);

        return '('.$columns.') '.$where['operator'].' ('.$values.')';
    }
286290
public function exists($where=null)
    {
        if ($where) {
            $this->getQuery()->where($where);
        }
        
        return $this->count() > 0;
    }
1272508
public static function selectSql($tablename, $fields, $where_clause=false) {
        $sql = "SELECT ";
        $first = true;
        foreach ( $fields as $field ) {
            if ( ! $first ) $sql .= ', ';
            $sql .= $field;
            $first = false;
        }
        $sql .= "\n FROM ".$tablename;
        if ( $where_clause && strlen($where_clause) > 0 ) $sql .= "\nWHERE ".$where_clause;
        return $sql;
    }
399146
public static function q_check(string $table, string $where = ''): bool
    {
        return (bool)self::getInstance()->sql_query('SELECT NULL FROM `' . $table . '`' . ($where ? ' WHERE ' . $where : '') . ' LIMIT 1')->rowCount();
    }
972595
public function fieldSelectAll($table, $fields, $where, $orderBy = null)
    {
        $table = $this->getTableName($table);

        $query = "SELECT ";

        $query .= $this->selectFields($fields);

        $query .= " FROM " . $table . " ";

        $params = null;
        if ($where !== self::NO_WHERE_CLAUSE) {
            $query .= "WHERE " . $this->where($where, $params);
        }

        if ($orderBy) {
            $query .= $this->orderBy($orderBy) . " ";
        }

        return $this->query($query, $params);
    }