代码之家  ›  专栏  ›  技术社区  ›  michi

访问自定义组件的站点视图中由外键链接的数据

  •  0
  • michi  · 技术社区  · 12 年前

    在我的自定义组件中,在站点视图中,我有一个国家列表(视图:国家)。单击某个国家,将显示另一个视图(视图:人),显示居住在该国家的所有人。

    现在,在个人视图中,我想显示国家的名称和国旗。

    所以我想添加一个 function getCountry() 在里面 ...site/models/persons.php :

    public function getCountry() {
        $db = $this->getDBO(); 
        $id = $this->state->get("filter.country");
        $sql = "SELECT * FROM #__cnlp_persons_country WHERE id = $id";
        $db->setQuery($sql); 
        $country = $db->loadResult(); 
        // var_dump($country);
        return $country; 
    }
    

    然后,我添加到 .../site/views/persons/view.html.php :

    class Cnlp_personsViewPersons extends JView
    {
        protected $items;
        protected $state;
        protected $params;
        protected $country; // <--- I added this
        ...
    
        public function display($tpl = null)
    {
            $app                = JFactory::getApplication();
            $this->state        = $this->get('State');
            $this->items        = $this->get('Items');
            $this->params               = $app->getParams('com_cnlp_trainers');
            $this->country              = $this->get('Country'); // <--- I added this
            (...)
    

    结果:我以为我可以进去 ---/site/views/persons/tmpl/default.php 像。。。

    <h1><?php echo $this->country->name; ?></h1>
    <img src="<?php echo $this->country->flag; ?>" />
    

    …但我明白 无输出 ... 我做错了什么?

    1 回复  |  直到 12 年前
        1
  •  0
  •   di3sel    12 年前

    $db->loadResult(); 用于加载单个值的结果(例如,如果您只想选择国家/地区名称或类似的内容),但在查询中您选择的是整行,因此您应该使用其中一个:

    $db->loadRow(); // returns an indexed array from a single record in the table
    $db->loadAssoc(); // returns an associated array from a single record in the table
    $db->loadObject(); // returns a PHP object from a single record in the table:
    

    你可以阅读更多关于这方面的内容 here (这是joomla 1.5文档,但2.5和3的文档相同)