I'm afraid on StackOverflow this question might be rejected as "opinion based".

I'm using CakePHP 3.7.9 and in one View I have a form with a select and a number input:

echo $this->Form->control('product_id', ['options' => $products, 'empty' => true]);
echo $this->Form->control('price');

The controller pass the list for products, among others:

public function add($id)
    $itemOrder = $this->ItemOrders->newEntity();
    $itemOrder['order_id'] = $id;
    if ($this->request->is('post')) {
        $itemOrder = $this->ItemOrders->patchEntity($itemOrder, $this->request->getData());
        if ($this->ItemOrders->save($itemOrder)) {
            $this->Flash->success(__('The {0} has been saved.', 'Item Order'));

            return $this->redirect(['controller' => 'orders', 'action' => 'index']);
        $this->Flash->error(__('The {0} could not be saved. Please, try again.', 'Item'));
    $orders = $this->ItemOrders->Orders->find('list');
    $itemOrdersTypes = $this->ItemOrders->ItemOrdersTypes->find('list');
    $products = $this->ItemOrders->Products->find('list', ['order' => ['Products.code' => 'ASC']]);
    $itemOrdersStates = $this->ItemOrders->ItemOrdersStates->find('list');
    $deliveryNotes = $this->ItemOrders->DeliveryNotes->find('list');
    $proformas = $this->ItemOrders->Proformas->find('list');
    $invoices = $this->ItemOrders->Invoices->find('list');
    $this->set(compact('itemOrder', 'orders', 'itemOrdersTypes', 'products', 'itemOrdersStates', 'deliveryNotes', 'proformas', 'invoices'));

When the user select a product from the list, I want to propose the current price in the related field. This information should be retrieve from the Products table, using the productId as foreign key.

I see two ways to accomplish this task:

  • use Ajax to send to the server the productId, perform on the server the query, and transmit back the result

  • given that the product's count is not big (< 200), the controller might pass a map of `○6 ready to be consumed by a simple javascript function.

I'm able to implement both of these solutions, but my knowledge in web-programming cannot tell me which is the best.

My thoughts: the first one should be the most elegant, but requires a lot of code just to retrieve a single value. The second one is very simple but cannot "scale" if (unlikely within this project) the number of products will grow a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.