Magento车型->;save()不起作用
您好,my model save()函数不起作用。Magento缓存已关闭。我的代码如下:Magento车型->;save()不起作用,magento,Magento,您好,my model save()函数不起作用。Magento缓存已关闭。我的代码如下: $newInventory = Mage::getModel('reservation/newinventory'); Mage::log( get_class( $newInventory ) ); //print Apptha_Reservation_Model_Newinventory $newInventory->setEntityId( $productId ); $newInvent
$newInventory = Mage::getModel('reservation/newinventory');
Mage::log( get_class( $newInventory ) ); //print Apptha_Reservation_Model_Newinventory
$newInventory->setEntityId( $productId );
$newInventory->setRoomTypeId( $roomTypeId );
$newInventory->setRoomsCount( $roomsCount );
$newInventory->setDateFrom( $checkInStamp );
$newInventory->setDateTo( $checkOutStamp );
$newInventory->setOrderId( $orderId );
$query = $newInventory->save();
Mage::log( "save query below: " ); //I get this string
Mage::log( $query->toString() ); //return 2012-10-15T06:00:58+00:00 DEBUG (7): 17, 7, 8,
//1349913600, 1349913600, 300000040, 12
这是我的代码\local\Apptha\Reservation\etc\config.xml
<models>
<reservation>
<class>Apptha_Reservation_Model</class>
<resourceModel>reservation_mysql4</resourceModel>
</reservation>
<reservation_mysql4>
<class>Apptha_Reservation_Model_Mysql4</class>
<entities>
<newinventory>
<table>apptha_booking_hotel_newinventory</table>
</newinventory>
</entities>
</reservation_mysql4>
</models>
<resources>
<reservation_setup>
<setup>
<module>Apptha_Reservation</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</reservation_setup>
<reservation_write>
<connection>
<use>core_write</use>
</connection>
</reservation_write>
<reservation_read>
<connection>
<use>core_read</use>
</connection>
</reservation_read>
</resources>
更新2
下面是我的代码\local\Apptha\Reservation\Model\newinventory.php
class Apptha_Reservation_Model_Newinventory extends Mage_Core_Model_Abstract{
protected function _construct(){
$this->_init('reservation/newinventory');
}
}
class Apptha_Reservation_Model_Mysql4_Newinventory extends Mage_Core_Model_Mysql4_Abstract{
protected function _construct(){
$this->_init('reservation/newinventory', 'id');
}
}
下面是我的代码\local\Apptha\Reservation\Model\Mysql4\newinventory.php
class Apptha_Reservation_Model_Newinventory extends Mage_Core_Model_Abstract{
protected function _construct(){
$this->_init('reservation/newinventory');
}
}
class Apptha_Reservation_Model_Mysql4_Newinventory extends Mage_Core_Model_Mysql4_Abstract{
protected function _construct(){
$this->_init('reservation/newinventory', 'id');
}
}
所以我没有从Magento那里得到任何错误。看起来Magento可以找到所有路径(模型、资源等),但无论如何都无法保存()我的数据
更新3
我将Mysql.php中记录查询的代码插入到公共函数查询($sql,$bind=array())
然后我进入日志文件:
2012-10-15T09:54:31+00:00 DEBUG (7): [2012-10-15 09:54:31] SQL: INSERT INTO
`apptha_booking_hotel_newinventory` (`entity_id`, `room_type_id`, `rooms_count`, `date_from`,
`date_to`, `order_id`) VALUES (?, ?, ?, ?, ?, ?)
BIND: Array
(
[0] => 17
[1] => 7
[2] => 8
[3] => 1349913600
[4] => 1349913600
[5] => 300000040
)
我试着在phpmyadmin中手动执行这个查询,它工作得很好。但是Magento做不到
更新4
好的。在调试这个问题时,我在尝试测试save()函数的控制器中编写了额外的testModel操作。我看到了,它在那里起作用了
然后我返回到最初的函数,并发现当我将save()置于if块之外时,save()工作正常。如果我在if块中设置save(),则它不起作用
我在if块中记录了一些字符串,并确保执行进入if块。所以,实际上要问为什么save()在下面的if块中不起作用:
if ( $state == "complete" ){
Mage::log('here');
$newInventory = Mage::getModel('reservation/newinventory');
$newInventory->setEntityId(12);
$newInventory->setRoomTypeId(7);
$newInventory->setRoomsCount(8);
$newInventory->setDateFrom(1349913600);
$newInventory->setDateTo(1349913600);
$newInventory->setOrderId(300000040);
$query = $newInventory->save();
die;
}
但如果情况会好转
if ( $state === "processing" )
那么它工作得很好。当订单状态改变时,此代码处于观察者功能中。当管理员为产品开具“发票”时,产品状态从“处理”更改为“完成”。在此之前,Magento将进入函数两次。第一次“处理”save()工作正常,但“完成”不起作用。我严重怀疑$newInventory是否包含您的模型。您正在config.xml中声明实体hotel\u newinventory,但试图加载实体newinventory。您可以通过以下方式进行检查:
Mage::log(get_class($newInventory));
如果我是对的,结果不是您的模型类,请尝试更改以下行
return Mage::getModel('reservation/newinventory');
到
所以问题在于一些内部的Magento处理。我花了一天的时间来理解它
但现在停止,在检查订单状态完成后,使用另一个观察者事件sales\u order\u save\u commit\u并使用save()函数解决它。当然,在这里它可以正常工作。您还可以使用“销售\订单\放置\之后”(无需使用“销售\订单\保存\提交\之后”)事件来持久化模型 您不能停止执行,因为sql事务在“sales\u order\u place\u after”中没有完成,不会直接写入任何内容,但会在事务提交到数据库之后写入
希望这对某人有所帮助。你在告诉调试步骤时弄错了。艾伦,当我创建模型时,我会遵循你博客中的步骤。刚才这是我自己的模型来导入扩展。我写了上面的代码,但不明白为什么它不起作用。人们需要比“它不起作用”更多的信息来帮助你。理解你。我已打开“显示错误”和“错误报告”。但我真的没有从Magento那里得到任何错误。在聚会上有点晚,但是如果你的表的主键不是自动递增的,你必须添加
$this->\u isPkAutoIncrement=false代码>在模型资源的构造中。否则save()将无法静默工作。来源:好的,我已经将config.xml从hotel_newinventory更改为newinventory。此外,我还添加了我的答案模型和资源文件。我制作了Mage::log(get_class($newInventory))。它正确地返回类名。但还是无法挽救。
return Mage::getModel('reservation/hotel_newinventory');