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

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();

Related

Typo3 9, MM-Relation loses translation if selected multiple

I created an extension with different MM-Relations. In example Products and Accessories. All accessories can be selected to every product. The problem is, if i select one accessory to one product the translation to another language works well. If i select one accessory to multiple products, the translation always uses the default language and no translation is shown. Could anyone please help me find a solution?
Im using Typo3 9.5
Many thanks.
public function findByUid($uid) {
$uidArray = explode(",", $uid);
$query = $this->createQuery();
$query->getQuerySettings()->setRespectSysLanguage(true);
foreach ($uidArray as $key => $value) {
$constraints[] = $query->equals('sys_language_uid', $value);
$constraints[] = $query->equals('uid', $value);
$constraints[] = $query->equals('l10n_parent', $value);
}
$query->matching(
$query->logicalAnd(
$query->logicalOr(
$constraints
),
$query->equals('hidden', 0),
$query->equals('deleted', 0)
)
);
}

How to write an extbase Repository-Method for Update in TYPO3?

I have written an update query in TYPO3, Now I need to change it to query-object repository method. How to change the code below?
public function updatePaymentDetails($uid, $txnID, $amt, $stats)
{
$itemUID = $uid;
$transID = $txnID;
$amountPaid = $amt;
$txStatus = $stats;
$tableName = 'tx_paypalpayment_domain_model_transactions AS tpp';
$whereCondition = 'tpp.uid=' . '"' . $itemUID . '"';
$setValues = ['transactionid' => $transID, 'amount' => $amountPaid, 'txnstatus' => $txStatus];
$result = $GLOBALS['TYPO3_DB']->exec_UPDATEquery($tableName, $whereCondition, $setValues);
return $result;
}
I created this much in my own idea (don't know it is correct or not), What should be the remaining portion?
public function paymentUpdate($uid, $txnID, $amt, $stats) {
$query = $this->createQuery();
$query->matching(
$query->logicalAnd(
$query->equals("transactionid", $txnID),
$query->equals("amount", $amt),
$query->equals("txnstatus", $stats)
)
);
/*--- Update Code ---*/
return $query->execute();
}
Is there any way to do that?
The TYPO3/Extbase way is to first fetch your transaction from the persistence layer then apply your changes to the domain object and then update it in your repository.
Something like below in your controller action:
$transaction = $this->transactionRepository->findByIdentifier($itemUid);
$transaction->setTransactionId($transID);
$transaction->setAmount($amountPaid);
$transaction->setStatus($txStatus);
$this->transactionRepository->update($transaction);
If you wants to do a direct update instead of first fetching the record then take a look at the \TYPO3\CMS\Core\Database\Query\QueryBuilder (Only exists in newer versions of TYPO3 - 8.7 and above). In older versions of TYPO3 you could take a look at $GLOBALS['TYPO3_DB']->exec_*.

TYPO3 - Using SQL operators in QueryBuilder API v8

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 ...

syntax for selecting the last recordset in my model

I tried different possibilities but nothing worked, in the tutorial I couldn't find an example either.
I have a method in my modelclass:
public function getlastImport($filename)
{
//$id = (int) $id;
$rowset = $this->tableGateway->select(['Path' => $filename]);
$row = $rowset->current();
if (! $row) {
throw new RuntimeException(sprintf(
'Could not find row with identifier %d',
$id
));
}
return $row;
}
I want to retrieve the last import of a given filename, so ist must be like in sql:
select max(ID) from table where filename = $filename;
But how would be the right syntax in this case?
The sql query should be
"SELECT * FROM table_name WHERE filename={$filename} ORDER BY id DESC LIMIT 1"
Use as the following in your model
public function getlastImport($filename)
{
$select = $this->tableGateway->getSql()->select();
$select->columns(array('id', 'filename', 'label'));
$select->where(array('filename' => $filename));
$select->order("id DESC");
$select->limit(1);
$result = $this->tableGateway->selectWith($select);
$row = $result->current();
if (! $row) {
throw new RuntimeException(sprintf(
'Could not find row with identifier %d',
$id
));
}
return $row;
}
Hope this would help you!

insert_id mysqli

I'm trying to return the inserted id from a mysql INSERT query. Each time I run the function I get 0 as the result. Can someone please explain at what point I can retrieve the value because although the script below executes I cannot retireve the inserted id. Probably done something stupid.
<?php
public function execSQL($sql, $params, $close){
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$stmt = $mysqli->prepare($sql) or die ("Failed to prepared the statement!");
call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
$this->insert_id($this->connection);
$stmt->execute();
if($close){
$result = $mysqli->affected_rows;
} else {
$meta = $stmt->result_metadata();
while ( $field = $meta->fetch_field() ) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $this->refValues($parameters));
while ( $stmt->fetch() ) {
$x = array();
foreach( $row as $key => $val ) {
$x[$key] = $val;
}
$results[] = $x;
}
$result = $results;
}
$stmt->close();
$mysqli->close();
return $result;
}
?>
Check $mysqli->insert_id after executing insert query.