This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hello,
I'm migrating an old site to october that has lots of nodes and lots of translations in i18n table. To keep thing simple, I copied both tables and created coresponding models with realtions in october and all works as expected in the frontend. But I'm having trouble setting up the relations in the backend.
My node model has:
public $hasMany = [ 'translations' => '...' ];
The translation model has: public $belongsTo = [ 'node' => '...' ];
When I'm trying to set up backend form, the above doesn't work at all, I also tried few other relation options, but nothing give the results I want.
So I decided to give a try on Translate plugin, it's very easy to implement and is working great, but I have lots of data to migrate. To keep things simple, I tried to copy all data from the old 'i18n' table to 'rainlab_translate_attributes' table, in the correct format of course, but translations are not loading neither in the front- or backend.
I inserted all rows with SQL, directly in the datase and have no idea what is wrong. If I manual update an entry with: {"title":"something","body":"something"}, it works. What could be the problem with the SQL entries?
Also, I'm curious how could I set up the relations with the first approach.
Any suggestions are welcome. Thank you.
Last updated
What are your Model's $translatable
entries?
Be aware that there is also a rainlab_translate_indexes
table. It's used for translatable entries using "index=>true".
Last updated
mjauvin said:
What are your Model's
$translatable
entries?Be aware that there is also a
rainlab_translate_indexes
table. It's used for translatale entries using "index=>true".
Thanks for the promt answer. public $translatable = ['title', 'body']; The 'rainlab_translate_indexes' table is empty. Howevery translation does work on manually added or edited entries. The problem is only with the ones added with SQL.
Update: The problem with the 'rainlab_translate_attributes'
table is solved. It was such a silly mistake, I didn't not escape the json string properly, MySQL function JSON_QUOTE()
solved the issue.
However, now a new issue raised. The old structure had a node
model with 'body' field, but that field was owerridden in the i18n
table. With the Translate plugin, 'body' filed from nodes
table is used, even if there is an entry 'rainlab_translate_attributes'
table for the default language.
-
Is it possilbe to enforce Translate plugin to read from
'rainlab_translate_attributes'
table first, and use the model only if there is no entry in'rainlab_translate_attributes'
? Similar to'rainlab_translate_messages'
, where we can have: {"x":"nav.home","en":"Home"}. 'x', gets displayed by default, but it's ignored if there is 'en'? -
I'm still curious how could I setup backend relations with the i18n approach. How to load 'hasMany' relations in backend, not as 'Select' option, but editable fields and load only connected ones.
mjauvin said:
Can you be less generic and show some code so that I know exactly what we're talking about?
For 1. I don't have any code yet.
For 2:
`class Node extends Model { public $table = 'myplugins_node_nodes';
/*
* Relation
*/
public $hasMany = [
'translations' => 'Myplugins\Node\Models\Translation'
];
}
class Translation extends Model {
public $table = 'myplugins_node_translations';
/*
* Relation
*/
public $belongsTo = [
'node' => 'Myplugins\Node\Models\Node'
];
} `
The above works in the frontend, but I don't know how to set it up in the backend. The following fileds.yaml give error: "Trying to access array offset on value of type int".
Fields for node: `
fields:
title:
label: Title
span: auto
type: text
body:
label: Body
span: full
size: large
type: richeditor
translations:
label: Relation
nameFrom: node_id
descriptionFrom: description
span: auto
type: relation
`
What does "works in the frontend" mean exactly? Example of usage? How did you try to set it up on the backend?
mjauvin said:
What does "works in the frontend" mean exactly? Example of usage? How did you try to set it up on the backend?
In frontend, for examlple with the Record details component, I just use {{ record.translations }}
and can loop trugh all translations that a node has.
In the backend I tried different things, from simply seeting up a relation with backend/rainlab/builder Models Froms Fields option, which gives error: "Trying to access array offset on value of type int".
Then I tried: `
translations:
label: Translation
manage:
form: $/myplugins/node/models/translation/fields.yaml
list: $/myplugins/node/models/translation/columns.yaml
view:
list: $/myplugins/node/models/translation/columns.yaml
toolbarButtons: create|delete
`
which gives: "htmlspecialchars() expects parameter 1 to be string, array given"
Update: Got the solution for "Trying to access array offset on value of type int". Adding nameFrom: title
line, where is title
is i18n table column.
translations:
label: Translations
nameFrom: title
descriptionFrom: description
span: auto
type: relation
However, now all transaltions are displayed in select box. I'm trying to figure out how to change that now with 'recordfinder' or other solution.
did yo try changing "type: relation" to "type: recordfinder" ?
ref. https://octobercms.com/docs/backend/forms#widget-recordfinder
mjauvin said:
did yo try changing "type: relation" to "type: recordfinder" ?
ref. https://octobercms.com/docs/backend/forms#widget-recordfinder
Yes, I tried Recordfinder, but didn't manage to get it work. Should that work with 'hasMany' relations as well?
I finally manged to get it work with 'type: partial', based on this tutorial: https://www.youtube.com/watch?v=LJWapm4gLkA However not all the steps worked. For example having <?= $this->relationRender('translations') ?>
in create.htm didn't return anything, just after I created partial and moved there.
Thank you for your help. I still have lots to learn.
Have a nice weekend.
1-13 of 13