1

我第一次将 Datamapper ORM 与 CRUD 的数组htmlforms扩展一起使用。到目前为止,这两个扩展都适用于我的所有模型,但今天我面临一个模型的奇怪问题。

当我尝试保存对象时,我收到此 SQL 错误:

A Database Error Occurred
Error Number: 1054
Unknown Column 'orderitems.orderitem_id' in where clause

SELECT COUNT(*) AS `numrows` FROM (`orderitems`) WHERE `orderitems`.`order_id` = 2 AND `orderitems`.`orderitem_id` NOT IN (8, 9)

Filename: libraries/datamapper.php
Line Number: 2526

我认为正确的 SQL 应该是...AND orderitemsidNOT IN (...)因为 Orderitem 模型引用的是自身而不是另一个相关模型。此错误在 count() 方法内部触发,该方法由数组DM 扩展的 from_array() 方法触发的 save() 方法触发。

这是我的控制器代码

//Get order
$order = new Order(2);

//Fields to render in the form
$fields = array('orderitem');

//Save changes from $_POST
if($post = $this->input->post())
{
$order->trans_begin();
if ( ! $order->from_array($post, $fields, TRUE) OR $order->trans_status() === FALSE)
$order->trans_rollback();
else
$order->trans_commit();
}

//Load view
$data = array(
'order' => $order,
'fields'=> $fields,
);
$this->load->view('order/edit', $data);

这是我的查看代码

echo $order->render_form($fields);

这是隐含的模型

class Order extends DataMapper {
public $has_one = array('orderstatus', 'place', 'user', 'paymenttype');
public $has_many = array('orderitem');
...
/*SQL:
CREATE TABLE IF NOT EXISTS orders (
id mediumint(4) unsigned NOT NULL auto_increment PRIMARY KEY,
user_id mediumint(1) unsigned, FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE RESTRICT,
orderstatus_id tinyint(1) unsigned, FOREIGN KEY (orderstatus_id) REFERENCES orderstatuses(id) ON UPDATE CASCADE ON DELETE RESTRICT,
place_id tinyint(1) unsigned, FOREIGN KEY (place_id) REFERENCES places(id) ON UPDATE CASCADE ON DELETE SET NULL,
paymenttype_id tinyint(1) unsigned, FOREIGN KEY (paymenttype_id) REFERENCES paymenttypes(id) ON UPDATE CASCADE ON DELETE SET NULL,
total float NOT NULL DEFAULT 0
) ENGINE = InnoDB;
*/

class Orderitem extends DataMapper {
public $has_one = array('order', 'orderitemstatus');
public $has_many = array('orderitemextra');
...
/*SQL:
CREATE TABLE IF NOT EXISTS orderitems (
id int(1) unsigned NOT NULL auto_increment PRIMARY KEY,
order_id mediumint(1) unsigned, FOREIGN KEY (order_id) REFERENCES orders(id) ON UPDATE CASCADE ON DELETE CASCADE,
orderitemstatus_id tinyint(1) unsigned, FOREIGN KEY (orderitemstatus_id) REFERENCES orderitemstatuses(id) ON UPDATE CASCADE ON DELETE RESTRICT,
name varchar(128) NOT NULL,
price float NOT NULL,
) ENGINE = InnoDB;
*/

正如我所说,我对其他模型没有任何问题,所以我不知道这是 DM 问题还是我的代码或任何模型定义的问题。

我会很感激任何帮助。

谢谢!

4

0 回答 0