corcel icon indicating copy to clipboard operation
corcel copied to clipboard

unserialize() function error

Open duyngha opened this issue 10 years ago • 11 comments

I got this error message when i tried to get option value in options table like this Options::get('option_name');

<b>Notice</b>:  unserialize(): Error at offset 0 of 19 bytes in <b>/Applications/MAMP/htdocs/thebest
/wp-content/plugins/wp-nice-search/vendor/jgrossi/corcel/src/Options.php</b> on line <b>55</b>

How can i resolve it? any help appreciates.

Thanks

duyngha avatar Jun 24 '16 02:06 duyngha

Hi @duyngha thanks for using Corcel :-)

About your request can you paste the code you're trying to run?

jgrossi avatar Jun 24 '16 12:06 jgrossi

Hi @jgrossi,

Here are my code. I create a class which has a method to get option value.

class SearchController extends Controller
{
    public function getOptions()
    {
        return Options::get('wpns_options');
    }
}

And then I create another class which using Options model to get option value.

class Entry extends Post
{
    public function getPermalink()
    {
        $permalink = '';

        $url = Options::get('home') . '?p=' . $this->ID;

        $search = '/\?p=.*/';

        $structure = Options::get('permalink_structure');

        if ($structure == '/%postname%/') {

            $permalink = preg_replace($search, $this->getSlugAttribute(), $url);

        }

        return $permalink;
    }
}

And I try to using the method getPermalink() and got the error above.

I figure out I use the static method get() many times and it only returns the first value of option. In this case is value of 'wpns_options'.

I resolved this issue instead using:

$options = new Options;

$options->where('option_name', 'home')->first()->option_value;

But I just want to use short way to get option value. So I create a class that extends Options model names Settings

<?php
class Settings extends Options
{
    public function getSettings($key)
    {
        return unserialize($this->where('option_name', $key)->first()->option_value);
    }   
}

Then I use this object to get option value by using getSettings() method. But I got the same error.

whether I do something wrong?

Thanks.

duyngha avatar Jun 25 '16 03:06 duyngha

I don't think you need to unserialize that value, it should already be unserialized. If it's not, then the option_value in the database is not properly serialized.

rundef avatar Jun 25 '16 14:06 rundef

@rundef But when I create instance by new keyword it works fine. So I don't think option_value is not properly serialized is problem.

duyngha avatar Jun 27 '16 03:06 duyngha

@duyngha are you using Corcel inside a WordPress plugin?

jgrossi avatar Jun 27 '16 12:06 jgrossi

@jgrossi Yes, I'm using it for my plugin.

duyngha avatar Jun 28 '16 07:06 duyngha

Ok, so I created a Settings class, like you did :

namespace App;

use Corcel\Options as Options;

class Settings extends Options
{
    public function getSettings($key)
    {
        return unserialize($this->where('option_name', $key)->first()->option_value);
    }   
}

And I put the following code in my controller :

$test = new Settings;
$test->option_name = 'testing_option_value';
$test->option_value = serialize(array('hello' => 'world'));
$test->save();

$setting = new Settings;
var_dump($setting->getSettings('testing_option_value'));

// by the way, you could just use the following method :
var_dump(Options::get('testing_option_value'));

And I'm seeing the unserialized value, as expected. How did you create the wp_options entry ? Through wordpress ? Can you lookup in the DB what is the option_value of the option you're trying to get ?

rundef avatar Jun 28 '16 12:06 rundef

Thank you for your response @rundef. Yes, I created the wpns_options through WP in old versions of plugin. It has already existed in database. I just want to get its value.

duyngha avatar Jun 29 '16 01:06 duyngha

Use Options::get($option_name); rather than your Settings class, it will fix the issue.

rundef avatar Jun 29 '16 11:06 rundef

@duyngha Did you solve your problem? Can you give us an update about that? Thanks

jgrossi avatar Aug 22 '16 13:08 jgrossi

@jgrossi I am so sorry. I'm busy recently so I forgot to respond the update. I resolved my problem. Just using new keyword to create an instance of Options model and then invoke the where method to get option value.

$setting = new Options;
$structure = $setting->where('option_name', 'permalink_structure')->first()->option_value;

Thank you very much for your support. Great work!

duyngha avatar Aug 23 '16 02:08 duyngha