如何使用phalcon createbuilder连接多个表

时间:2017-08-23 13:59:58

标签: php mysql phalcon

这是使用 PHQL 显示来自3个不同表的行的工作代码。我想使用$this->modelsManager->createBuilder()进行相同的编码。请在下面找到工作代码和错误。

控制器

<?php
    $phql  = 'SELECT product.*, currency.*, unittype.* FROM product LEFT JOIN currency ON product.CurrencyId=currency.Id LEFT JOIN unittype ON product.UnitTypeId=unittype.Id ORDER BY product.Name ASC';
    $query = $this->modelsManager->createQuery($phql);
    $this->view->products = $query->execute();
?>

查看

<table class="table">
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Unit Type</th>
        <th>Price</th>
        <th>Currency</th>
        <th></th>
        <th></th>
    </tr>
    <?php foreach($products as $row)    { ?>
    <tr>
        <td><?php echo $row->product->Name; ?></td>
        <td><?php echo $row->product->Description; ?></td>
        <td><?php echo $row->unittype->Name; ?></td>
        <td><?php echo $row->product->Price; ?></td>
        <td><?php echo $row->currency->Code; ?></td>
        <td><?php echo $this->tag->linkTo('product/edit/'.$row->product->Id,'Edit'); ?></td>
        <td><?php echo $this->tag->linkTo('product/delete/'.$row->product->Id,'Delete'); ?></td>
    </tr>
    <?php } ?>
</table>
  

我想使用来自modelmanager的createbuilder;如何写查询和   在视图中打印?

     

下面是我编写的代码并有问题:

控制器

<?php
    $this->view->products = $this->modelsManager->createBuilder()
        ->from('product')
        ->innerjoin('currency','product.CurrencyId=currency.Id')
        ->orderBy('product.Name')
        ->getQuery()
        ->execute();
?>

查看

<table class="table">
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>Unit Type</th>
        <th>Price</th>
        <th>Currency</th>
        <th></th>
        <th></th>
    </tr>
    <?php foreach($products as $product)    { ?>
    <tr>
        <td><?php echo $product->Name; ?></td>
        <td><?php echo $product->Description; ?></td>
        <td><?php echo $product->UnitTypeId; ?></td>
        <td><?php echo $product->Price; ?></td>
        <td><?php echo $product->currency->Id; ?></td>
        <td><?php echo $this->tag->linkTo('product/edit/'.$product->Id,'Edit'); ?></td>
        <td><?php echo $this->tag->linkTo('product/delete/'.$product->Id,'Delete'); ?></td>
    </tr>
    <?php } ?>
</table>

以上代码产生以下错误:

  

注意:尝试获取非对象的属性   第xx行的C:\ wamp \ www \ xxxx.volt.php

1 个答案:

答案 0 :(得分:3)

在您的控制器中尝试此操作。结果数据应为Resultset

$builder = $this->modelsManager->createBuilder();

$builder
    ->columns(
        [
            'Product.Id'          => 'productId',
            'Product.Name'        => 'productName',
            'Product.Description' => 'productDescription',
            'Unittype.Name'       => 'unitTypeName',
            'Product.Price'       => 'productPrice',
            'Currency.Code'       => 'Currency.Code',
        ]
    )
    ->addFrom('Product')
    ->leftJoin('Currency', 'Product.CurrencyId = Currency.Id')
    ->leftJoin('Unittype', 'Product.UnitTypeId = Unittype.Id')
    ->orderBy('Product.Name')
    ->getQuery();

$data = $builder->execute();

var_dump($data);

这将只返回您视图中的字段。