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

Seeder file with multi langue #650

Closed
spyap7 opened this issue Aug 27, 2021 · 1 comment
Closed

Seeder file with multi langue #650

spyap7 opened this issue Aug 27, 2021 · 1 comment

Comments

@spyap7
Copy link

spyap7 commented Aug 27, 2021

Hi, I newbie for October CMS, currently I trying make seeder file for data. Can someone guide a sample code for many data with translate on seeder file?

The "title" will have translate to English and Chinese version, when I run "php artisan october:migrate" will show error : "Unexpected type of array when attempting to save attribute "title", try adding it to the $jsonable property.".

But when I add "title" for $jsonable the data will store as array on Model and "rainlab_translate_attributes" didn't have record.

Below is sample code I trying:

<?php namespace App\Test\Updates;

use Seeder;
use App\Test\Models\Resource;
use System\Models\File;

class SeedResourcesTable extends Seeder
{
    public function Run()
    {
        $resources = [
            [
                'title' => ['en' => 'This is English version',
                            'cn' => 'This is Chinese version'],
                'sources' => json_decode('[{"author":"ABCD","year":"2020"}]'),
                'isActive' => 1,
                'source_file' => ['Resources1.pdf']
            ]
        ];

        foreach ($resources as $resource) {
            $model = Resource::create($resource);
            $model['sort_order'] = $model->getkey();

            foreach($resource['source_file'] as $key => $pdf) {
                $filePath = "./themes/app/assets/resource/" . $pdf;
                if (!file_exists($filePath)) {
                    echo 'File ' . $filePath . ' not exists.' . "\n";
                    continue;
                }
                $file = new File();
                $file->fromFile($filePath);
                //$file['title'] = $model->title;
                //$file['description'] = $model->content['description'];
                $model->source_file()->save($file, null);
            }
        }
    }
}

Thank you

@daftspunk
Copy link
Member

Hi @spyap7

You may need to implement the translateContext() method to get the attributes to save. Take a look at this tutorial for more info: https://www.youtube.com/watch?v=pDr3KAvZY44

I hope this helps

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

No branches or pull requests

2 participants