Portnyagin Mikhail
Portnyagin Mikhail

Reputation: 337

Magento model->save() doesnt working

Hello my model save() function doesnt work. Magento cache turned off. My code below:

$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

This is my code\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>

What I make wrong? Why new row doesnt appear in db?

UPDATE 1

In my index.php I have

error_reporting(E_ALL ^ E_NOTICE);

//if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
ini_set("display_errors", 1);
//}

UPDATE 2

My code\local\Apptha\Reservation\Model\newinventory.php below

class Apptha_Reservation_Model_Newinventory extends Mage_Core_Model_Abstract{

protected function _construct(){
    $this->_init('reservation/newinventory');
}
}

My code\local\Apptha\Reservation\Model\Mysql4\newinventory.php below

 class Apptha_Reservation_Model_Mysql4_Newinventory extends Mage_Core_Model_Mysql4_Abstract{

protected function _construct(){
    $this->_init('reservation/newinventory', 'id');
}

}

So I dont get any errors from Magento. It looks like Magento find all pathes (model, resource and etc.) But anyway can't save() my data.

UPDATE 3 I inserted code to log queries in Mysql.php to 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);

Then I get in log file:

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
)

I try manually execute this query in phpmyadmin, It`s working fine. But Magento can't make it.

UPDATE 4

okay. While I was debuging this issue, I have written additional testModel action in the controller where tried to test save() function. I saw, It worked there.

Then I returned to originally function and discovered when I make save() outside of if-block then save() works fine. if I make save() inside if-block,then it doesn't work.

I log some string inside if-block and make sure, execution go inside if-block. So actually question why save() doesn't work inside if-block below:

    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;
   } 

but if condition will be

if ( $state === "processing" )

then It works fine. This code are in observer function when order status is changed. When admin make "invoice" for product, producr state is changed from "processing" to "complete". Thefore Magento go into the function two times. First time on "processing" save() working fine but on "complete" It doesn't work.

Upvotes: 3

Views: 15786

Answers (3)

Marie Schreiber
Marie Schreiber

Reputation: 11

you can also persist models with the "sales_order_place_after" (no need to use "sales_order_save_commit_after") Event.

You just can't stop execution because the sql transaction is not finished in "sales_order_place_after" nothing will be written directly but later when the transaction is commited to the database.

hope this helps someone.

Upvotes: 0

Portnyagin Mikhail
Portnyagin Mikhail

Reputation: 337

So problem is in some internal Magento processing. I have spent 1 day to understand it.

But Stop now and solved it by using another observer event sales_order_save_commit_after where I check order state is completed and use save() function. And sure here It works fine.

Upvotes: 2

Alexei Yerofeyev
Alexei Yerofeyev

Reputation: 2103

I'm having some serious doubts that $newInventory contains your model. You are declaring entity hotel_newinventory in config.xml, but trying to load entity newinventory. You can check it this way:

Mage::log(get_class($newInventory));

If I'm right and result is not your model class, try changing the following line

return Mage::getModel('reservation/newinventory');

to

return Mage::getModel('reservation/hotel_newinventory');

Upvotes: 1

Related Questions