开发者

Extract Drupal encoded data using MySQL from Ubercart table

开发者 https://www.devze.com 2023-02-04 17:07 出处:网络
I am writing an integration piece between Drupal/Ubercart and a in-house admin system. The customer uses Ubercart products with attributes (e.g. a Plaque, which can contain a name, a company, a regis

I am writing an integration piece between Drupal/Ubercart and a in-house admin system.

The customer uses Ubercart products with attributes (e.g. a Plaque, which can contain a name, a company, a registration date and month). When an order is placed, the values entered for the attributes are written to uc_order_products, where the data field contains the actual values entered by the user. As far as I can tell, this is the only place where product attributes values, as entered by an end user to place an order, is stored.

The encoded attribute values uses a format seen all over Drupal tables to encode objects:

a:3:{s:10:"attributes";a:4:{s:26:"Name (to appear on plaque)";a:1:{i:0;s:10:"Some  
Namee";}s:7:"Company";a:1:{i:0;s:28:"Some Company Name Goes here_";}s:19:"Certification 
Month";a:1:{i:0;s:0:"";}s:18:"Certification Y开发者_开发技巧ear";a:1:
{i:0;s:4:"2011";}}s:9:"shippable";s:1:"1";s:6:"module";s:10:"uc_product";}

And expanded, it looks like this:

a:3:
{
    s:10:"attributes";
    a:4:
        {
            s:26:"Name (to appear on plaque)";
            a:1:
                {
                    i:0;
                    s:10:"Some Namee";
                }
            s:7:"Company";
            a:1:
                {
                    i:0;
                    s:28:"Some Company Name Goes Herep";
                }
            s:19:"Certification Month";
            a:1:
                {
                    i:0;
                    s:0:"";
                }
            s:18:"Certification Year";
            a:1:
                {
                    i:0;
                    s:4:"2011";
                }
        }
    s:9:"shippable";
    s:1:"1";
    s:6:"module";
    s:10:"uc_product";
}

I there a simple way to get to the individual field values within this text using SQL? I can write a SQL function to go look for specifics, but I'd like to know if someone knows of an existing MySQL approach (perhaps within Drupal) which would do this.

Thanks!


That's a serialized array, meaning data that was processed by PHP's serialize() function before Drupal inserted it into the database. Use the opposite function unserialize() to turn that string back into an array.


Don't know if any built-in solution exists, but I ended up writing a SQL Function which takes as parameter text such as Name (to appear on plaque). The function then locates the text and extracts the succeeding { ... } block and from it retrieves the corresponding string value. Rough, but works in this case.

If someone has a better solution, I'd like to hear about it!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号