如何使用 phalcon createbuilder 连接多个表

How to use phalcon createbuilder for join multiple tables

这里是使用 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>

I wanted to use createbuilder from modelmanager; how to writequery and print in view?

below is the code i wrote and having issue:

控制器

<?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>

以上代码产生以下错误:

Notice: Trying to get property of non-object in C:\wamp\www\xxxx.volt.php on line xx

在你的控制器中试试这个。结果数据应该是 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);

这将 return 只有您在视图中拥有的字段。