Yii2 Multilingual问题:无法访问翻译表字段
joinWith('translation') Not Working I’ve run into a similar snag with the OmgDef/Multilingual extension before—here are some practical checks to help you get this sorted:
Verify the translation relationship in your PageAdmin model
Double-check that yourtranslation()method is correctly defined. It should look something like this (adjust the translation model name if yours differs):public function translation() { return $this->hasOne(PageAdminTranslation::class, 'page_admin_id'); }Make sure the foreign key matches the one in your translation table, and that the translation model has the
localefield set up properly.Confirm default language configuration
Ensure your app’s default language is correctly set tobg:- Check your multilingual config file (usually
config/multilingual.php) for'default_locale' => 'bg' - You might also need to explicitly set the default language in your PageAdmin model:
public $defaultLanguage = 'bg';
- Check your multilingual config file (usually
Check the PageAdminSearch model setup
If you’re usingjoinWith('translation')in the search model, make sure:- The search model properly extends the base PageAdmin model and inherits the multilingual traits/relationships
- Sometimes using
->withTranslation()instead ofjoinWith()works better for loading translations in search contexts—give that a try if you haven’t already - Ensure your search query isn’t overriding the join with other conditions that exclude the translation table
Validate database translation records
Double-check that you actually havebglanguage translation entries for your PageAdmin records in the database. If there’s no matching translation row for a PageAdmin entry, joining won’t add any visible changes.Debug the generated SQL
Dump the final SQL query to see if the join is being applied correctly. Add this line right after your query build:dd($query->toSql());Look for a JOIN clause with your translation table, and confirm it includes a condition filtering for
locale = 'bg'.
内容的提问来源于stack exchange,提问作者Toma Tomov

