cakephp保存数据给了我一个错误

时间:2011-11-07 04:25:46

标签: cakephp cakephp-2.0

我正在尝试插入以下数组

foreach($list as $l)
    {

        $data = array(

        'source_number'=> $l['Csv']['Source'],
        'destination_number'=> $l['Csv']['Destination'],
        'seconds'=> $l['Csv']['Seconds'],
        'callerID'=> $l['Csv']['CallerID'],
        'disposition'=> $l['Csv']['Disposition'],
        'cost'=> $l['Csv']['Cost'],
        'billing_cost'=> $l['Csv']['newCost']

        );
        //$total[] = $l['Csv']['newCost'];
        debug($data);
        $this->CallcenterBilling->save($data);
        unset($data);

    }

但它给我这个错误

( ! ) Fatal error: Call to a member function save() on a non-object in C:\wamp\www\wadic\app\Controller\CallcenterBillingsController.php on line 61
Call Stack
#   Time    Memory  Function    Location
1   0.0005  708160  {main}( )   ..\index.php:0
2   0.0169  2775744 Dispatcher->dispatch( ) ..\index.php:96
3   0.0279  3946176 Dispatcher->_invoke( )  ..\Dispatcher.php:89
4   0.0293  4016984 Controller->invokeAction( ) ..\Dispatcher.php:107
5   0.0293  4017992 ReflectionMethod->invokeArgs( ) ..\Controller.php:473
6   0.0293  4018024 CallcenterBillingsController->import( ) ..\CallcenterBillingsController.php:0
我错过了什么? 感谢

2 个答案:

答案 0 :(得分:1)

首先,您可以确保在CallcenterBillingsController中使用CallcenterBilling模型: public $ uses = array('CallcenterBilling');

辅助您可以通过保存不循环来优化它: 请参阅模型的 saveAll 方法

所以你会得到类似的东西:

class CallcenterBillingController extends AppController {

  public $uses = array('CallcenterBilling');

  public function someaction() {

    $data = array();
    foreach ($list as $l) {

      $data[] = array(
          'source_number' => $l['Csv']['Source'],
          'destination_number' => $l['Csv']['Destination'],
          'seconds' => $l['Csv']['Seconds'],
          'callerID' => $l['Csv']['CallerID'],
          'disposition' => $l['Csv']['Disposition'],
          'cost' => $l['Csv']['Cost'],
          'billing_cost' => $l['Csv']['newCost']
      );
      //$total[] = $l['Csv']['newCost'];

    }
    debug($data);
    $this->CallcenterBilling->saveAll($data);
  }

}

PS。如果这没有帮助,请显示控制器代码。

答案 1 :(得分:-1)

你应该实现这个:

$this->CallcenterBilling->read(null, 1);
$this->CallcenterBilling->set(array(
   'source_number'=> $l['Csv']['Source'],
    'destination_number'=> $l['Csv']['Destination'],
    'seconds'=> $l['Csv']['Seconds'],
    'callerID'=> $l['Csv']['CallerID'],
    'disposition'=> $l['Csv']['Disposition'],
    'cost'=> $l['Csv']['Cost'],
    'billing_cost'=> $l['Csv']['newCost']
));
$this->CallcenterBilling->save();

这可能对你有用。