dsbifvcxr458755493 2013-05-29 05:09
浏览 21
已采纳

CakePHP查找数据但不删除它

I'm moving data from one table to another, both are labeled identically. As soon as the data is transfered from the orders table to the archived_orders table, it should then be deleted from the orders table

Both tables have the exact same structure except for the names. One is called orders and the other is archived_orders

CREATE TABLE `archived_orders` (
  `orderid` int(10) default NULL,
  `userid` int(10) default NULL,
  `order_status` varchar(20) default NULL,
  `email` varchar(50) default NULL,
  `total` varchar(50) default NULL,
  `fullName` varchar(60) default NULL,
  `address` varchar(50) default NULL,
  `city` varchar(50) default NULL,
  `state` varchar(50) default NULL,
  `zip` varchar(15) default NULL,
  `created` datetime default NULL,
  `modified` datetime default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Here is the code to deal with the transfer and deleting

$data = $this->Order->find('all',array(
     'conditions' => array('order_status'=> "filled"
     'userid' => AuthComponent::user('id'))
));
$this->loadModel('ArchivedOrder');
$archived_data = array();
foreach($data as $order => $o) {
     $archived_data[]['ArchivedOrder'] = $o['Order'];  
}
if($this->ArchivedOrder->SaveAll($archived_data)){ 
     $this->Order->deleteAll(array(
       'conditions' => array('order_status'=> "filled",
        'userid' => AuthComponent::user('id'))
    ),$cascade = true, $callbacks = false);
}

The data is successfully saved into the archived_orders table, but I get the following error related to the deletion. It's trying to find a column labeled 'id' but I never specify it.

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Order.id' in 'field list'
SQL Query: SELECT `Order`.`id` FROM `testdb`.`orders` AS `Order` WHERE conditions IN ('filled', '4')

One other thing, I know this is not the proper solution and I do NOT plan on using it, but if I write out the query

$this->Order->query("delete....");

The values will be deleted. Yes I know it's not the proper way. Just pointing out that it appears to be be searching for an id field that doesn't exist.

  • 写回答

1条回答 默认 最新

  • douai2499 2013-05-29 07:55
    关注

    Your tables are not following the CakePHP convention of having the primary key named as id.

    You have two options to rectify this:

    1. Rename the primary key from orderid to id.
    2. In your Order model set the public var $primaryKey = 'orderid';

    I would use option one if this is not a legacy app to stay in convention.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?