Search code examples
databasevariablestypo3where-clausetyposcript

Variables in where-clause


I am on my first Typo3 project. I stuck on an effort now for several days. I searched here and googeled. But it seems, that my question is that simple, so that no one asks it :-)

I have a FLUIDTEMPLATE in my setup.typoscript (which came from sitepackagebuilder.com). Everything works, and also the websites looks like i wanted to.

BUT: I have to join a where-clause with variables. This is my Code:

page = PAGE
page {

    10 = FLUIDTEMPLATE
    10 {

        variables {
            
            dayNow = USER
            dayNow.userFunc = VereinLilaWinkel\LilaWinkelTemplate\Schultermine->dayNow

            day30 = USER
            day30.userFunc = VereinLilaWinkel\LilaWinkelTemplate\Schultermine->day30
        }

        dataProcessing {

            60 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
            60 {

                table = tx_mask_termin
                pidInList = 123
                selectFields = tx_mask_termin.*, usr_plz.plz, usr_plz.ort
                leftjoin = usr_plz ON usr_plz.uid = tx_mask_termin.tx_mask_plzort
                where = tx_mask_termin.tx_mask_datum BETWEEN {dateNow} AND {date30}
                orderBy = tx_mask_termin.tx_mask_datum ASC
                as = nextSchultermine
            }
        }
    }
}

The userFuncs work fine, i get back for example (today):

  • dateNow: 2023-05-02
  • date30: 2023-06-01

The goal is to get the where-statement working. As far, i can only hard-code it every day. So i know, that the rest of the query works fine.

I tried all kinds of wrap and dataWrap that i could find. I tried lib.myValues, or as constants... I also asked ChatGPT for it... No running version yet... The variables wont get rendered inside the where-clause!


Solution

  • Since both, the variables and the DataProcessor are defined on the same level and there is no nested dataProcessing your apporach has to fail.

    The reason is, that the variables are not available at the time the dataProcessing is going to happen, so you can't access them as data for the TypoScript of the DataProcessor.

    You could still achieve what you want with TypoScript though, since there are several date functions available via stdWrap.

    https://docs.typo3.org/m/typo3/reference-typoscript/main/en-us/Functions/Stdwrap.html?highlight=strtotime#strtotime

    60 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
    60 {
        table = tx_mask_termin
        pidInList = 123
        selectFields = tx_mask_termin.*, usr_plz.plz, usr_plz.ort
        leftjoin = usr_plz ON usr_plz.uid = tx_mask_termin.tx_mask_plzort
        where.cObject = COA
        where.cObject {
           10 = TEXT
           10.value = now
           10.strtotime = 1
           10.noTrimWrap = |tx_mask_termin.tx_mask_datum BETWEEN | AND                    
           20 = TEXT
           20.value = +30 day
           20.strtotime = 1
        }
        orderBy = tx_mask_termin.tx_mask_datum ASC
        as = nextSchultermine
    }
    

    But as mentioned in the comment for Sivas solution, I would recommend a custom DataProcessor instead.