如何在Zend模型中的select查询中使用locale

时间:2012-09-26 23:12:27

标签: zend-framework locale

在我的管理员cms上,我可以添加newsitems并将“language”列中的语言代码添加到newsitem'en'或'nl'。在bootstrap文件中,语言通过以下方式设置:

public function _initLanguage()
{
    $objSessionLanguage= new Zend_Session_Namespace('Zend_Lang'); 
    $objLocale = new Zend_Locale();
    $locale = new Zend_Locale();
    $language = $locale->getLanguage();
    $region = $locale->getRegion();
    Zend_Loader::loadClass('Zend_Controller_Request_Http');
    $request = new Zend_Controller_Request_Http();

    if($language=='nl' or $language=='en')
    {
        if($language=='nl')
        {
            $localFile = 'dutch.php';        
            $Locale = 'nl';
        }else
        {           {
                $localFile = 'english.php';      
                $Locale = 'en';

        }
        $objSessionLanguage->localFile=$localFile;
        $objSessionLanguage->Locale=$Locale;

    }else
    {
        if(!isset($objSessionLanguage->localFile))
        {
            $localFile = 'english.php';      
            $Locale = 'en';
        }else
        {
            $localFile = $objSessionLanguage->localFile;
            $Locale =$objSessionLanguage->Locale;
        }           
    }

    $objTranslate = new Zend_Translate('array', APPLICATION_PATH .'/../language/english.php', 'en');
    $objTranslate->addTranslation(APPLICATION_PATH .'/../language/'.$localFile, $Locale);
    $objTranslate->setLocale($Locale);
    Zend_Registry::set("Zend_Translate", $objTranslate);
}

要在NewsList中显示newsitems,我想根据语言在newsmodel中选择newsitems。

<?php

class Admin_Model_News extends  Zend_Db_Table_Abstract
{
    protected $_modelName = 'news';
    protected $_modelLabel = 'News';
    protected $_name = 'news';  
    protected $_objGeneralSettingVar;

public function init()
{
    parent::init(); 
    $this->_objGeneralSettingVar =  Zend_Registry::get( "objGeneralSettingVar");
}    
public function fetchNewsList()
{
    $objSelect = $this->select()->limit(5);
    $objSelect->where ("language = '$language'");
    $objSelect->order("news_date DESC");

    return $this->fetchAll($objSelect)->toArray(); 
}
}

但是用上面的

$objSelect->where ("language = '$language'");

没有显示新网站。我确信我错过了一些东西,但似乎无法找到它。如何在语言选择newsitems时使用语言设置?

1 个答案:

答案 0 :(得分:0)

在Admin_Model_News中,您正在使用

$objSelect->where ("language = '$language'");

代表您的where子句,但$language未在任何地方设置,因此您要查询Where language = null

函数fetchNewsList应如下所示: -

public function fetchNewsList($language)
{
    $objSelect = $this->select()->limit(5);
    $objSelect->where ("language = '$language'");
    $objSelect->order("news_date DESC");

    return $this->fetchAll($objSelect)->toArray(); 
}

您没有显示您使用Admin_Model_News的方式,但它应该是这样的: -

$news = new Admin_Model_News();
$newList = $news->fetchNewsList(howeverYouGetlanguage());