setValue() to 0

setValue() to 0

crush123crush123 Posts: 417Questions: 126Answers: 18
edited February 2015 in Editor

On Editor::ACTION_EDIT I am trying to setValue of a database (decimal) field to 0, if certain criteria are met,- but it stubbornly refuses.

Any other value is fine

if ( Editor::action( $_POST ) === Editor::ACTION_EDIT ) {
$returnvalue = $_POST['data']['tblorderdetails']['Returned'];

if ($returnvalue == 1) {//if item is returned, set the ReturnedDate field to today, set the PatronPayment field to 0
    $dateField
        ->set( Field::SET_BOTH )
        ->setValue( $today );
    $patronpaymentField
        ->set( Field::SET_BOTH )
        ->setValue( 0.01 );
}

}

if i setValue(0.01) to my field, (or any other numerical value, including negative values) the table is updated, but 0 does not update

(Using editor 1.4.0)

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Hi,

    In the Field.php file on line 458 (in the apply method) you found find:

    if ( ! $this->_setValue &&

    Could you change it to be:

    if ( $this->_setValue === null &&
    

    please? I think that should address the issue.

    Regards,
    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    Unfortunately, that doesn't make a difference,

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Odd - I've just tried it and it appears to work as expected. Can you show me your full PHP and also a schema dump for the DB table?

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited February 2015

    ok,

    table

    CREATE TABLE IF NOT EXISTS `tblorderdetails` (`DetailID` int(11) NOT NULL AUTO_INCREMENT,`DetailOrderID` int(11) DEFAULT NULL,  `DetailItemID` int(11) DEFAULT NULL,  `DetailPrice` decimal(5,2) DEFAULT NULL,  `DetailSKU` varchar(50) NOT NULL,  `DetailQuantity` int(11) DEFAULT NULL,  `Returned` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Item was returned  ?',  `ReturnedDate` date NOT NULL COMMENT 'Date of return, if applicable',  `PatronPayment` decimal(5,2) NOT NULL COMMENT 'AmountAllocated to Patron',  `PatronPaid` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Paid or Not',  `PatronPaidDate` date NOT NULL COMMENT 'Date Payment Made to Patron',  PRIMARY KEY (`DetailID`)) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=25 ;
    

    php

        <?php $today = date("Y-m-d");?>
    
        <?php
     
        // DataTables PHP library
        include( "../../../DataTables-1.10.4/extensions/Editor-PHP-1.4.0/php/DataTables.php" );
     
        // Alias Editor classes so they are easy to use
        use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Join,
        DataTables\Editor\Validate;
            
        $filter = isset($_GET['DetailOrderID'])?$_GET['DetailOrderID']:0;//filter recordset according to currently selected row
        if ( isset($_POST['action']) && $_POST['action'] === 'edit' ) {
        $filter = $_POST['data']['tblorderdetails']['DetailOrderID'];//if edit form has been posted filter recordset according to orderid of row being edited
        }//this will ensutre that the orders page is refreshed correctly
     
        $editor = Editor::inst( $db, 'tblorderdetails', 'DetailID' );
     
        $dateField =  Field::inst('tblorderdetails.ReturnedDate')
        ->set( Field::SET_NONE )
        ->getFormatter( 'Format::date_sql_to_format', 'd M Y' );
        
        $patronpaymentField =  Field::inst('tblorderdetails.PatronPayment')
        ->set( Field::SET_NONE );
     
        if ( Editor::action( $_POST ) === Editor::ACTION_EDIT ) {
        $returnvalue = $_POST['data']['tblorderdetails']['Returned'];
     
        if ($returnvalue == 1) {//if item is returned, set the ReturnedDate field to today, set the PatronPayment field to 0
            $dateField
                ->set( Field::SET_BOTH )
                ->setValue( $today );
            $patronpaymentField
                ->set( Field::SET_BOTH )
                ->setValue( 0.01 );
           }
          }
          $editor->
        field(
            $dateField,
            $patronpaymentField,
            Field::inst( 'tblorderdetails.DetailOrderID' ),
            Field::inst( 'tblorderdetails.DetailItemID' ),
            Field::inst( 'tblorderdetails.DetailPrice' ),
            Field::inst( 'tblorderdetails.DetailSKU' ),
            Field::inst( 'tblorderdetails.Returned' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                } )
        )
        ->where('tblorderdetails.DetailOrderID', $filter);
      
        $data = $editor
        ->process( $_POST )
        ->data();
        ?>
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    I don't understand that I'm afraid. With the change I suggested above your code looks like it should be working perfectly.

    We'll need to add some debug statements in to figure out what is happening. On line 546 you will find:

    $val = $this->_setValue !== null ?

    Immediately before it add:

    if ( $this->_setValue !== null ) {
      echo $this->name() .' '. $this->_getAssignedValue( $this->_setValue )."\n";
    }
    

    it will cause a JSON error, but when you look at the JSON return in your browser you should be able to see what Editor is trying to use as a value for the field.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited February 2015

    OK, apologies, I made a mistake.

    In the process of updating datatables to 1.10.5, I neglected to change the path on my page, so it was still pointing to 1.10.4.

    Let me do that again...

    I can confirm that changing line 458 in Fields.php to

    if ( $this->_setValue === null &&
    

    does indeed fix the problem with setting the value of PatronPayment to 0, however it causes a different one.

    Now the values in 2 other fields are cleared on edit

    The DetailPrice field is set to NULL and the DetailSKU to empty string

    Using the code you supplied to debug, the json returned is as follows...

    tblorderdetails.ReturnedDate 2015-02-17
    tblorderdetails.PatronPayment 0
    {"row":{"DT_RowId":"row_23","tblorderdetails":{"ReturnedDate":"17 Feb 2015","PatronPayment":"0.00","DetailOrderID":"15","DetailItemID":"251","DetailPrice":null,"DetailSKU":"","Returned":"1"}}}
    
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Odd! Can you show me the values that are being submitted by the client-side please? You'll be able to find them in the "Headers" of the Ajax request made in your browser's developer tools.

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited February 2015

    i can confirm that if line 458 in Fields.php reads...

    if ( ! $this->_setValue &&
    

    the row is updated fine, with the exception of when 0 is the value

    if i change line 458 to

    if ( ! $this->_setValue === null &&
    

    0 is updated OK, but the two fields mentioned above are set to NULL and ""

    Headers info (extract)...

    action:edit
    data[tblorderdetails][DetailItemID]:885
    data[tblorderdetails][Returned]:1
    data[tblorderdetails][DetailOrderID]:16
    id:row_24
    

    One thing I haven't done in the json is explicity ->set( Field::SET_NONE ); for these fields, but as the issue doesn't occur otherwise, i didn't think it necessary, (these fields do not appear in the editor instance)

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    if ( ! $this->_setValue === null &&

    That's not quite right. It should be:

    if ( $this->_setValue === null &&
    

    as I have above. That would probably explain the problem since the logic is being inverted!

    Allan

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    My bad.

    IATD.

    Thanks Allan

This discussion has been closed.