1

Anahkiasen/Polyglot パッケージを Laravel 4.2 プロジェクトに追加した後、これを機能させようとしました。あるべきだと思うようにすべてをセットアップしました(ドキュメントはちょっと悪いです)。データベースへの保存は問題ないようですが、読みたいときに次のエラーが発生します。

Trying to get property of non-object (View: /Applications/MAMP/htdocs/*my view*.blade.php)

モデル:

use Polyglot\Polyglot;

class Page extends Polyglot {

    use SoftDeletingTrait;

    protected $fillable = [
        'lang',
        'meta_title',
        'meta_description',
        'title',
        'page_title',
        'page_content',
    ];
    protected $polyglot = [
        'meta_title',
        'meta_description',
        'title',
        'page_title',
        'page_content',
    ];

    // ...
}

class PageLang extends Eloquent {

    public $timestamps = false;
    protected $fillable = [
        'page_id',
        'lang',
        'meta_title',
        'meta_description',
        'title',
        'page_title',
        'page_content',
    ];
}

私のブレードテンプレート:

$page->nl->title
/*
This is what's causing the error
$page->title doesn't produce errors but is, of course, empty
*/

しばらくこれで立ち往生しています。どんな助けでも大歓迎です:-)

4

1 に答える 1

2

私はライブラリに精通していませんが、ベースの Polyglot クラスを見ると、この抽象化は、PHP のマジック__get変数を使用して、次のようなものにアクセスするときに n ローカリゼーション オブジェクトを挿入することで機能しているようです。$page->nl

を見て__get

public function __get($key)
{
    // If the relation has been loaded already, return it
    if (array_key_exists($key, $this->relations)) {
        return $this->relations[$key];
    }
    // If the model supports the locale, load and return it
    if (in_array($key, $this->getAvailable())) {
        $relation = $this->hasOne($this->getLangClass())->whereLang($key);
        if ($relation->getResults() === null) {
            $relation = $this->hasOne($this->getLangClass())->whereLang(Config::get('polyglot::fallback'));
        }
        return $this->relations[$key] = $relation->getResults();
    }
    // If the attribute is set to be automatically localized
    if ($this->polyglot) {
        if (in_array($key, $this->polyglot)) {
            /**
             * If query executed with join and a property is already there
             */
            if (isset($this->attributes[$key])) {
                return $this->attributes[$key];
            }
            $lang = Lang::getLocale();
            return $this->$lang ? $this->$lang->$key : null;
        }
    }
    return parent::__get($key);
}

__get失敗した場合、親が呼び出される結果になる多くの条件があります

return parent::__get($key);

つまり、通常のモデルの動作です。それはおそらく上で起こっていることです -- そしてnl、set オブジェクトではないので、メソッドを呼び出そうとすると PHP は文句を言います。

の 3 つの条件文のうち__get、これが失敗する可能性が最も高い候補のようです。

    if (in_array($key, $this->getAvailable())) {
        $relation = $this->hasOne($this->getLangClass())->whereLang($key);
        if ($relation->getResults() === null) {
            $relation = $this->hasOne($this->getLangClass())->whereLang(Config::get('polyglot::fallback'));
        }
        return $this->relations[$key] = $relation->getResults();
    }

あなたの特定のケースなら、それは次のようになります

    if (in_array('nl', $this->getAvailable())) {
        //...
    }

見つめているgetAvailable()

protected function getAvailable()
{
    return Config::get('polyglot::locales');
}

という名前の構成フィールドを探しますConfig::get('polyglot::locales');。その構成フィールドを呼び出すと、構成nlされたロケールとして返されるかどうかを確認します

var_dump(Config::get('polyglot::locales'));

パッケージの構成を公開するために artisan コマンドを実行していない可能性があります。

php artisan config:publish anahkiasen/polyglot
于 2015-04-09T17:57:45.043 に答える