CakePHP没有插入模型

时间:2015-03-28 21:42:37

标签: cakephp cakephp-1.3

我的模特:

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]

没有插入数据,看起来它找不到我的模型,任何想法?

3 个答案:

答案 0 :(得分:0)

在这种情况下,根本没有理由需要->create();行。

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

答案 1 :(得分:0)

将数据设置为数组并通过save Method保存。

$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);

答案 2 :(得分:0)

看起来您的模型未附加到您的控制器上。也许您重新定义了控制器的$uses变量。如果是这样,您需要确保将“BuyItPackage”添加到此变量,否则默认情况下不再加载它。

class BuyItPackagesController extends AppController {

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

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