$PLURAL_INFLECTIONS = array(3, 11, 19, 27, 35);
if ($modelType == 'T') {
    $whereClause = 'modelType = "T"';
} else {
    if ($modelType) {
        $whereClause = "modelType = '{$modelType}' and restriction like '%P%'";
    } else {
        $whereClause = '(modelType = "T") or (modelType in ("M", "F", "N") and restriction like "%P%")';
    }
}
$dbResult = db_execute("select * from Lexem where {$whereClause} order by formNoAccent", PDO::FETCH_ASSOC);
$lexems = array();
foreach ($dbResult as $row) {
    $lexem = Model::factory('Lexem')->create($row);
    $matches = array();
    $ifs = InflectedForm::get_all_by_formNoAccent($lexem->formNoAccent);
    foreach ($ifs as $if) {
        if (in_array($if->inflectionId, $PLURAL_INFLECTIONS) && $if->lexemId != $lexem->id) {
            $matches[] = Lexem::get_by_id($if->lexemId);
        }
    }
    $lexem->matches = $matches;
    if (count($lexem->matches)) {
        $lexem->ifs = InflectedForm::loadByLexemId($lexem->id);
        // When a plural LOC lexem is merged into a non-LOC singular, we end up losing some word forms from LOC.
        // Therefore, we have to add the singular lexem to LOC as well. Matei says it is ok to expand LOC this way.
        $srcIfs = loadIfArrayByLexemId($lexem->id);
        foreach ($lexem->matches as $match) {
            $destIfs = loadIfArrayByLexemId($match->id);
            $addedForms = array();
            $lostForms = array();