V3.0.3 - some problems with setMasterDetail

setMasterDetail

The documantation is rather poor on how this can actually work. Following the example did not helped me.
Is there some files needed to include to orders.php?
Is there any changes needed in view.php ? Can you post a more detailed example?

export to pdf does not work

Hello @Alcibiades ,

There is not any other changes you need to be honest. What issue are you experiencing? What framework do you use? Maybe if you can provide us some example and maybe a screenshot of the issue that you have?

Regards
Johnny

test.php

include("libraries/autoload.php");

use GroceryCrud\Core\GroceryCrud;

$cont_id=210;    

$lng = isset($_GET["l"]) ? $_GET["l"] : "el";


$rel_items = 'items210el';

$rel_menus= 'menus'.$cont_id.$lng ;

$items_db = 'items'.$cont_id.$lng ;

$database = include('database.php');

$config = include('config.php');


$crud = new GroceryCrud($config, $database);

$crud->setTable($items_db);

$crud->setSubject('Γραμμής Menu','Menu');

$crud->setMasterDetail("https://scan4.menu/xon/aegean.php");


if ($lng=="el") {

$crud->columns(['last_update', 'language', 'title', 'description','extra_line', 'menu_id', 'placement', 'price', 'is_sub', 'isv']);

$crud->fields(['last_update', 'language', 'title', 'description','extra_line', 'menu_id', 'placement', 'price', 'is_sub', 'isv']);

$crud->requiredFields(['language', 'title', 'menu_id','placement']);


$crud->displayAs([
    'last_update'=> 'Τελευταία Ενημέρωση',
    'language'=> 'Γλώσσα',
    'title'=> 'Τίτλος',
    'description'=> 'Περιγραφή',
    'isv'=> 'Ορατό;',
    'is_sub'=> 'Υπότιτλος;', 
    'menu_id'=> 'Menu',
    'placement' => 'Θέση',
    'price' => 'Τιμή',
    'extra_line' => 'Extra Γραμμή'
]);

//$crud->fieldType('isv', 'checkbox_boolean');

//$crud->fieldType('is_sub', 'checkbox_boolean');


// $crud->setFieldUpload('Link', '../img/menu', 'https://scan4.menu/img/menu');

$crud->setRelation('menu_id', $rel_menus, 'title');


$crud->defaultOrdering([
   'menu_id' => 'ASC',
   'placement' => 'ASC'
]);   

    } else {


$crud->columns(['last_update', 'language', 'title', 'description','original_item_id', 'menu_id']);

$crud->fields(['last_update', 'language', 'title', 'description','original_item_id', 'menu_id']);

$crud->requiredFields(['language', 'title','original_item_id', 'menu_id']);


$crud->displayAs([
    'last_update'=> 'Τελευταία Ενημέρωση',
    'language'=> 'Γλώσσα',
    'title'=> 'Τίτλος',
    'description'=> 'Περιγραφή',
    'original_item_id' => 'Στη Βασική Γλώσσα',
    'menu_id'=> 'Menu'
]);

$crud->setRelation('menu_id', $rel_menus, 'title');

$crud->setRelation('original_item_id', $rel_items, 'title');

/
$crud->defaultOrdering([
   'menu_id' => 'ASC']); 
}


/* $crud->callbackBeforeInsert(function ($stateParameters) use ($cont_id) {
    $stateParameters->data['contact_id'] = $cont_id;
    $stateParameters->data['last_update'] = date('Y-m-d');
    return $stateParameters; 
});   

$crud->callbackBeforeUpdate(function ($stateParameters) use ($cont_id) {
    $stateParameters->data['contact_id'] = $cont_id;
    $stateParameters->data['last_update'] = date('Y-m-d');
    return $stateParameters; 
});   

*/

$crud->setClone();


$output = $crud->render();



if ($output->isJSONResponse) {

    header('Content-Type: application/json; charset=utf-8');

    echo $output->output;

    exit;

}


$css_files = $output->css_files;

$js_files = $output->js_files;

$output = $output->output;


include('view.php');

view.php


<html>

<head>

    <meta charset="utf-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <?php

    foreach($css_files as $file): ?>

        <link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />

    <?php endforeach; ?>


</head>

<body>

<div style="padding: 20px 10px;">

    <?php echo $output; ?>

</div>

<?php foreach($js_files as $file): ?>

    <script src="<?php echo $file; ?>"></script>

<?php endforeach; ?>

</body>

</html>

aegean.php

include("libraries/autoload.php");

use GroceryCrud\Core\GroceryCrud;

$database = include('database.php');

$config = include('config.php');

