Hello,
I built a nested editor one for products and another one for prices. the relationship is one-to-many. 1 product can have multiple prices. everything is working fine but when selecting the product and editor open I need to get the prices for the selected products.
When I replace this one for example
$q->where('prices.product_id','products.id');
with
$q->where('prices.product_id','1');
I got the correct prices but this is a fixed value. How can I assign products.id in a dynamic when the row is selected and the editor is open?
and also, when I edit the price row in the nested editor, the new editor opens but it's empty. it should have the values of the selected price.
**prices.php controller. **
<?php
session_start();
/*
* Example PHP implementation used for the index.html example
*/
// DataTables PHP library
include( "../lib/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
function logChange ( $db, $action, $id, &$values ) {
$db->insert( 'log', array(
'user' => $_SESSION['name'],
'action' => $action,
'values' => json_encode( $values ),
'row' => $id,
'when' => date('Y-m-d H:i:s')
) );
}
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'prices' )
->fields(
Field::inst( 'prices.id' ),
Field::inst( 'prices.price' ),
Field::inst( 'prices.start_date' ),
Field::inst( 'prices.end_date' ),
Field::inst( 'prices.product_id' )
)
->leftJoin('products', 'products.id', '=', 'prices.product_id')
->where(function ($q) {
$q->where('prices.product_id','products.id');
})
->on( 'postCreate', function ( $editor, $id, &$values, &$row ) {
logChange( $editor->db(), 'create', $id, $values );
} )
->on( 'postEdit', function ( $editor, $id, &$values, &$row ) {
logChange( $editor->db(), 'edit', $id, $values );
} )
->debug(true)
->process( $_POST )
->json();
products.php controllers:
<?php
session_start();
/*
* Example PHP implementation used for the index.html example
*/
// DataTables PHP library
include( "../lib/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
function logChange ( $db, $action, $id, &$values ) {
$db->insert( 'log', array(
'user' => $_SESSION['name'],
'action' => $action,
'values' => json_encode( $values ),
'row' => $id,
'when' => date('Y-m-d H:i:s')
) );
}
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'products' )
->fields(
//Field::inst( 'products.id' ),
//Field::inst( 'image' ),
Field::inst( 'products.internalcode' ),
Field::inst( 'products.batch' ),
Field::inst( 'products.brand' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'A Brand is required' )
) ),
Field::inst( 'products.vendorcode' ),
/* Field::inst( 'price_row.price' )
->validator( Validate::numeric() )
->setFormatter( Format::ifEmpty(null) ), */
Field::inst( 'products.sellingprice' )
->validator( Validate::numeric() )
->setFormatter( Format::ifEmpty(null) ),
Field::inst( 'products.name' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'A Name is required' )
) ),
Field::inst( 'products.collection' ),
Field::inst( 'products.modale_category' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'A Category is required' )
) ),
Field::inst( 'products.modale_subcategory' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'A Sub Category is required' )
) ),
Field::inst( 'products.dimensions' ),
Field::inst( 'products.designedby' ),
Field::inst( 'products.materialfinishes' ),
Field::inst( 'products.specifications' ),
Field::inst( 'products.description' ),
Field::inst( 'products.catalog_type' )
->validator( Validate::notEmpty( ValidateOptions::inst()
->message( 'A Catalog Type is required' )
) ),
Field::inst( 'products.colors' ),
Field::inst( 'products.unit' ),
Field::inst( 'products.weight' ),
Field::inst( 'products.image_url' ),
Field::inst( 'products.id' )
)
->join(
Mjoin::inst('files')
->link('products.id', 'products_files.product_id')
->link('files.id', 'products_files.file_id')
->fields(
Field::inst('filename'),
Field::inst('id')
),
Mjoin::inst('prices')
->link('products.id', 'prices.product_id')
->fields(
Field::inst('price')
)
)
->on( 'postCreate', function ( $editor, $id, &$values, &$row ) {
logChange( $editor->db(), 'create', $id, $values );
} )
->on( 'postEdit', function ( $editor, $id, &$values, &$row ) {
logChange( $editor->db(), 'edit', $id, $values );
} )
->debug(true)
->process( $_POST )
->json();
?>
Attached also a screenshot of the nested editors.