Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Not Working Along with Observer #171

Closed
robinhoo1973 opened this issue Sep 22, 2018 · 6 comments
Closed

Cache Not Working Along with Observer #171

robinhoo1973 opened this issue Sep 22, 2018 · 6 comments

Comments

@robinhoo1973
Copy link

robinhoo1973 commented Sep 22, 2018

Describe the bug
As the Model Article content field is stored the rich text with base64 encoded string, and an observer was created for encoding & decoding while saving and reading. It works fine until the trait mode Cachable involved in the Article model. And when it removed from the model, everything returned ok.

Eloquent Query
Please provide the complete eloquent query that caused the bug, for example:
Observer
/app/Observers/ArticleObserver.php

<?php

namespace App\Observers;
use App\Article;

class ArticleObserver
{
	public function saving(Article $article)
	{
		$article->content = bin2hex($article->content);
	}

	public function retrieved(Article $article)
	{
		// if(strlen($article->content)%2==1 || preg_replace('/[a-zA-Z0-9]+/','',$article->content)!='')
		// return;
		try{
			$article->content = hex2bin(trim($article->content));
		}
		catch (\Exception $e) {
			return;
		} 
	}
}

/app/Providers/ArticleModelServiceProvider.php

<?php

namespace App\Providers;
use Illuminate\Support\ServiceProvider;

class ArticleModelServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        \App\Article::observe(\App\Observers\ArticleObserver::class);
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Stack Trace
The full stack trace from your log file.

Environment

  • PHP: [e.g. 7.1.0]
  • OS: [e.g. Ubuntu 18.04]
  • Laravel: [e.g. 5.6.15]
  • Model Caching: [e.g. 0.2.61]

Additional context
Add any other context about the problem here.

@mikebronner
Copy link
Owner

Hi @robinhoo1973, I'm out of the country at the moment, I will take a look. In the mean while, can you paste the code for your observer here as well? Thanks!

@robinhoo1973
Copy link
Author

Hi @robinhoo1973, I'm out of the country at the moment, I will take a look. In the mean while, can you paste the code for your observer here as well? Thanks!

Yes, I just updated my issue report with the observer code included. Thanks for your prompt response.

@mikebronner
Copy link
Owner

Thanks! I will have to create a test for observers. Give me a few weeks. If you would like to take a stab at submitting a PR with a new unit test or even an update to the functionality, that would be great! :)

@robinhoo1973
Copy link
Author

Thanks! I will have to create a test for observers. Give me a few weeks. If you would like to take a stab at submitting a PR with a new unit test or even an update to the functionality, that would be great! :)

Take your time and enjoy your trip, it's not urgent case. :)

@mikebronner
Copy link
Owner

@robinhoo1973 I created a test for observers and do not see an issue with using observers. It works as expected. Please provide the complete query that you are running.

@mikebronner
Copy link
Owner

I'm closing this for now due to inactivity. Please feel free to reopen with additional information if you still have this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants