Custom Function Converter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    Custom Function Converter

    I am trying to add a custom function converter to be able to use the mySQL function DATE_ADD. How do I pass the string INTERVAL 1 YEAR as a parameter?


    PHP Code:
    use Espo\ORM\QueryComposer\Part\FunctionConverter;
    
    class DateAdd implements FunctionConverter
    {
    public function convert(string ...$argumentList): string
    {
    return 'DATE_ADD(' . implode(', ', $argumentList) . ')';
    }
    }
    PHP Code:
    $query = (new SelectBuilder())
    ->select([
    ['DATE_ADD:(createdDate,INTERVAL 1 YEAR)','someDate']
    ]); 
    
    This results in $argumentList coming into this function as:

    $argumentList= [["created_date","i_n_t_e_r_v_a_l1_y_e_a_r"]] but I want $argumentList= [["created_date","INTERVAL 1 YEAR"]] ?




  • czcpf
    Senior Member
    • Aug 2022
    • 160

    #2
    UPDATE:

    Here is one such solution for anyone that needs this. If there is a better way please advise.


    PHP Code:
    use Espo\ORM\QueryComposer\Part\FunctionConverter;
    
    class DateAdd implements FunctionConverter
    {
    public function convert(string ...$argumentList): string
    {
    if( sizeof($argumentList) > 1 ) {
    $argumentList[1] = str_replace("'", "", $argumentList[1]);
    }
    
    return 'DATE_ADD(' . implode(', ', $argumentList) . ')';
    }
    }
    USE
    PHP Code:
    $query = (new SelectBuilder())
    ->select([
    ['DATE_ADD:(createdDate,\'INTERVAL 1 YEAR\')','someDate']
    ]);

    Comment

    Working...