Reputation: 856
I have the following sample schema of 2 models OrderDetail and Item, many-to-one relationship:
OrderDetail:
columns:
invoice_id: { type: integer, notnull: true }
item_id: { type: integer, notnull: true }
quantity: { type: integer, notnull: true }
transaction_price: { type: integer }
currency: { type: string(50), default: peso }
relations:
Invoice: { type: one, local: invoice_id, foreign: invoice_id }
Item: { type: one, local: item_id , foreign: item_id }
indexes:
invoice_to_item:
fields: [item_id, invoice_id]
type: unique
Item:
actAs: { Timestampable: ~, SoftDelete: ~ }
columns:
item_id: { type: integer, autoincrement: true, primary: true }
item_name: { type: string(100), notnull: true }
description: { type: text, notnull: true }
part_number: { type: string(50) }
size: { type: string(50) }
unit: { type: string(10) }
in_stock: { type: integer, notnull: true }
in_transit: { type: integer, notnull: true }
released: { type: integer, notnull: true }
borrowed: { type: integer, notnull: true }
borrower_name: { type: string }
item_type_id: { type: integer, notnull: true }
relations:
OrderDetails: { type: many, class: OrderDetail, local: item_id, foreign: item_id }
Prices: { type: many, class: Price, local: item_id, foreign: item_id }
In my form, I want to include information from the Item table such as item_name
and in_stock
.
What I simply want to do is that I want to display data from the Item table that is related to my form. I want to include this information in my form object for clarity and organization. I want to be able to do something like this in my form:
echo $form[$number]['Item']['part_number']->getValue()
// where $number is the index of the OrderDetail in the form and key 'Item' contains data for the Item related to this OrderDetail.
I tried embedding the Item relation in the OrderDetail such as:
class OrderDetailForm extends BaseOrderDetailForm {
public function configure() {
// some config here...
$this->embedRelation('Item');
}
}
The problem with this is when I save the form, the Item object is being validated and saved as well. I simply am using that information for display purposes. Here is a rough sample of how I want the form to be and what I wish to do:
OrderDetail: 1
Item: Headset
In Stock: 5pcs
Quantity: [input field here that can be changed by the user]
If there is anything unclear, please let me know. Also, if you can suggest a better approach, I'd really appreciate it.
Upvotes: 0
Views: 582
Reputation: 2893
If the OrderDetailForms are embedded inside the InvoiceForm and you want to access the Item object from these. Then I'd suggest you use something like this in your template:
// apps\myApp\modules\myModule\templates\editSuccess.php
$orderDetailForms = $invoiceForm->getEmbeddedForms();
foreach ($orderDetailForms as $key => $form) {
$item = $form->getObject()->getItem();
echo 'Item name: ' . $item->getItemName() . '<br />';
echo 'In stock: ' . $item->getInStock() . '<br />';
echo $form;
}
The thing to do here, is make sure your Item object is retrieved along with the OrderDetail objects when you do your query. Because that line ($form->getObject()->getItem()) would effectively perform a database query each time.
Upvotes: 1