how to get Field value on PHP server file using Editor

how to get Field value on PHP server file using Editor

cpshartcpshart Posts: 246Questions: 49Answers: 5

I need to get the Field value of another Field on server file to check for a duplicate value

server file extracts below

Editor::inst( $db, 'dm_holdings' )
    ->debug(true)
    ->fields(
        Field::inst( 'dm_holdings.portfolio_id' )
            ->options( Options::inst()
                ->table( 'dm_portfolios' )
                ->value( 'id' )
                ->label( array('code', 'name') )
                ->where( function ( $q ) use ( $userid) {
                    $q->where( 'dm_portfolios.user_id', $userid);
                } )
            )

           ->validator( function($val, $data) { 
               global $portfolio_id;
               $portfolio_id = $val;
               return $portfolio_id;
               echo '$portfolio_id is:' .$portfolio_id.':';
           } )

I have an SQL statement below to check for duplicate values where I need to access the value entered for the dm_holdings.portfolio_id above but I do not know how to access another Field value.

           ->sql('SELECT COUNT(*) as count FROM dm_holdings WHERE stock_id = '.$val.' AND user_id = '.$_POST['userid'].' AND portfolio_id = '.$portfolio_id.' ');

Error Message is

Notice: Undefined variable: portfolio_id in /home/ukincome/public_html/Editor-PHP-1.9.0/controllers/stock_holdings_basice.php on line 64
{"fieldErrors":[{"name":"dm_holdings.portfolio_id","status":"16"}],"error":"An SQL error occurred: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1","data":[],"ipOpts":[],"cancelled":[],"debug":[{"query":"SELECT COUNT(*) as count FROM dm_holdings WHERE stock_id = 1293 AND user_id = 2 AND portfolio_id = ","bindings":[]}]}

I don't know how to access another Field value to use in the sql command

Note: If I exclude the check on portfolio_id it works, but I need the check on 3 values stock_id and portfolio_id and user_id for my requirements.

I can provide access and further details to my system by PM if required

Any help much appreciated

Best

Colin

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin
    Answer ✓

    Inside the validator $val should be the value that was submitted for dm_holdings.portfolio_id.

    To get the values of the other fields, you can access them with the $data object - e.g. $data[‘dm_holdings’][‘stock_id’] and $data[‘dm_holdings’][‘user_id’].

    Allan

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Allan

    Many thanks for your quick response, I will be able to test the changes later this afternoon and report back.

    Best Regards Colin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Allan

    I have just tested the following line in the script and all works as expected, great news.

    $res = $db ->sql('SELECT COUNT(*) as count FROM dm_holdings WHERE stock_id = '.$data['dm_holdings']['stock_id'].' AND user_id = '.$_POST['userid'].' AND portfolio_id = '.$data['dm_holdings']['portfolio_id'].'');    
    

    Just one observation, the type of single quote used is critical.

    Many Thanks

    Colin

This discussion has been closed.