mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-12 06:48:41 +00:00
Add limit to random result
This commit is contained in:
parent
2b2808ef4e
commit
0351533b05
|
|
@ -994,21 +994,26 @@ class DataMapperAbstract implements DataMapperInterface
|
||||||
/**
|
/**
|
||||||
* Get random object
|
* Get random object
|
||||||
*
|
*
|
||||||
|
* @param int $amount Amount of random models
|
||||||
* @param int $relations Relations type
|
* @param int $relations Relations type
|
||||||
*
|
*
|
||||||
* @return array
|
* @return mixed
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public static function getRandom(int $relations = RelationType::ALL)
|
public static function getRandom(int $amount = 1, int $relations = RelationType::ALL)
|
||||||
{
|
{
|
||||||
$query = new Builder(self::$db);
|
$query = new Builder(self::$db);
|
||||||
$query->prefix(self::$db->getPrefix())
|
$query->prefix(self::$db->getPrefix())
|
||||||
->random(static::$primaryKey)
|
->random(static::$primaryKey)
|
||||||
->from(static::$table);
|
->from(static::$table)
|
||||||
|
->limit($amount);
|
||||||
|
|
||||||
return self::get(self::$db->con->prepare($query->toSql())->execute(), $relations);
|
$sth = self::$db->con->prepare($query->toSql());
|
||||||
|
$sth->execute();
|
||||||
|
|
||||||
|
return self::get($sth->fetchAll(), $relations);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,8 @@ class MicrosoftGrammar extends Grammar
|
||||||
$expression = '*';
|
$expression = '*';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'SELECT TOP 1 ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY IDX FETCH FIRST 1 ROWS ONLY';
|
$query->limit = $query->limit ?? 1;
|
||||||
|
|
||||||
|
return 'SELECT TOP 1 ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY IDX FETCH FIRST ' . $query->limit . ' ROWS ONLY';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,6 @@ class MysqlGrammar extends Grammar
|
||||||
$expression = '*';
|
$expression = '*';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RAND() LIMIT 1';
|
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RAND() ' . $this->compileLimit($query, $query->limit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,8 @@ class OracleGrammar extends Grammar
|
||||||
$expression = '*';
|
$expression = '*';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'SELECT ' . $expression . ' FROM (SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY dbms_random.value) WHERE rownum = 1';
|
$query->limit = $query->limit ?? 1;
|
||||||
|
|
||||||
|
return 'SELECT ' . $expression . ' FROM (SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY dbms_random.value) WHERE rownum >= 1 AND rownum <= ' . $query->limit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,6 @@ class PostgresGrammar extends Grammar
|
||||||
$expression = '*';
|
$expression = '*';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() LIMIT 1';
|
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() ' . $this->compileLimit($query, $query->limit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,6 @@ class SqliteGrammar extends Grammar
|
||||||
$expression = '*';
|
$expression = '*';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() LIMIT 1';
|
return 'SELECT ' . $expression . ' ' . $this->compileFrom($query, $query->from) . ' ORDER BY RANDOM() ' . $this->compileLimit($query, $query->limit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user