Create pivot with value using inline editing

Create pivot with value using inline editing

DTUser560425DTUser560425 Posts: 1Questions: 1Answers: 0

Hi,

I'm leftjoining a table to a pivot table with a price value, as expected items without an entry in the pivot table have no value in the price column.

Would it be possible to create an entry in the pivot table when an empty price column is filled out using inline editing?
From what I can tell this isn't standard and I'm having no luck trying to use the 'preEdit' event to do it manually.

$data = Editor::inst( $db, 'items', 'id' )
    ->fields(
        Field::inst( 'items.id' ),
        Field::inst( 'items.name' ),
        Field::inst( 'items.code' ),
        Field::inst( 'items.product_code' ),
        Field::inst( 'item_prices.price'),
    )
    ->leftJoin( 'item_prices', 'items.id', '=', 'item_prices.item_id' )
    ->process( $_POST )
    ->json();

Answers

  • allanallan Posts: 53,621Questions: 1Answers: 8,318 Site admin

    Currently no - I'm afraid not. As you say, the only way to do this is with a server-side event such as postCreate.

    In this case perhaps:

    ->on('postCreate', function ($editor, $id, $values, $row) }
      $editor->db()->insert(
        'item_prices',
        [
          'item_id' => $id,
          'price' => $values['item_prices']['price']
        ]
      );
    });
    

    This is the reference doc for the database class btw.

    A similar approach could be done for inline editing if the links rows might not be there, or if the joined column can be inline edited (see the Database->push() method for an upsert).

    Regards,
    Allan

Sign In or Register to comment.