TYPO3 - Using SQL operators in QueryBuilder API v8 - typo3

How to use SQL operators (https://www.w3schools.com/sql/sql_operators.asp) in this query? Like e.g. $uidMin = 5; $uidMax = 20; ...
$uid = 10;
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$queryBuilder->getRestrictions()->removeAll();
$statement = $queryBuilder
->select('uid', 'pid', 'header')
->from('tt_content')
->where(
$queryBuilder->expr()->eq('uid', $uid)
)
->execute();
while ($row = $statement->fetchAll()) {
$this->view->assign('inet', $row);
}
What I tried is e.g (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html).
$uidMin = 5;
$uidMax = 20;
...
$queryBuilder->expr()->between('uid', $uidMin, $uidMax)
... but didn't work.

What works is this:
$queryBuilder->expr()->andX(
$queryBuilder->expr()->gt('uid', $uidMin),
$queryBuilder->expr()->lt('uid', $uidMax)
)
... but between() still doesn't work ...

Related

How to migrate legacy function exec_SELECT_mm_query to Doctrine based Querybuilder / Connection in TYPO3

Assume the following code snippet:
$res = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
$foreign . '.*', $local, $mm, $foreign, 'AND ' . $local . '.uid=' . $constraintUid);
while ($r=$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
...
$foreign / foreign: name of foreign DB table
$local / local: name of local DB table
$mm / mm : name of relation DB table (typically somethingorother_mm)
$constraintUid / constraintUid: some uid
I would like to migrate code to TYPO3 9 using the Doctrine based database functions.
The above code gets converted to the following SQL statement:
SELECT foreign.* FROM local,mm,foreign
WHERE local.uid=mm.uid_local
AND foreign.uid=mm.uid_foreign
AND local.uid = constraintUid
see exec_SELECT_mm_query:
Parameters:
string $select Field list for SELECT
string $local_table Tablename, local table
string $mm_table Tablename, relation table
string $foreign_table Tablename, foreign table
string $whereClause Optional additional WHERE clauses put in
the end of the query. ...
...
My suggestion would be:
$foreign = 'a';
$mm = 'mm';
$local = 'b';
$item = 1;
/** #var \TYPO3\CMS\Core\Database\Query\QueryBuilder $queryBuilder */
$queryBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
\TYPO3\CMS\Core\Database\ConnectionPool::class
)->getQueryBuilderForTable($foreign);
$expr = $queryBuilder->expr();
/** #var \Doctrine\DBAL\Driver\Statement $rows */
$rows = $queryBuilder
->select('foreign.*')
->from($foreign, 'foreign')
->innerJoin('foreign', $mm, 'mm', $expr->eq('foreign.uid', 'mm.uid_foreign'))
->innerJoin('mm', $local, 'local', $expr->eq('mm.uid_local', 'local.uid'))
->where(
$expr->eq('local.uid', $queryBuilder->createNamedParameter($item, \PDO::PARAM_INT))
)
->execute();
while (($row = $rows->fetch()) != null) {
}

TYPO3 how to make query using $this->createQuery();

I have TYPO3 version 7.6.18
I need execute this query SELECT * from table WHERE something != something AND (something = something OR something = something)
$query = $this->createQuery();
$query->matching(
$query->logicalNot(
$query->equals('deleted_by_user', $userUid)
)
);
$query->matching(
$query->logicalOr(
$query->equals('user_from', $userUid),
$query->equals('user_to', $userUid)
)
);
return $query->execute();
this code does't work properly. Help me please anybody
$query = $this->createQuery();
$constraints = array();
$constraints[] = $query->logicalNot($query->equals('deleted_by_user', $userUid));
$constraints[] = $query->logicalOr($query->equals('user_from', $userUid), $query->equals('user_to', $userUid));
$query->matching($query->logicalAnd($constraints));
return $query->execute();

How can set up the table correctly for receiving the string converted from dates?

I have a problem and don't know how to solve it. After i run the code i didn't receive corectly a period of time. I put the code here.
I think something is wrong with table from database (i use PhpMyAdmin 4.2.0 module from EasyPhp). I put an image too to see what happens. The dates marked with red need to be at the end of table.
<?php
function data_range($first, $last, $step = '+1 day', $output - format = 'd-m-Y')
{
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while ($current <= $last)
{
$dates[] = date($output_format, $current);
$current = strtotime($step, $current);
}
foreach($dates as $zile)
{
$krr = explode('-', $zile);
var_dump($krr);
$result2 = implode('/', $krr);
echo $result2 . "<br/>";
$sql4 = "INSERT INTO studenti3 (data) VALUES ('$result2')";
$rez4 = mysql_query($sql4);
}
var_dump($dates);
}
$first = "06-04-2015";
$last = "07-05-2015";
$step = "+1day";
$output_format = 'd-m-Y';
date_range($first, $last, $step, $output_format); ?>

How to output records in HTML table from moodle database?

