我有下面的表架构,工作正常。唯一的问题是我想在order_products表中设置数量值。我无法弄清楚如何做到这一点。我用来创建订单的代码如下。
<database name="mysql">
<table name="products" namespace="Flex\Shop\Models" package="../../packages/flex/shop/src/Models" phpName="product">
<column name="id" type="integer" primaryKey="true" autoIncrement="true" />
<column name="name" type="varchar" size="255" />
<column name="price" type="integer" />
</table>
<table name="orders" namespace="Flex\Shop\Models" package="../../packages/flex/shop/src/Models" phpName="order">
<column name="id" type="integer" primaryKey="true" autoIncrement="true" />
<column name="delivery_address_id" type="integer" />
<column name="invoice_address_id" type="integer" />
<column name="person_id" type="integer" />
<column name="amount" type="integer" />
<column name="status" type="enum" valueSet="unpaid,paid,shipped,cancelled" />
<foreign-key foreignTable="addresses" phpName="deliveryAddress">
<reference local="delivery_address_id" foreign="id" />
</foreign-key>
<foreign-key foreignTable="addresses" phpName="invoiceAddress">
<reference local="invoice_address_id" foreign="id" />
</foreign-key>
<foreign-key foreignTable="persons" phpName="person">
<reference local="person_id" foreign="id" />
</foreign-key>
<behavior name="timestampable" />
</table>
<table isCrossRef="true" name="order_products" namespace="Flex\Shop\Models" package="../../packages/flex/shop/src/Models">
<column name="order_id" type="integer" primaryKey="true" />
<column name="product_id" type="integer" primaryKey="true" />
<column name="quantity" type="integer" />
<foreign-key foreignTable="orders" phpName="order">
<reference local="order_id" foreign="id" />
</foreign-key>
<foreign-key foreignTable="products" phpName="product">
<reference local="product_id" foreign="id" />
</foreign-key>
</table>
</database>
php代码:
<?php
$order = new Order();
$order->setAmount(0); // Calculate total amount
$order->setDeliveryAddress($address);
$order->setInvoiceAddress($address);
$order->setPerson($person);
$order->setStatus('unpaid');
$order->addProduct($product);
$order->save();