user1443519
user1443519

Reputation: 589

CakePHP finding data but not deleting it

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.

Upvotes: 0

Views: 302

Answers (1)

timstermatic
timstermatic

Reputation: 1740

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.

Upvotes: 1

Related Questions