$c = new GroceryCrud($config, $database);

$c->setLanguage('English');

$c->setTable('items210en');

$c->unsetDatagridTitle();

$c->unsetTools();

$c->unsetOperations();

$c->unsetSearchColumns(['last_update', 'language', 'title', 'description', 'original_item_id', 
'extra_line','menu_id', 'contact_id']);

 if (!empty($_POST['id'])) {


        $c->where(['original_item_id' => $_POST['id']]);
    } else {
        throw new InvalidArgumentException("Invalid argument for the field 'master_id'");
    }
} 

$output = $c->render();
?>

first of all if i do not use $c->setLanguage(‘English’)
i get a missconfiguration message

if i do the message is
We can’t process the response of the server. Not well JSON formatted response.

In any case i see that no javascript files echoed

<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
</head>
<body>
<div style="padding: 20px 10px;">
    <script>
    window.GroceryCrudConfiguration = window.GroceryCrudConfiguration || {};

    Object.assign(window.GroceryCrudConfiguration, {
        assetsUrl: 'assets/vendor/grocery-crud/',
        autoLoad: true,
    });

            setTimeout(() => {
            // Search for class name ".gc-hidden-message"
            const gcHiddenMessage = document.querySelector('.gc-hidden-message');
            const gcGifLoader = document.querySelector('.gc-gif-loader');
            if (gcHiddenMessage) {
                gcHiddenMessage.style.display = 'block';
            }
            if (gcGifLoader) {
                gcGifLoader.remove();
            }
        }, 3000);
    
    </script>

<div class="grocery-crud" data-api-url="/xon/test.php" data-landing-page-url="/xon/test.php" data-theme="bootstrap-v5"data-unique-id="c74984b74f5c0e38ad3d7ea013335349" data-publish-events="true">
    <div style="padding: 10px; border: 1px solid #aaa; border-radius: 10px;margin-bottom: 20px;display: none;" class="gc-hidden-message">
        Ooooops, something went wrong! If you can see this message, this is probably a misconfiguration in Grocery CRUD Enterprise!
    </div>
        <div class="gc-gif-loader" style="width: 16px; height: 16px; background-repeat: no-repeat; background-image: 
....
.......
</div>
</div>
    <script src="assets/vendor/grocery-crud/static/js/bundle.v3.0.3.71331119.js"></script>
    <script src="assets/vendor/grocery-crud/static/js/93.503d461b.chunk.js"></script>
    <script src="assets/vendor/grocery-crud/static/js/main.29095ca1.chunk.js"></script>
</body>
</html>

i have tried for hours but can not find what is wrong with the code

it works fine in all previous versions and strange enough it works in the newest version too - besides when i click the arrow to load the second table

The mistake from the above code is at aegean.php file. You should also add the same logic for render as per test.php. For example instead of:

$output = $c->render();
?>

You should have:

$output = $c->render();



if ($output->isJSONResponse) {

    header('Content-Type: application/json; charset=utf-8');

    echo $output->output;

    exit;

}


$css_files = $output->css_files;

$js_files = $output->js_files;

$output = $output->output;


include('view.php');

instead. Let me know if that worked for you.

Regards
Johnny

thanks for the fast response.
It works in a way - the second table is loaded with no errors
But probably the post method does not work
because i get the whole table when i use

if (!empty($_POST[‘id’])) {
if (is_numeric($_POST[‘id’])) {
$c->where([‘original_item_id’ => $_POST[‘id’]]);
} else {
throw new InvalidArgumentException(“Invalid argument for the field ‘master_id’”);
}
}

and an empty table when i just use
$c->where([‘original_item_id’ => $_POST[‘id’]
so $_POST[‘id’] is empty

and this warning is in the console
DevTools failed to load source map: Could not load content for https://sca-----u/xon/assets/vendor/grocery-crud/css/bootstrap-v5/bootstrap.min.css.map: Unexpected end of JSON input

(the path is the right one)

Hello @Alcibiades ,

There is one more mistake here. You should expect the ‘master_id’ and not the ‘id’ So in your case you should change it to:

if (!empty($_POST['master_id'])) {
    if (is_numeric($_POST['master_id'])) {
        $c->where(['original_item_id' => $_POST['master_id']]);
    } else {
        throw new InvalidArgumentException("Invalid argument for the field 'master_id' ");
    }
}

Also about the error on devTools about source map don’t bother a lot since this is not a real issue. I will keep it in mind though to remove this issue in the future.

Regards
Johnny

I have tried setMasterDetail in much more complicated code now and works smoothly. Thanks.

1 Like