【问题标题】:CakePHP - Re-imported model and (i18n) translate with empty stringsCakePHP - 重新导入模型和(i18n)用空字符串翻译
【发布时间】:2013-06-25 13:32:35
【问题描述】:

问题是我导入了一个模型。

App::import('Model', 'Carrier');
$this->Carrier = new Carrier;

我稍后会重新导入并安装此模型。通常它也会起作用。但是,这是一个多语言网站。第二个实例返回一个空字符串。

我试过了

App::import('Model', 'Carrier');
$this->Carrier = new Carrier;
... blabla...
App::import('Model', 'Carrier');
$this->getCarrier = new Carrier;
... blabla...

并尝试过:

App::import('Model', 'Carrier');
$this->Carrier = new Carrier;
... blabla...
unset($this->Carrier);
App::import('Model', 'Carrier');
$this->Carrier = new Carrier;
... blabla...

同样的结果:第二次实例化它从数据库返回一个空字符串。

我的翻译模型:

<?php
class Carrier extends AppModel
        {
        var $name = 'Carrier';
        public $actsAs = array('Translate' => array(
                                                'name',
                                                'description'
                                                     )
                                );
        }
?>

更新:

正常结果:

Array ( [Carrier] => Array ( [id] => 1 [name] => TestCarrier [description] => Example  [status] => 1 ) )

重新导入模型的错误结果:

Array ( [Carrier] => Array ( [id] => 1 [name] =>  [description] => [status] => 1 ) )

【问题讨论】:

  • 不要使用App::import('Model', 'Carrier'); $this-&gt;Carrier = new Carrier;!您应该始终使用ClassRegistry::init('Carrier') 包含您的模型。此外,不需要 App::uses()。
  • 我试过了:ClassRegistry::init('Carrier'); $this->Carrier = 新的运营商;但结果相同...... :-(
  • 毕竟好。对不起。我先用错了。 ClassRegistry::init('Carrier')-&gt;findById(key($carriers));谢谢!!!

标签: cakephp internationalization translation cakephp-2.1 cakephp-model


【解决方案1】:

你不应该使用 App::import()。这仅适用于供应商类。 在内部,它的 App::uses()。

但对于模型,这也不适用。 只需使用 ClassRegistry::init():

$Carrier = ClassRegistry::init('Carrier');
$results = Carrier->find(...);

对于模型,蛋糕有自己的加载机制。

【讨论】:

    猜你喜欢
    • 2014-09-07
    • 2018-06-16
    • 2013-07-09
    • 2012-11-11
    • 2015-10-27
    • 2013-07-18
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    相关资源
    最近更新 更多