Domanda

I am trying to execute a query in Extbase repository, but it is not return any result.

This is my syntax and if anybody knows the problem, please help.

In repository file;

public function getImages( $uidOfCE, $pid ) {

    $query = $this->createQuery(); 
    $query->statement('SELECT sf.identifier,sfm.title,sfr.description 
        FROM tt_content AS tc 
        LEFT JOIN sys_file_reference sfr ON sfr.`uid_foreign` = tc.uid 
        LEFT JOIN sys_file AS sf ON sf.`uid` = sfr.uid_local 
        LEFT JOIN sys_file_metadata AS sfm ON sfm.`file` = sf.uid 
        WHERE tc.uid = '.$uidOfCE.' 
        AND tc.pid='.$pid.' 
        AND tc.list_type = "myimage_gallery" 
        AND sfr.`deleted` = 0 AND sfr.`hidden`= 0  
        AND tc.`deleted` = 0 AND tc.`hidden`= 0  AND  tc.sys_language_uid ='.$GLOBALS['TSFE']->sys_language_uid.' 
        AND sfr.fieldname = "tx_myext_images" AND sfr.tablenames = "tt_content"');

    return $query->execute();
}

In controller:

$myimagegalleries = $this->myimagegalleryRepository->getImages(
$uidOfCE, $this->cObj->data['pid'] );

Is it possible to create custom queries in extbase repository?

È stato utile?

Soluzione

$Query = $this->createquery();
$Query->getQuerySettings()->setReturnRawQueryResult(TRUE);
$Query->getQuerySettings()->setRespectStoragePage(FALSE);
$Query->statement('your query'); 
return $Query->execute();

In your controller

class your_Controller_name extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController { 

    protected $myimagegalleryRepository;

    public function yourAction() {
        $myimagegalleries = $this->myimagegalleryRepository->getImages(
        $uidOfCE, $this->cObj->data['pid'] );
    }
}

setReturnRawQueryResult if true you will get result in array.

Try to avoid statement see warning here: http://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html

Altri suggerimenti

What you do isn't encouraged if it can be solved with Extbase QOM, but it works.

If it doesn't return any result, your query constraints may not be set properly.

Debug $uidOfCEand $pid to find out whether they are set. If not, fix them. If they are, execute the whole query in MySQL/phpMyAdmin and see if you get any result. If you don't, fix your query to have it working properly.

We can't guess what's wrong with your query if you can't do it... the easiest way to find query problems is enabling statement logging, so you can copy it and try to run using common DB GUI, ie' like in this tip (also check Mateng's answer).

My personal shoot it's missing config for storage PID, anyway for sure testing statements will give you correct answer, or at least some sensible clue.

In (rare) cases when one needs to run complex sql (especially insert, update, delete) statements, it is possible to get access to the exec method of the Doctrine DBAL Connection object, which is an ancestor of the Extbase Connection object:

use TYPO3\CMS\Core\Database\Connection;

class MyConnection
{
    /**
     * myExec
     * Exposes the exec method of the Doctrine DBAL which is non public
     * in the Extbase Connection object. This is feasible, using
     * PHP Reflection Object and ReflectionMethod.
     *
     * @param $conn
     * @param $statement
     *
     * @return int
     */
    public function myExec(Connection $conn, $statement)
    {
        $ro = new \ReflectionObject($conn);
        $rm = $ro->getMethod('exec');
        $rm->setAccessible(true);
        $i = $rm->invoke($conn, $statement);
        return $i;
    }

}

Please have in mind that this method bypasses all the anti sql injection mechanisms provided by the framework, so use it with extreme care at your own risk.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top