I want to display data from moodle database in HTML table, but for every record separate table is being displayed:
Code:
$rec=$DB->get_records_sql('SELECT * FROM `mdl_schedules`');
$table = new html_table();
$table->head = array('Date','Time', 'A' , 'B', 'C','D', 'E', 'F');
foreach ($rec as $records) {
$id = $records->id;
$scheduledatet = $records->scheduledate;
$scheduletime = $records->scheduletime;
$session = $records->s;
$venue = $records->v;
$trainer = $records->t;
$category = $records->c;
$course = $records->course;
$link = $records->link;
$table->data = array(array($scheduledatet, $scheduletime, $a,$b,$c,$d,$e,'View'));
echo html_writer::table($table);
}
Any reference or help will be much appreciated.
The echo should be outside the loop :)
}
echo html_writer::table($table);
But you will probably want to use flexible_table instead so you can use pagination.
Have a look in /admin/localplugins.php for an example.
I know this is old, but the reason you're only getting one record is you're not adding to the $table->data array. This is the code you want:
$rec=$DB->get_records('schedules');
$table = new html_table();
$table->head = array('Date','Time', 'A' , 'B', 'C','D', 'E', 'F');
foreach ($rec as $records) {
$id = $records->id;
$scheduledatet = $records->scheduledate;
$scheduletime = $records->scheduletime;
$session = $records->s;
$venue = $records->v;
$trainer = $records->t;
$category = $records->c;
$course = $records->course;
$link = $records->link;
$table->data[] = array($scheduledatet, $scheduletime, $a,$b,$c,$d,$e,'View');
}
echo html_writer::table($table);
I also changed up your query a bit. If you're just getting all of the records from a table, $DB->get_records('TABLE_NAME_WITHOUT_PREFIX'); is the way to go.

How do I do this sql query in Zend?

How do I do this sql query in Zend Framework, I need to some how do this in the PDO context I think? I tried ->query but not sure if I am getting this right. The three variables are user_id and to and from date.
SELECT
ss.subcategory_id,
ss.subcategory_name,
ss.subcategory_issaving,
IFNULL(SUM(m.mv_monthly_total),0) AS expendsum
FROM
(SELECT
s.subcategory_id,
s.subcategory_name,
s.subcategory_issaving
FROM
subcategory s
WHERE
s.subcategory_isexpend = 'Y'
AND
s.subcategory_issaving = 'Y') ss
LEFT JOIN
mv_monthly m
ON ss.subcategory_id = m.mv_monthly_subcategory_id
AND m.mv_monthly_user_id = 2
AND m.mv_monthly_month >= '2010-01-01'
AND m.mv_monthly_month <= '2020-01-01'
GROUP BY
ss.subcategory_id,
ss.subcategory_name,
ss.subcategory_issaving
ORDER BY
ss.subcategory_issaving DESC,
expendsum;
I have tried the following with no luck
$db = Zend_Db_Table::getDefaultAdapter();
$dbExpr1 = new Zend_Db_Expr("s.subcategory_id, s.subcategory_name, s.subcategory_issaving");
$dbExpr2 = new Zend_Db_Expr("ss.subcategory_id, ss.subcategory_name, ss.subcategory_issaving, IFNULL(SUM(m.mv_monthly_total),0) AS expendsum");
$select = $db->select()
->from(
array(
'ss' => new Zend_Db_Expr(
'('. $db->select()
->from(array("s" => "subcategory"), $dbExpr1)
->where("s.subcategory_isexpend = 'Y'")
->where("s.subcategory_issaving = 'Y'") .')'
)
),
$dbExpr2
)
->joinLeft(array("m" => "mv_monthly"), "ss.subcategory_id = m.mv_monthly_subcategory_id")
->where("m.mv_monthly_user_id = ?", $user_id)
->where("m.mv_monthly_month >= ?", $fromMonth)
->where("m.mv_monthly_month <= ?", $toMonth)
->group(array("ss.subcategory_id","ss.subcategory_name","ss.subcategory_issaving"))
->order(array("ss.subcategory_issaving DESC", "expendsum"));
$row = $db->fetchAll($select);
For such a complex query, you can just execute it directly rather than using the object oriented approach as it gets fairly complicated with a query like that.
Try something like this, replacing my query with yours, and binding your variables into the query:
$db = Zend_Db_Table::getDefaultAdapter();
$stmt = new Zend_Db_Statement_Pdo($db, 'SELECT a, b, c FROM a WHERE username = ? AND date = ?');
try {
$res = $stmt->execute(array($user_id, $fromMonth));
if ($res) {
$rows = $stmt->fetchAll();
}
} catch (Zend_Db_Statement_Exception $dbex) {
// log Query failed with exception $dbex->getMessage();
}
If you prefer to use the object oriented approach, or need to because some parts of the query will be conditional, I usually build by subqueries up first as their own select, and you can simply embed those in to the main query with the select object for the subquery.
Here is what I mean by that:
$subselect = $this->getDbTable()
->select()
->from('mytable', array('time' => 'max(time)', 'id'))
->where('id IN (?)', $serialNumbers)
->group('id');
$select = $this->getDbTable()
->select()
->setIntegrityCheck(false)
->from('mytable')
->join('other', 'mytable.id = other.id', array('label'))
->join(array('dt' => $subselect),
'(mytable.time, mytable.id) = (dt.time, dt.id)', '');