0

好吧,老实说,我在这里有点迷失了,我为 joomla 2.5 创建了一个组件,并在数据库中有两个表,“managers”和另一个名为“Branches”。我正在尝试将经理与分支机构一起加入。

我希望我的问题能更精确,但老实说我不明白,这是我调用数据库的模型文件:

<?php
defined( '_JEXEC' ) or die;

 jimport('joomla.application.component.model');

class LocateModelBranches extends JModel
{
public function getItem()
{
    $branch_id = JRequest::getInt('id');

    $row = JTable::getInstance('branches', 'LocateTable');
    $row->load($branch_id);

    return $row;
}

public function getBranches()
{
    $branch_id = JRequest::getInt('id');

    $db = $this->getDbo();
    $query = $db->getQuery(true);

    $query->select('*');
    $query->from('#__branches');
    $query->join('LEFT', '#__managers AS a USING(manager_id)');
    $query->where("published = 1");

    $db->setQuery($query);

    $rows = $db->loadObjectList();

    return $rows;
}
}

然后是我的 view.json.php 文件:

<?php
defined( '_JEXEC' ) or die;

jimport( 'joomla.application.component.view');

class LocateViewBranches extends JView
{
public function display($tpl = null)
{
    $branch = $this->get('Branches');
    $response = array();

    foreach ($branch as $row) {
            $response[] = array(
                    'lat' => $row->branch_latitude,
                    'lng' => $row->branch_longitude,
                    'data' => array(
                        'name' => $row->branch_name,
                        'address' => $row->branch_address,
                        'fax' => $row->branch_fax,
                        'email' => $row->branch_email,
                        'city' => $row->branch_city,
                        'telephone' => $row->branch_telephone,
                        'id' => $row->branch_id,
                        'lati' => $row->branch_latitude,
                        'lngi' => $row->branch_longitude,
                        'manager_id' => $row->manager_id,
                    ),
                );
        }
    echo json_encode($response);
}
}

然后我得到回应:

[]

如果我删除“$query->join('LEFT', '#__managers AS a USING(manager_id)');” 然后我的回应是(这是我想要的以及经理的详细信息:

[{"lat":"-33.9249","lng":"18.4241","data":{"name":"test 2","address":"greenpoint, Cape Town","fax":"044 382 0605","email":"test","city":"blah","telephone":"044 382 0605","id":"2","lati":"-33.9249","lngi":"18.4241"}},{"lat":"-34.0438","lng":"23.0759","data":{"name":"jam factory","address":"15 meeu street knysna","fax":"00000000","email":"00000000","city":"am factory","telephone":"0000000","id":"14","lati":"-34.0438","lngi":"23.0759"}}]

我确定这只是我的 view.json.php 的问题

另外,如果我从 json_encode 更改为 print_r 并保留“$query->join('LEFT', '#__managers AS a USING(manager_id)');” 我得到以下回复:

Array
 (
 )
 1

所以肯定这就是我输出数据的方式

4

1 回答 1

0

简单如:

    public function getBranches()
{
    $branch_id = JRequest::getInt('id');

    $db = $this->getDbo();
    $query = $db->getQuery(true);

    $query->select('*, a.manager_name,manager_mobile');
    $query->from('#__branches AS t')
            ->join('LEFT', '#__managers AS a USING(manager_id)')
            ->where('t.published = 1');

    $db->setQuery($query);

    $rows = $db->loadObjectList();

    return $rows;
}

感谢大家的意见。:)

于 2012-11-27T21:40:16.187 回答