こんにちは、私のモデルの save() 関数が機能しません。Magento キャッシュをオフにしました。以下の私のコード:
$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>
私が間違っていることは何ですか?新しい行がデータベースに表示されないのはなぜですか?
更新 1
私のindex.phpには
error_reporting(E_ALL ^ E_NOTICE);
//if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
ini_set("display_errors", 1);
//}
更新 2
以下の私のコード\local\Apptha\Reservation\Model\newinventory.php
class Apptha_Reservation_Model_Newinventory extends Mage_Core_Model_Abstract{
protected function _construct(){
$this->_init('reservation/newinventory');
}
}
以下の私のコード\local\Apptha\Reservation\Model\Mysql4\newinventory.php
class Apptha_Reservation_Model_Mysql4_Newinventory extends Mage_Core_Model_Mysql4_Abstract{
protected function _construct(){
$this->_init('reservation/newinventory', 'id');
}
}
したがって、Magento からエラーは発生しません。Magento がすべてのパス (モデル、リソースなど) を見つけたように見えますが、とにかく私のデータを save() できません。
更新 3 Mysql.php のクエリをログに記録するコードを public function query($sql, $bind = array()) に挿入しました
$code = 'SQL: ' . $sql . "\r\n";
if ($bind) {
$code .= 'BIND: ' . print_r($bind, true) . "\r\n";
}
Mage::Log("[".date('Y-m-d H:i:s')."] ".$code);
次に、ログファイルに入ります:
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 でこのクエリを手動で実行してみましたが、問題なく動作しています。しかし、マジェントはそれを作ることができません。
更新 4
わかった。この問題のデバッグ中に、save() 関数をテストしようとしたコントローラーに追加の testModel アクションを記述しました。私は見た、それはそこで働いた。
その後、元の機能に戻り、if-block の外で save() を作成すると、save() が正常に機能することを発見しました。if-block 内で save() を作成すると、機能しません。
if-block 内の文字列をログに記録し、実行が if-block 内にあることを確認します。したがって、実際にはなぜ 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 は関数に 2 回入ります。初めて「処理中」の save() は正常に動作しますが、「完了」では動作しません。