cakephp:从其他控制器创建新记录

时间:2014-08-24 11:41:00

标签: cakephp model

我要在模型数据库中创建50个临时/虚拟行。我的问题是如何在我的数据库中创建和保存新记录?
假设我的模型随机包含列(id,randomValue,...)。

1 个答案:

答案 0 :(得分:0)

public function addNew() {
    $this->autoRender = false; // We don't render a view in this example
    $moduleSize = 50;
    $count = $moduleSize;
    while ($count > 0) {
        $random_number = rand(1000000000, 9999999999);
        //check if duplicate
        if ($this->checkIfDuplicateSerial($random_number)) {
            //nothing to be done
        } else {
            $this->Random->create(array('randomValue' => $random_number));
            $this->Random->save();
            $this->Random->clear(); //to init new one
            $count--;
        }
    }
    $this->response->body('Inserted' . $moduleSize . 'Random Numbers');
}