Custom module: entity/model not updating using translations



  • Let's see how can I explain this.

    I have a Testimonials module. This module will have translations. And I added in the TestimonialsServiceProvider a trigger, so when I create a new testimonial, it modifies a column in a table.

    The problem is, on updating. If I modify the testimonial, only the testimonials__testimonial_translations table is affected, but the timestamps on testimonials__testimonials are not, so there're no events fired when the model is updated/saved. I guess that's because only the TestimonialTranslation model is affected, and not the 'parent' Testimonial. (But I think the parent model should be aware).

    Which relation I'm missing... and where do I have to put it? on the Testimonial entity? on the TestimonialTranslation entity? On both?

    I'm a bit lost here, because I don't see a direct relation between the model and the translation model... Actually I'm missing what exactly does the Dimsav\Translatable\Translable class is doing in the entity declaration... (Well it does manage the translations)



  • @zedee

    AutoSolved:

    This is not an AsgardCMS issue / question, but from how the relations are made on the Entities.

    So, for others looking for this, the TestimonialTranslation entity should look like this:

    <?php namespace Modules\Testimonials\Entities;
    
    use Illuminate\Database\Eloquent\Model;
    
    class TestimonialTranslation extends Model
    {	
        public $timestamps = false;
        
        protected $fillable = [
            'testimonial_id',
            'title',
            'content'
        ];
        protected $table = 'testimonials__testimonial_translations';
      
        protected $touches = ['news'];  // THIS enables 'touching' (updates timestamps) on the 'parent' model.
        
        // And we make the relation with the model we want to touch
        public function testimonial() 
        {
            return $this->belongsTo(Testimonial::class);
        }
    }
    

    And that's it! :)


  • Global Moderator

    @zedee

    Yeah if i remember correctly dimsav/laravel-translatable does not touch parents timestamps out of the box.
    But you can try and use $touches attribute on model.

    Should go something like this (in translation entity)

    use Modules\Testimonials\Entities\Testimonial;
    
    protected protected $touches = ['testimonial'];
    
    public function testimonial()
    {
         return $this->belongsTo(Testimonial::class);
    }
    

    Same minute posts :D



  • @armababy

    Haha, yes! We both answered at same time... :D

    I investigated a bit and found the solution by myself... (well, actually I found this):

    https://github.com/dimsav/laravel-translatable/issues/239

    I also thought the updating was doing out-of-the box.

    By the way... I know it's time consuming, but would be possible to make a screencast like the one published (the testimonials one), but with translations? All details like this one, can bring confusion to newcomers...
    Or make a tutorial? Well, perhaps I'd be able to do by myself, but I may be wrong on some stuff as I don't know a deep knowledge of the CMS innerings...



  • thanks for the post I also have the same "issue" didn't have time so far to focus on it ! I know how to do now :)


Log in to reply
 

Looks like your connection to AsgardCms was lost, please wait while we try to reconnect.