CakePHP 没有插入到模型中

CakePHP not inserting into model

我的模特:

class BuyItPackage extends AppModel {

    var $name = 'BuyItPackage';

    var $belongsTo = array('User', 'Auction');

    function __construct($id = false, $table = null, $ds = null){
        parent::__construct($id, $table, $ds);
    }

}

我的保存操作:

function add($user_id = null, $auction_id = null)
{
    $this->BuyItPackage->create(); // this is line 23

    $data = array (
            'BuyItPackage' => array(
                'user_id' => $user_id,
                'auction_id' => $auction_id,
                'name' => '',
                'price' => 0.00,
                'contract' => '',
                'points' => 0
            )
        );
    $this->BuyItPackage->save($data);
}

当我导航到 add() 操作时,会生成此错误

Undefined property: BuyItPackagesController::$BuyItPackage [APP/controllers/buy_it_packages_controller.php, line 23]

没有插入数据,好像找不到我的模型,有什么想法吗?

在这种情况下,您根本不需要 ->create(); 行。

我猜你的控制器启动方式也有问题,但我们看不到,所以很难判断。

将你的数据设置在一个数组中,并通过 save 方法保存它。

$this->data["BuyItPackage"]['user_id'] = $user_id;
$this->data["BuyItPackage"]['auction_id'] = $auction_id;
$this->data["BuyItPackage"]['name'] = 'dummyvalue1';
$this->data["BuyItPackage"]['price'] = '0.00';
$this->data["BuyItPackage"]['contract'] = 'dummyvalue2';
$this->data["BuyItPackage"]['points'] = '0';

$this->BuyItPackage->save($this->data["BuyItPackage"], false);

您的模型似乎没有连接到您的控制器。也许您重新定义了控制器的 $uses 变量。如果是这样,您需要确保将 'BuyItPackage' 添加到此变量,否则它不再默认加载。

class BuyItPackagesController extends AppController {

    var $uses = array ('BuyItPackage', 'OtherModel');

    function add($user_id = null, $auction_id = null)
    {
        ...
    }
}