Monthly Archives: 七月 2016

修改数据库后 unserialize error at offset 报错

今天直接修改了数据库一个serialize保存的数据,再unserialize报错unserialize(): Error at offset 340 of 376 bytes

发现a:2:{s:2:”en”;s:5:”hello”;s:2:”cn”;s:4:”9999”;s:6:”您好}

而unsrialize()要根据类似 s:6:”您好” 中的6来判断字符长度并进行反序列,但原来的是4,字符长度不符,也就是出现了offset错误

Magento 自定义建立model

1.建立sql脚本
<?php
$installer = $this;
$installer->startSetup();
$installer->run(”
  — DROP TABLE IF EXISTS {$this->getTable(‘customer_loginpoint_record’)};
  CREATE TABLE `{$this->getTable(‘customer_loginpoint_record’)}` (
  `id` INT(10) unsigned NOT NULL auto_increment,
  `customer_id` INT(10) NOT NULL,
  `email` VARCHAR(40) NOT NULL,
  `record_credit_point` INT(10) NOT NULL DEFAULT ‘0’,
  `login_time` timestamp NULL,
   PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
“);
$installer->endSetup();
?>
2.config.xml声明
<models>
    <creditpoint>
        <class>Ei_Creditpoint_Model</class>
        <resourceModel>creditpoint_mysql4</resourceModel>
    </creditpoint>
       <creditpoint_mysql4>
        <class>Ei_Creditpoint_Model_Mysql4</class>
        <entities>
          
            <creditloginpoint>
                <table>customer_loginpoint_record</table>
            </creditloginpoint>
        </entities>
    </creditpoint_mysql4>
</models>
3.建立app/code/community/Ei/Creditpoint/Model/Creditloginpoint.php
<?php
class Ei_Creditpoint_Model_Creditloginpoint extends Mage_Core_Model_Abstract
{
    public function _construct()
    { 
     parent::_construct();
     $this->_init(‘creditpoint/creditloginpoint’);
       
    }
     
public function loadByCustomerId($customerId){
    $collection = $this->getCollection()
        ->addFieldToFilter(‘customer_id’, $customerId);
    return $collection->getFirstItem();
}
}
4.建立app/code/community/Ei/Creditpoint/Model/Mysql4/Creditloginpoint/Collection.php
class Ei_Creditpoint_Model_Mysql4_Creditloginpoint_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {
    public function _construct() {
        parent::_construct();
        $this->_init(‘creditpoint/creditloginpoint’);
    }
}
到这里,你可以
取Mage::getModel(‘creditpoint/creditloginpoint’)了
5.但是要使用load()加载的话,还需要建立app/code/community/Ei/Creditpoint/Model/Mysql4/Creditloginpoint.php
class Ei_Creditpoint_Model_Mysql4_Creditloginpoint extends Mage_Core_Model_Mysql4_Abstract
{
   
        public function _construct(){   
       
        $this->_init(‘creditpoint/creditloginpoint’, ‘id’);
    }
}
Mage::getModel(‘creditpoint/creditloginpoint’)->load(1)->getData()就可以使用了,

Magento custom emails

Magento custom emails

Ever wanted to “just send the email” using the built in email features in Magento? Ever hit the wall trying to do something with Magento? OK, I know the answer to the other one, just had to ask :). Anyhow, sending the emails with Magento turned out to be a process of “just” a few hours of tracing Magento code.

I cant wait for smart comments like, “few hours, huh its so easy…”. Yea, thats the beauty of Magento… few hours of bashing your head against the wall while you are sipping the 4th cup of coffee until the solution hits you. Interesting do, just when you get the hang of it, Magento gets you this “have you tried this” attitude :).

What am I talking about? Scenario: I want to create email template named activecodeline_custom_email1.html, I want to pass few variables to it during runtime, I want to send emails programmaticaly. I dont want to create 56 lines of xml file just to call one template.

Here is how.

... /*
 * Loads the html file named 'custom_email_template1.html' from
 * app/locale/en_US/template/email/activecodeline_custom_email1.html
 */ $emailTemplate = Mage::getModel('core/email_template') ->loadDefault('custom_email_template1');   //Create an array of variables to assign to template $emailTemplateVariables = array(); $emailTemplateVariables['myvar1'] = 'Branko'; $emailTemplateVariables['myvar2'] = 'Ajzele'; $emailTemplateVariables['myvar3'] = 'ActiveCodeline';   /**
 * The best part ?
 * Opens the activecodeline_custom_email1.html, throws in the variable array 
 * and returns the 'parsed' content that you can use as body of email
 */ $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);   /*
 * Or you can send the email directly, 
 * note getProcessedTemplate is called inside send()
 */ $emailTemplate->send('john@someemail.com','John Doe', $emailTemplateVariables); ...

And here we go again, nothing without xml files ? -In order for above piece of code to work, you need to add an entry to your config.xml file like shown below.

... <global> <template> <email> <custom_email_template1 module="SampleModule1"> <label>ActiveCodeline custom email module</label> <file>activecodeline_custom_email1.html</file> <type>html</type> </custom_email_template1> </email> </template> </global> ...

And lets not forget the email template itself, app/locale/en_US/template/email/activecodeline_custom_email1.html.

<!--@subject ActiveCodeline custom email module @-->
 
<div>
<h1>ActiveCodeline custom email example by Branko Ajzele</h1>
<p>Hi there {{var myvar1}} {{var myvar2}} from {{var myvar3}}. This is just some example template to test custom email module.</p>
</div>

Hope this was helpful. Cheers.