I am trying to update a custom field of customer_entity in magento I am using the following code:
$customer = Mage::getModel('customer/customer')->load($customerId);
print_r($customer->getData());
try {
$customer->setData('field', $flag);
$insertId = $customer->save()->getId();
echo "Data successfully inserted. Insert ID: ".$insertId;
print_r($customer->getData());
} catch (Exception $e){
echo $e->getMessage();
}
Now I does not get why it is not saving in a database, when i write the log customer data has the updated value, but when i try to see in the database it is not reflecting.
Any Idea?
Edited:
$write = Mage::getSingleton("core/resource")->getConnection("core_write");
$sql = "update `customer_entity` set `field` = ".$flag." where`entity_id` =".$customerId;
$write->query($sql);
This code is working with charm but the above code nention at the top does work.
Does any body have any idea?
Try clearing your cache once and reindexing from magento admin! Hope it works
first check the following
1. check you made the database table defined in the config.xml in the resource model.
2.get the value for $flag and check whether the field is present or not.
Step1:- Check your resource table name in config.xml file,and Clear your cache folder inside /var folder and than check.
Step2:- If step 1 is not working than reindex your project and than check.
Step3:- If step 1 and 2 is not working, than check your table Engine type if it's InnoDB than change it to MyISAM and than check.
For more Info click here
Related
I'm trying to update a record just after I get it from database.
$item = Model::find($id);
$item->field = 'foo';
$item->save();
it do finds the requested record, but the query generated to update the record is not correct:
update `Models` set `field` = ? where `id` is null
I don't know why this is happening!
what is wrong ?
Update:
I just renamed the primary-Key from ID to id in the database table. and then it worked! I didn't know it's case-sensitive
update `bannerads_orders` set `ViewedCount` = ? where `id` = ?
Well, I found the problem. as Laravel documents says:
Note: Eloquent will also assume that each table has a primary key
column named id. You may define a primaryKey property to override this
convention
but in my table it was ID not id. so after I changed it to id, everything works as expected.
Laravel uses prepared statements. There is nothing wrong with this. Additionally you don't actually specify an error.
You should probably use in this case:
$item = Model::findOrfail($id);
$item->field = 'foo';
$item->save();
It seems that $id might be null or user cannot be found and than saving it doesn't make any sense.
If user is not found exception will be thrown and this save won't be executed
I'm writing an installation script in php, this script has to ask the user some data and then store them in a XML file and on a database.
Here is the procedure:
1) Insert data;
2) Save configuration data (such as database name,user,password,host,port) in a XML file
3) Using those data to install a database (I have an empty dump I use to create it)
4) Insert in the "admin" table the first user data (taken from the form in point 1)
Now, everything works fine until point 4.
When I try to access the database MySql always returns my an error #1146 saying that the table doesn't not exists.
This is my custom JSON log:
{
"Status":"KO",
"Reason":"SELECT * FROM bbscp_admin_user; || Table 'adb.bbscp_admin_user' doesn't exist",
"Errno":1146
}
Obviously I checked both with phpMyAdmin and mysql (from cli) and I can say that both DB and Table DO exists.
Here is the part where I create the DB: [this works fine]
$Install = Install::getInstance();
$sqlscript = str_replace("__DBNAME__",
$config_info['dbname'],
file_get_contents('../config/bbscp-base-db.sql')
);
$connection = mysqli_connect($config_info['dbhost'],
$config_info['dbuser'],
$config_info['dbpwd']
) or die("Unable to connect! Error: ".mysql_error());
if(mysqli_query($connection,'CREATE DATABASE '.$config_info['dbname'].';')){
echo 'Database successfully createad';
mysqli_close($connection);
}else{
die('ERROR CREATING DATABASE!');
}
Here the part where I populate the database from the dump (it only add the tables)
$connection = mysqli_connect($config_info['dbhost'],
$config_info['dbuser'],
$config_info['dbpwd'],
$config_info['dbname']
) or die("Unable to connect! Error: ".mysql_error());
if(mysqli_multi_query($connection, $sqlscript)){
echo 'Database successfully imported';
mysqli_close($connection);
}else{
die('ERROR IMPORTING DATABASE');
}
Now the decisive part, I open a connection with the database (using a library that I've developed in the past years and has always worked fine)
$DB = new Database(Install::getInstance());
$Q = new Query();
$DB->connect();
$query = $Q->addNewUser($config_info['nickname'],
$config_info['password'],
$config_info['firstname'],
$config_info['lastname'],
$config_info['email'],
0); // this just returns the query string I need
// the query in this case is:
// INSERT INTO bbscp_admin_user
// (nickname,password,firstname,lastname,mail,confirmed)
// VALUES (the ones above);"
$res=$DB->startQuery($query); // THIS IS WHERE THE ERROR IS RETURNED
// this function only wraps the mysqli_query and returns a custom JSON
I've tried a lot of solution.. The tables DO exists but every query I try to invoke at this point doesn't work because apparently "table doesn't exist" (even using mysqli function instead of mines).
Thanks in advance and sorry for the long question! :-)
EDIT 1
During this days I've changed a lot of code, I tried using different implementation using functions, objects, redirects but none of them seems to work.
The thing is always the same:
It creates the database
Returns the error code when trying to add the user
If I refresh the page it correctly adds the user
i am using pyrocms for my web application.
i want create library for my module in addons.
when i use this code for tabel llist in my database.
$CI = & get_instance();
$all=$CI->db->list_tables();
i have "defualt_products" value in $all array. this means i have "default_products" table in my database. but when i use next code , result is false. why?
if(!$CI->db->table_exists("default_products"))
return false;
i use pyrocms 2.2.
You can use the dbprefix method to include you table prefix from database.php config file:
if ( !$CI->db->table_exists($CI->db->dbprefix('products')) ){
//there is no such table, products
echo "there is no table named ".$CI->db->dbprefix('products');
die();
}else{
//table found
echo "table found"; die();
}
In case it dose not work, then I think your problem is not this piece of code!
You aren't passing anything to table_exists. How is it supposed to know which table you are trying to check for? It takes one parameter...the table name you are checking for.
http://ellislab.com/codeigniter/user-guide/database/table_data.html
so, if you did this, and a table called "tablename" existed..then you would still get false, because table_exists returns TRUE if the table does exist.
if ($CI->db->table_exists('tablename')
{
return FALSE;
}
Followed Alan Storm's tutorial, and I got some problem while calling createEntityTables() method. My script is like:
$installer->createEntityTables(
$this->getTable('complexworld/eavblogpost')
);
I've solved the problem related to BLOB/TEXT Mysql error, but another problem occurred. The tables I created have double prefix ("mgt_" is the prefix),
mgt_mgt_eavblog_posts
mgt_mgt_eavblog_posts_char
mgt_mgt_eavblog_posts_datetime
mgt_mgt_eavblog_posts_decimal
mgt_mgt_eavblog_posts_int
mgt_mgt_eavblog_posts_text
mgt_mgt_eavblog_posts_varchar
Tried to dig in createEntityTables() method, and while I print $this->getTable($baseTableName),
if (!$isNoCreateMainTable) {
/**
* Create table main eav table
*/
echo $this->getTable($baseTableName);
exit;
$connection = $this->getConnection();
I got "mgt_mgt_eavblog_posts" on the screen, which means the core method may have added an extra prefix to the table name. Any idea what's going wrong here? I appreciate all your kind helps!
$table = strtolower(substr(ltrim($this->getTable('complexworld/eavblogpost')), strlen(Mage::getConfig()->getTablePrefix())));
$installer->createEntityTables($table);
I have added status field in sale_flat_quote_item table and trying to save updated status by changing in my custom module's Controller.php(EntitycodesaarnaController.php) file but it not saving updated status.
You can load the sales_flat_quote_item objetc by it's id
$quote_item = Mage::getModel('sales/quote_item')->load($quoteitem_id);
$quote_item->setStatus('status');
$quote_item->save();
Hope this helps!