Magento Forum

$model->save(); doesn’t save my data in database ! (no error log)
 
Perrine L
Jr. Member
 
Total Posts:  22
Joined:  2009-05-11
France
 

Hello,

I have the following problem :

I have created a model in my “ProductReferences” module, and I simply want to insert one record by this way :

$model Mage::getModel('productreferences/references');
$model->addData(array(
     
'refcom'=>'REFTESTMODEL''product_id' => 3)
);
$model->save();

Here an extract of my module tree :

ProductReferences
-- + Model
---- + Mysql4
------ + References
-------- + Collection.php
------ + References.php (1)
---- + 
References.php (2)

The file References.php (1) contains :

<?php
class Smile_ProductReferences_Model_Mysql4_References
extends Mage_Core_Model_Mysql4_Abstract
{
    
public function _construct()
    
{
        $this
->_init('productreferences/references''refcom');
    
}
}

The file References.php (2) contains :

<?php
class Smile_ProductReferences_Model_References
extends Mage_Core_Model_Abstract
{
    
public function _construct()
    
{
        parent
::_construct();
        
$this->_init('productreferences/references');
    
}
}

My table, “smile_productreferences_references” :

+------------+------------------+------+-----+---------+-------+
Field      Type             Null Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
refcom     varchar(255)     | NO   PRI NULL    |       |
product_id int(10unsigned NO   MUL NULL    |       |
+------------+------------------+------+-----+---------+-------+

created with :

CREATE TABLE `smile_productreferences_references` (
  `
refcomvarchar(255NOT NULL,
  `
product_idint(10unsigned NOT NULL,
  
PRIMARY KEY (`refcom`)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Store trade references/products relation';

ALTER TABLE `smile_productreferences_references`
  
ADD CONSTRAINT `FK_REFCOM_PRODUCT_IDFOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE;

Then, here an extract of my config.xml file :

<global>
        <
models>
            <
productreferences>
                <class>
Smile_ProductReferences_Model</class>
                <
resourceModel>productreferences_mysql4</resourceModel>
            </
productreferences>
            <
productreferences_mysql4>
                <class>
Smile_ProductReferences_Model_Mysql4</class>
                <
entities>
                    <
references>
                        <
table>productreferences_references</table>
                    </
references>
                </
entities>
            </
productreferences_mysql4>
        </
models>
        <
resources>
            <
productreferences_setup>
                <
setup>
                    <
module>Smile_ProductReferences</module>
                </
setup>
                <
connection>
                    <use>
core_setup</use>
                </
connection>
            </
productreferences_setup>
            <
productreferences_write>
                <
connection>
                    <use>
core_write</use>
                </
connection>
            </
productreferences_write>
            <
productreferences_read>
                <
connection>
                    <use>
core_read</use>
                </
connection>
            </
productreferences_read>
        </
resources>
    </global>

My problem : the insert of data with code given at the top of this post doesn’t work ! But no error neither in navigator, neither in logs…

Any idea ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Damian Culotta
Enthusiast
 
Total Posts:  878
Joined:  2008-12-10
Argentina
 

Did you try waht happens if you use

$model->setData([your-data]);
 
Magento Community Magento Community
Magento Community
Magento Community
 
Perrine L
Jr. Member
 
Total Posts:  22
Joined:  2009-05-11
France
 

Yes, I tried to use method “setData” instead of “addData”, but nothing’s saving in my database :(.

 
Magento Community Magento Community
Magento Community
Magento Community
 
darryla
Member
 
Total Posts:  52
Joined:  2008-07-08
 

This was of great help to me but I am still not even able to access the database, I am getting an error:

Fatal error: Call to a member function getModelInstance() on a non-object

Whenever I try and initiate a getSingleton database connection, with both core/resource and modulename/resource.

How are you initialising the database connection?

Many thanks smile

EDIT: Problem solved, calling requests from jQuery and had only included Mage.php, but not initliased Magento with Mage::app() wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
Damian Culotta
Enthusiast
 
Total Posts:  878
Joined:  2008-12-10
Argentina
 

@Perrine L.

You said that your table is called: smile_productreferences_references.
But in your module definition you set another table for the entity references of your model.

<global>
        <
models>
            <
productreferences>
                <class>
Smile_ProductReferences_Model</class>
                <
resourceModel>productreferences_mysql4</resourceModel>
            </
productreferences>
            <
productreferences_mysql4>
                <class>
Smile_ProductReferences_Model_Mysql4</class>
                <
entities>
                    <
references>
                        
//Your line
                        
<table>productreferences_references</table>
                        
//This is the table name that you mention
                        
<table>smile_productreferences_references</table>
                    </
references>
                </
entities>
            </
productreferences_mysql4>
        </
models>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Perrine L
Jr. Member
 
Total Posts:  22
Joined:  2009-05-11
France
 

Hello everybody,

Thank you for your answer. That’s right I made a mistake concerning the table name on this post, but I had corrected it in my code but the problem still existed.

I don’t know why this code :

$model Mage::getModel('productreferences/references');
$model->addData(array(
     
'refcom'=>'REFTESTMODEL''product_id' => 3)
);
$model->save();

didn’t (doesn’t ?) work, but I took another way to do my module : I inspired by the “customer option” tab (by copy/paste and by changing the code step by step).

So the post is solved and unsolved too :s

 
Magento Community Magento Community
Magento Community
Magento Community
 
Perrine L
Jr. Member
 
Total Posts:  22
Joined:  2009-05-11
France
 

Finally, to do what I had to, I copy what it made for ”customer options”, concerning model, blocks and templates. By doing step by step, copying file and modifying little by little, I success.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top