Search code examples
phpormpropelpropel2

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


I need to select specific set of columns from both the table using propel orm 2.0.0. The equivalent query is as below

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;

I am struggling where the necessary columns on both the tables have same name but need to join using different column.

Help me with the propel php code and also with proper reference site. The document at official site is not a good tutorial.


Solution

  • This should work, provided you have correctly defined your models.

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