I'm using APC cache for expensive query which retrieve job with related files, payments, events etc.
I would like to know if there are available solutions for cache invalidation in Doctrine 1.*.
I came up with following working solution, it does work, I just don't want to invent a wheel.
Please suggest me if there are better/other existing solutions.
Record listener tries to clear cache with given id's on postSave event:
class My_Doctrine_Record_Listener_ClearCache extends Doctrine_Record_Listener
{
/**
* Clear cache by table tags
*
* #param Doctrine_Event $event
* #return null
*/
public function postSave(Doctrine_Event $event)
{
$cache = new Doctrine_Cache_Apc();
/* #var $model Doctrine_Record */
$model = $event->getInvoker();
$name = get_class($model);
/* #var $table Doctrine_Table */
$table = $model->getTable($name);
if (method_exists($table, 'getCacheTags')) {
foreach ($table->getCacheTags() as $tag) {
$id = preg_replace('/%([\w]+)%/e', '$model->{\\1}', $tag);
$cache->delete($id);
}
}
}
}
This is what I have in tables:
class FileTable extends Doctrine_Table
{
/* ... */
public function getCacheTags()
{
return array(
'job_view_%job_id%'
);
}
/* ... */
}
class JobTable extends Doctrine_Table
{
/* ... */
public function getCacheTags()
{
return array(
'job_view_%id%'
);
}
/* ... */
}
The above solution has been used for 7 years in the production environment, so it's safe to say - it does work good enough.
Related
As stated in the doc, laravel will not fire an event on mass update/insert/delete.
https://laravel.com/docs/5.8/eloquent#events
It uses the Builder for this and will not fire an event.
Is there a way that I can still fire an event after a mass update for example? I would only need the query Builder to extract the needed info myself ( log purposes).
It is actually possible , but you have to extend the Eloquent builder ,overwrite the update/insert methods and send the event there.
Just been playing around with it... Needs work, but the basic idea is the following :
class Test extends Model
{
protected $guarded = [];
public $dispatchesEvents = [
'saved' => SavedTest::class
];
/**
* INCLUDE this as a trait in your model.
* Overwrite the eloquentBuilder.
*
* #param \Illuminate\Database\Query\Builder $query
* #return \Illuminate\Database\Eloquent\Builder|static
*/
public function newEloquentBuilder($query)
{
return new TestBuilder($query);
}
}
Extend the eloquent builder...
class TestBuilder extends Builder
{
/**
* Update a record in the database and fire event.
*
* #param array $values
* #return int
*/
public function update(array $values)
{
// normal eloquent behavior.
$result =$this->toBase()->update($this->addUpdatedAtColumn($values));
/*
* Fire event.
*/
if($result){
if( $event = Arr::get($this->model->dispatchesEvents,'saved')){
// at the attributes.
$this->model->fill($this->addUpdatedAtColumn($values));
$queryBuilder =$this->toBase();
event(new $event($this->model,$queryBuilder));
}
}
}
public function insert(array $values)
{
// same idea..
}
}
The event class :
class SavedTest
{
use SerializesModels;
public $model;
public $query;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($model,$query =null)
{
$this->model = $model;
$this->query = $query;
}
}
The listener.
class SavedTestEvent
{
/**
* Create the event listener.
*
*
*/
public function __construct()
{
}
/**
* Handle the event.
*
* #param object $event
* #return void
*/
public function handle($event)
{
// The model , with the attributes.
dump($event->model);
// the query builder , you could extract the wheres or whatever to build your own log for it.
dump($event->query);
}
}
#Paolo on batch request it would not be file the event you must have to perform operation on single record.. like
Analytic::where('id', '>', 100)->get()->each(function($analytic) {
$analytic->delete();
});
Does anybody know how to add extra data on a collection?
The doc says much about how to add extra data on an item which translates into decorating the ItemNormalizer service, and it works pretty well.
But I’m struggling in finding out which normalizer to decorate when it comes to add some data on a collection of entities. The extra data could be anything: the current user logged in, a detailed pager, some debug parameters, ... that are not related to a specific entity, but rather on the request itself.
The only working solution for now is to hook on a Kernel event but that's definitely not the code I like to write:
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
final class SerializeListener implements EventSubscriberInterface
{
/**
* #var Security
*/
private $security;
/**
* #var NormalizerInterface
*/
private $normalizer;
public function __construct(
Security $security,
NormalizerInterface $normalizer
) {
$this->security = $security;
$this->normalizer = $normalizer;
}
public function addCurrentUser(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
if ($request->attributes->has('_api_respond')) {
$serialized = $event->getControllerResult();
$data = json_decode($serialized, true);
$data['hydra:user'] = $this->normalizer->normalize(
$this->security->getUser(),
$request->attributes->get('_format'),
$request->attributes->get('_api_normalization_context')
);
$event->setControllerResult(json_encode($data));
}
}
/**
* #inheritDoc
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
'addCurrentUser',
EventPriorities::POST_SERIALIZE,
],
];
}
}
Any ideas?
Thank you,
Ben
Alright, I finally managed to do this.
namespace App\Api;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
final class ApiCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{
/**
* #var NormalizerInterface|NormalizerAwareInterface
*/
private $decorated;
public function __construct(NormalizerInterface $decorated)
{
if (!$decorated instanceof NormalizerAwareInterface) {
throw new \InvalidArgumentException(
sprintf('The decorated normalizer must implement the %s.', NormalizerAwareInterface::class)
);
}
$this->decorated = $decorated;
}
/**
* #inheritdoc
*/
public function normalize($object, $format = null, array $context = [])
{
$data = $this->decorated->normalize($object, $format, $context);
if ('collection' === $context['operation_type'] && 'get' === $context['collection_operation_name']) {
$data['hydra:meta'] = ['foo' => 'bar'];
}
return $data;
}
/**
* #inheritdoc
*/
public function supportsNormalization($data, $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
/**
* #inheritdoc
*/
public function setNormalizer(NormalizerInterface $normalizer)
{
$this->decorated->setNormalizer($normalizer);
}
}
# config/services.yaml
services:
App\Api\ApiCollectionNormalizer:
decorates: 'api_platform.hydra.normalizer.collection'
arguments: [ '#App\Api\ApiCollectionNormalizer.inner' ]
Keep it for the records :)
I'm attempting to add what I hope is a simple voting module to posts and comments. A "Connection" is a type of post in my application. Users can vote up or down a Connection, or a Comment.
The issue I'm running into is when I attempt to attach a vote to a Connection. I receive this error: Class name must be a valid object or a string.
Here's the line of code in question:
$voteToCast = $vote->voteable()->associate($voteable);
I am certain the $voteable var is an instance of an Ardent/Eloquent model, so I can only presume the error lies within the way I am namespacing my models, or some pathetic typo I am too blind to see. Any help would be greatly appreciated.
Thanks!
Connection Model (type of post):
...
public function votes()
{
return $this->morphMany('Acme\Votes\Vote', 'voteable');
}
And the Votes Model:
/* Votes Model */
namespace Acme\Votes;
use Illuminate\Database\Eloquent\Model;
use LaravelBook\Ardent\Ardent;
class Vote extends Ardent {
protected $table = 'votes';
protected $fillable = [
'value',
'votable_id',
'voteable_type'
];
/**
* Establish the polymorphic relationship
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function voteable()
{
return $this->morphTo();
}
public function users()
{
return $this->belongsTo('Acme\\Users\\User');
}
/**
* Vote the item up
*
* #param Model $voteable
* #return mixed
*/
public static function up(Model $voteable)
{
return (new static)->cast($voteable, 1);
}
/**
* Vote the item down
*
* #param Model $voteable
* #return mixed
*/
public static function down(Model $voteable)
{
return (new static)->cast($voteable, -1);
}
/**
* Execute the vote
*
* #param Model $voteable
* #param int $value
* #return bool
*/
protected function cast(Model $voteable, $value = 1)
{
if (!$voteable->exists) return false;
$vote = new static;
$vote->value = $value;
$voteToCast = $vote->voteable()->associate($voteable);
$voteToCast->save();
}
/**
* Restrict the votes so the absolute value is 1
*
* #param $value
*/
public function setValueAttribute($value)
{
$this->attributes['value'] = ($value == -1) ? -1 : 1;
}
}
Votes Controller:
...
public function cast($connection)
{
$voteable = Connection::findOrFail($connection);
if (Input::get('value' < 1)){
return Vote::down($voteable);
}
return Vote::up($voteable);
}
After some more troubleshooting, this appears to be an issue with the way Ardent handles relationships. I was able to use Eloquent on my Vote model instead of Ardent and the voting mechanism now works flawlessly.
I can't Insert into this table and this drives me crazy
This is the error Msg I get
var_export does not handle circular references
open: /var/www/frameworks/Scout/vendor/laravel/framework/src/Illuminate/Database/Connection.php
* #param Exception $e
* #param string $query
* #param array $bindings
* #return void
*/
protected function handleQueryException(\Exception $e, $query, $bindings)
{
$bindings = var_export($bindings, true);
$message = $e->getMessage()." (SQL: {$query}) (Bindings: {$bindings})";
Here is my Full Mode
<?php
namespace Models;
use Illuminate\Database\Eloquent\Collection;
class Student extends \Eloquent
{
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'students';
/**
* The rules used to validate new Entry.
*
* #var array
*/
protected $newValidationRules = array(
'studentCode' => 'unique:students,code|numeric|required',
'studentName' => 'required|min:2',
'dateOfBirth' => 'date',
'mobile' => 'numeric'
);
/**
* Relation with sessions (Many To Many Relation)
* We added with Created_at to the Pivot table as it indicates the attendance time
*/
public function sessions()
{
return $this->belongsToMany('Models\Session', 'student_session')->withPivot('created_at')->orderBy('created_at', 'ASC');
}
/**
* Get Student Subjects depending on attendance,
*/
public function subjects()
{
$sessions = $this->sessions()->groupBy('subject_id')->get();
$subjects = new Collection();
foreach ($sessions as $session) {
$subject = $session->subject;
$subject->setRelation('student', $this);
$subjects->add($subject);
}
return $subjects;
}
/**
* Insert New Subject
* #return Boolean
*/
public function insertNew()
{
$this->validator = \Validator::make(\Input::all(), $this->newValidationRules);
if ($this->validator->passes()) {
$this->name = \Input::get('studentName');
$this->code = \Input::get('studentCode');
if ($this->save()) {
return \Response::make("You have registered the subject successfully !");
} else {
return \Response::make('An Error happened ');
}
} else {
Return $this->validator->messages()->first();
}
}
}
I am just trying to insert a new row with three Columns (I call the insertNew function on instance of Student)
1- ID automatically incremented
2- Special Code
3- Name
And I got this above Msg
What's I have tried till now :
removing all relations between from this model and other models
that has this one in the relation
Removed the validation step in insertNew()
Removed the all Input class calls and used literal data instead.
note that I use similar Inserting function on other Models and it works flawlessly
Any Comments , Replies are appreciated :D
Solution
I solved it and the problem was that I am accessing the validator
$this->validator = \Validator::make(\Input::all(), $this->newValidationRules);
And it was because I forgot that
/**
* The validator object.
*
* #var Illuminate\Validation\Validator
*/
protected $validator;
I had a similar problem. But to me, changing this code:
if ($this->validator->passes()) {
$this->name = \Input::get('studentName');
$this->code = \Input::get('studentCode');"
to this:
if ($this->validator->passes()) {
$this->setAttribute ("name" , \Input::get('studentName'));
$this->setAttribute ("code" , \Input::get('studentCode'));"
solved it.
Say I have two simple Documents like this, where a person can have many papers, but a paper can only belong to one person.
namespace Dashboard\Document;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** #ODM\Document(db="testing", collection="person")
* #ODM\InheritanceType("COLLECTION_PER_CLASS")
*/
class Person
{
/**
* #ODM\Id
*/
protected $id;
/** #ODM\Field(type="string") */
protected $slug;
/** #ODM\Field(type="string") */
protected $name;
/** #ODM\ReferenceMany(targetDocument="Paper", cascade={"all"}) */
protected $papers;
public function __get($property) {
return $this->$property;
}
public function __set($property, $value) {
$this->$property = $value;
}
public function toArray() {
return get_object_vars($this);
}
}
namespace Dashboard\Document;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** #ODM\Document(db="testing", collection="paper")
* #ODM\InheritanceType("COLLECTION_PER_CLASS")
*/
class Paper
{
/**
* #ODM\Id
*/
protected $id;
/** #ODM\Field(type="string") */
protected $name;
/** #ODM\ReferenceOne(targetDocument="Person", cascade={"all"}) */
protected $person;
public function __get($property) {
return $this->$property;
}
public function __set($property, $value) {
$this->$property = $value;
}
public function toArray() {
return get_object_vars($this);
}
}
I thought I read somewhere when you create a reference on one end, Doctrine ODM will auto create the references on both sides for you. So if I execute the statement below, I will see a reference to Person from a Paper document, AS WELL AS references to Paper(s) in a Person document.
//For demo sake; $person already contains a Person document
try {
$paper = $dm->getRepository('\Dashboard\Document\Paper')
->find($paperId);
} catch (\Doctrine\ODM\MongoDB\MongoDBException $e) {
$this->setStatusFailure($e->getMessage());
$this->sendResponse();
}
$paper->person = $person;
$dm->persist($paper);
$dm->flush();
When I do that, and check the mongodb, the reference from paper-->person is there. But I see no reference person-->paper shown in the db. I thought the cascade annotations helped with this, but obviously I'm missing something.
How can I ensure the reference is contained on both ends, so I can run queries to see all the papers that belong a single person? Does this have to be done manually, or can I have doctrine handle this for me?
UPDATE
The first paragraph on this page made me think it was possible.
http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/bidirectional-references.html
Turns out I should have read that whole page. If I use mappedBy & inversedBy, and always persist the document that has inversedBy in it's Annotation, then I get that bi-directional relationship
/** #ODM\ReferenceOne(targetDocument="Person", cascade={"all"}, inversedBy="papers") */
protected $person;
//Will give me a relationship I can query on both sides
$person->papers->add($paper);