Typo3 extbase sql_fetch_assoc error

In Typo3 version 6.1, I have written some custom queries in My extension Repository

for example in file Mytest/Classes/Domain/Repository/MytestRepository.php

class MytestRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
 public function myFunction(){

        $sql = 'SELECT * FROM some_table ';

        $sqlResult = $GLOBALS['TYPO3_DB']->sql_query($sql);
        while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($sqlResult)) {
                $rowone = $row['rowone'];
        }
 }
}

And calling that function in controller

$test = $this->MytestRepository->myFunction();

But the issue here is, I am getting error

Fatal error: Call to a member function fetch_assoc() on a non-object in /home/src/typo3_src-6.1.1/typo3/sysext/core/Classes/Database/DatabaseConnection.php on line 1029 

Anybody have the solution ? Thanks in advance.

Upvotes: 0

Views: 2747

Answers (1)

Shufla
Shufla

Reputation: 872

You can execute custom queries like this:

$query = $this->createQuery();
$query->statement('SELECT * FROM some_table');
$result = $query->execute();

If you don't want to get objects as a result from the query, which i assume looking at your while loop, you can set the following line before executing the query:

$query->getQuerySettings()->setReturnRawQueryResult(TRUE);

With setReturnRawQueryResult you get a plain array as a result.

Upvotes: 2

Related Questions