使用 Anahkiasen/Polyglot 读取数据 returns 尝试获取 属性 的 non-object 错误
Reading data using Anahkiasen/Polyglot returns error trying to get property of non-object
将 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',
];
}
我的 blade 模板:
$page->nl->title
/*
This is what's causing the error
$page->title doesn't produce errors but is, of course, empty
*/
在这上面停留了一段时间。非常感谢任何帮助:-)
我不熟悉这个库,但是看看 base Polyglot class,看起来这个抽象是通过使用 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');
的配置字段。我会检查是否调用该配置字段 returns nl
作为配置的语言环境
var_dump(Config::get('polyglot::locales'));
我猜它不会,可能是因为您没有 运行 artisan 命令来发布包的配置
php artisan config:publish anahkiasen/polyglot
将 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',
];
}
我的 blade 模板:
$page->nl->title
/*
This is what's causing the error
$page->title doesn't produce errors but is, of course, empty
*/
在这上面停留了一段时间。非常感谢任何帮助:-)
我不熟悉这个库,但是看看 base Polyglot class,看起来这个抽象是通过使用 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');
的配置字段。我会检查是否调用该配置字段 returns nl
作为配置的语言环境
var_dump(Config::get('polyglot::locales'));
我猜它不会,可能是因为您没有 运行 artisan 命令来发布包的配置
php artisan config:publish anahkiasen/polyglot