【发布时间】:2013-12-01 23:56:14
【问题描述】:
我正在尝试在 CakePHP 中检索翻译后的数据,但遇到了一些问题。
我的模型如下。
class Newelement extends AppModel{
public $name = "Newelement";
public $actsAs = array(
'Translate' => array(
'title' => 'titleTranslation',
'subtitle' => 'subtitleTranslation',
'content' => 'contentTranslation'
)
);
// Use a different model (and table)
public $translateModel = 'NewElementTranslation';
// Use a different table for translateModel
public $translateTable = 'newelement_translations';
}
我的翻译模型是这样的:
class NewelementTranslation extends AppModel {
public $displayField = 'field'; // important
}
我可以使用不同的语言环境保存数据。数据已正确保存在数据库中。
但是当我尝试使用“查找”方法检索数据时...
$newelements = $this->Newelement->find();
print_r($newelements);
我收到以下错误:
Missing Database Table
Error: Table new_element_translations for model titleTranslation was not found in datasource default.
Notice: If you want to customize this error message, create app/View/Errors/missing_table.ctp
Stack Trace
CORE/Cake/Model/Model.php line 3476 → Model->setSource(string)
CORE/Cake/Model/Datasource/DboSource.php line 1063 → Model->getDataSource()
CORE/Cake/Model/Model.php line 2880 → DboSource->read(Newelement, array)
CORE/Cake/Model/Model.php line 2852 → Model->_readDataSource(string, array)
APP/Controller/NewelementsController.php line 39 → Model->find()
[internal function] → NewelementsController->index()
CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(NewelementsController, array)
CORE/Cake/Routing/Dispatcher.php line 187 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 162 → Dispatcher->_invoke(NewelementsController, CakeRequest, CakeResponse)
APP/webroot/index.php line 110 → Dispatcher->dispatch(CakeRequest, CakeResponse)
如果我使用默认 i18n 数据库、将此代码用于我的模型并避免使用已翻译模型,则不会发生这种情况。
class Newelement extends AppModel{
public $name = "Newelement";
public $actsAs = array(
'Translate' => array(
'title' => 'titleTranslation',
'subtitle' => 'subtitleTranslation',
'content' => 'contentTranslation'
)
);
}
我想使用 translateModel 和 translateTable 轻松维护数据库,因为这不是我要翻译的唯一模型。
如果有人可以帮助我,那就太好了,因为我一直坚持这一点。
谢谢!
【问题讨论】:
-
我不确定,但请尝试坚持大小写标准 - 您的模型应该是
NewElement,而不是Newelement...等 -
谢谢!正如你所说,我对其中一个模型类有误。