Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Updated_at fields not changing correctly #239

Closed
karamosky opened this issue Apr 26, 2016 · 7 comments
Closed

Updated_at fields not changing correctly #239

karamosky opened this issue Apr 26, 2016 · 7 comments

Comments

@karamosky
Copy link

Hi, first, thank you for the great work you have done here 👍
I am facing an issue with my translated model, when using magic properties the timestamps are not updated correctly. do you have an idea how to fix it.
Thank you

@dimsav
Copy link
Owner

dimsav commented Apr 26, 2016

Hi, can you share some code?

@karamosky
Copy link
Author

karamosky commented Apr 26, 2016

Article model code:

<?php namespace App\Models;

use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use Translatable;


    public $translatedAttributes = ['title', 'content'];

    protected $table = 'articles';
    protected $fillable = ['published'];


    public function articleCategory()
    {
        return $this->belongsTo(\App\Models\ArticleCategory::class, 'article_categorie_id', 'id');
    }


}

Article Translation code:

<?php namespace App\Models;

use Cviebrock\EloquentSluggable\SluggableTrait;
use Illuminate\Database\Eloquent\Model;

class ArticleTranslation extends Model
{
    use SluggableTrait;

    public $timestamps = false;

    protected $sluggable = [
        'build_from' => 'title',
        'save_to' => 'slug'
    ];
    protected $table = 'article_translations';
    protected $fillable = ['title', 'content'];


}

And the controller updating my stuffs :

<?php
namespace App\Http\Controllers\Backend;

use App\Models\Article;
use App\Models\ArticleCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class ArticlesController extends BackendBaseController
{
    public function index()
    {
        $articles = Article::paginate(10);
        return view('backend.cms.articles.index')->with('articles',$articles);
    }

    public function add(Request $request) {
        if($request->isMethod('get'))
            return view('backend.cms.articles.add')->with('categories', ArticleCategory::all());

        $this->validate($request, [
            'title' => 'required|max:255',
            'content' => 'required',
        ]);

        $article = new Article();
        $article->title = $request->input('title');
        $article->content = $request->input('content');
        $article->articleCategory()->associate($request->input('article_categorie_id'));
        if($article->save())
            return back()->with('save_success', 'La page a été créé avec succès');
    }

    public function edit(Request $request, $id) {

        $article = Article::findOrFail($id);
        if($request->input('lang')) {
            App::setLocale($request->input('lang'));
        }

        if($request->isMethod('get')) {
            return view('backend.cms.articles.edit')->with('article', $article)->with('categories', ArticleCategory::all());
        }

        $this->validate($request, [
            'title' => 'required|max:255',
            'content' => 'required',
        ]);

        $article->title = $request->input('title');
        $article->content = $request->input('content');
        $article->articleCategory()->associate($request->input('article_categorie_id'));
        if($article->save())
            return back()->with('save_success', 'La page a été enregistrée avec succès');

    }
}

Thanks

@dimsav
Copy link
Owner

dimsav commented Apr 26, 2016

Can you describe what's happening, what action has the issue? What do you mean the timestamps are not updated correctly?

@karamosky
Copy link
Author

karamosky commented Apr 26, 2016

The timestamps are not updated if the update affects ArticleTranslation model attributes (columns are in translations table), but they are updated if changes affects attributes of my Article model (columns exists in the table articles).
In my project, i need to show the updated_at date, but if someone changes only title or content (which is translated) the updated_at does not change.
example :

$article->title = "a title";
$article->content = "some content";
$article->save(); // the timestamps are not modified
--------------------------------------------------------------------------------------------
// if changes affects columns in the same table
$article->title = "a title";
$article->content = "some content";
$article->categorie()->associate($categorie); // this column is part of the table articles
$article->save(); // timestamps are updated but only if categorie changes wich is normal

the issue happen in the edit action.
hope it was clear enough

@BenConstable
Copy link

@karamosky you should just be able to add the following to your ArticleTranslation model to get the desired functionality:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ArticleTranslation extends Model
{
    // Your existing code...

    /**
     * All of the relationships to be touched.
     *
     * (see https://laravel.com/docs/5.2/eloquent-relationships#touching-parent-timestamps)
     *
     * @var array
     */
    protected $touches = ['article'];

    /**
     * Setup the relationship to the parent article.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function article()
    {
        return $this->belongsTo(Article::class);
    }
}

@karamosky
Copy link
Author

@BenConstable Thank you, this is exactly what i was looking for 👍 it works now.
I was thinking that this is handeled transparently by Laravel-Translatable.
Great day.

@BenConstable
Copy link

No problem, glad I could help!

@dimsav dimsav closed this as completed Apr 27, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants