add value on insert

add value on insert

mmontoyammontoya Posts: 84Questions: 27Answers: 4
edited July 2015 in Editor

After selecting a customer on the customerSearch page, the user is brought to another page customerDetail.php?id=3 (where the id is the customer id). On customerDetail I have a data table that shows appointments for that customer. It is filtering correctly so it is only showing appointments for customerID=3. However, when the user adds an appointment, the record is currently getting added with a null in the CustomerID field. I need the CustomerID field in this new record to automatically be populated with the CustomerID (3 in this example), which I have stored in a variable. How do I do this?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,777Questions: 1Answers: 10,112 Site admin
    Answer ✓

    Hi,

    I replied to your e-mail version of this question a little while ago, but for completeness and incase anyone else finds this thread and is interested, I'll reply here as well.

    It sounds like you want to make use of the Field->setValue() method:

    Allan

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4
    edited July 2015

    Perfect: Line 6 is what I needed.

    $cid= $_GET['id'];
    
    Editor::inst( $db, 'CustomerContactInfo', 'ContactID' )
        ->fields(
            Field::inst( 'CustomerContactInfo.CustomerID' )
                    ->setValue($cid),
            Field::inst( 'CustomerContactInfo.ContactTypeID' )
                    ->validator( 'Validate::notEmpty' )
                    ->options( 'ContactTypes', 'ContactTypeID', 'ContactType' ),
            Field::inst( 'ContactTypes.ContactType' ),      
            Field::inst( 'CustomerContactInfo.ContactValue' )
                    ->validator( 'Validate::notEmpty' ),
            Field::inst( 'CustomerContactInfo.IsPrimary' )
                    ->validator( 'Validate::notEmpty' ),
            Field::inst( 'CustomerContactInfo.CanText' )
                    ->validator( 'Validate::notEmpty' )
        )
        ->leftJoin('ContactTypes','CustomerContactInfo.ContactTypeID','=','ContactTypes.ContactTypeID')
        ->where('CustomerContactInfo.CustomerID',$cid)
        ->process( $_POST )
        ->json();
    
This discussion has been closed.