2017-08-23 27 views
-1

這裏是使用PHQL來顯示來自3個不同表格的行的工作代碼。我想使用$this->modelsManager->createBuilder()來編碼。請在下面找到工作代碼和錯誤。如何使用phalcon 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

+0

哪一行是'xx'?您是否嘗試過使用'<?php die(var_dump($ products))?>'來查看您是否返回任何數據? – PaulSCoder

+0

@PaulSCoder是,如果你需要我的全部源代碼,它的返回空數組 –

+0

@PaulSCoder你可以在這裏找到https://github.com/syednizamudeen/invoice –

回答

2

在你的控制器試試這個。由此產生的數據應該是一個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); 

這將返回只有你在你看來有場。

+0

'<?PHP \t \t \t $建設者= $這個 - > modelsManager - > createBuilder() \t \t \t - >列( \t \t \t \t [ \t \t \t \t \t 'PRODUCTID'=> 'product.Id', \t \t \t \t \t '產品名稱'=> 'product.Name', \t \t \t \t \t '產品描述'=> 'product.Description', \t \t \t \t \t 'UnittypeName'=> 'unittype.Name', \t \t \t \t \t 'ProductPrice'=> 'product.Price', \t \t \t \t \t '貨幣代碼'=> 'currency.Code', \t \t \t \t] \t \t \t) \t \t \t - > addFrom( '產品') \t \t \t - > leftJoin( '貨幣',「product.CurrencyId =貨幣。ID ') \t \t \t - > leftJoin(' UNITTYPE」, 'product.UnitTypeId = unittype.Id') \t \t \t - > ORDERBY( 'product.Name') \t \t \t - > getQuery() \t \t \t - > execute(); \t \t?>' –