如果我不使用 cake,UPDATE 查询将非常简单。
使用 CakePHP 更简单... CakePHP 不是问题。你的问题是(真的)“小菜一碟”。您想减少可用预订的整数值,但您试图在表字段中存储一个字符串:
$this->Booking->saveField('booking_allotment', 'booking_allotment - 1' );
我猜“0”值是由于不可存储的类型问题而自动存储的。您应该使用 Model $validate 变量真正验证您的字段。
其次,总是要避免 loadModel 调用:90% 的时间它用于解决由错误的问题分析引起的问题。例如,这是我根据您的具体情况猜测的...
你应该有:
1. a "Package" model for all the available offers (Package hasMany Booking / AvailableBooking)
2. a "Booking" model for all the user's bookings (Booking belongs to Package / hasMany Message)
3. a "AvailableBooking" model for the available Booking number for the current month/week/anything-else (dependent on your customer's needs)
4. a "Message" model for your Booking communications
使用以下语句定义您应该从其他控制器访问的模型之间的关系(例如):
$this->Package->Booking->find();
$this->Package->Booking->Message->find();
$this->Message->Booking->find();
我真的建议您阅读 CookBook 时更加注意(http://book.cakephp.org/2.0/en/getting-started.html)。
让我们完成这篇文章来解决您的问题...
1. Set the Booking id as you did...
2. Read the field that contains the value you're searching for... and decrement it
3. Save the decremented field value
$this->Booking->id = $id;
$this->Booking->saveField('your_field', $this->Booking->field('your_field') - 1);
快乐编码!