繁体   English   中英

如何在propel中选择表联接上的特定列?

[英]How do I select specific columns on table join in propel?

我需要使用propor orm 2.0.0从两个表中选择特定的列集。 等效查询如下

select b.name as brand_name, b.grade, d.name as dealer_name, d.number
from brand as b join dealer as d
on d.id = b.dealer_id;

我在两个表上必要的列具有相同名称但需要使用不同列进行联接的地方苦苦挣扎。

用适当的php代码以及适当的参考站点帮助我。 官方站点上的文档不是一个很好的教程。

只要正确定义了模型,这应该可以工作。

$rows = BrandQuery::create()
    ->select(['name', 'grade'])
    ->joinWith('dealer')
    ->withColumn('dealer.name', 'dealer_name')
    ->withColumn('dealer.number')
    ->find();

您需要使用ORM。 假设所有模型都正确设置:

<?php
$brands = BrandQuery::create()->find();
// $brands contains a collection of Brand objects
// one object for every row of the brand table
foreach($brands as $brand) {
    $dealers = $brand->getDealers();
    // $dealers contains a collection of Dealer objects for this brand
    // one object for every row of the dealers table for brand row
}

另一个(我认为更好)的方法是使用自定义SQL。

<?php
use Propel\Runtime\Propel;
$con = Propel::getWriteConnection(YOUR_DATABASE_NAME);
$sql = "select b.name as brand_name, b.grade, d.name as dealer_name, d.number from brand as b join dealer as d on d.id = b.dealer_id";
$stmt = $con->prepare($sql);
$stmt->execute();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM