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它不是一个集合对象,当您尝试调用它的方法时,PHP 会抱怨。

在 中的三个条件句中__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 回答