문제

I'm struggling with retreiving data in propel 1.6.7.

Here's the sample of query I want to achieve:

select count(1) as amount, p1.local_id FROM panel_data pd
         JOIN panel_data_has_code pp1 on (pd.panel_data_id = pp1.panel_data_id)
         JOIN panel_code p1 on (pp1.panel_code_id = p1.panel_code_id AND p1.type='equipment'  AND model_id = 'my_model_id')
         JOIN panel_data_has_code pp2 on (pd.panel_data_id = pp2.panel_data_id)
         JOIN panel_code p2 on (pp2.panel_code_id = p2.panel_code_id AND p2.type='model' AND p2.local_id='my_local_id') 
         GROUP BY p1.local_id

However whenever I try to construct proper criteria in Propel ORM, I'm having an issue - apparently propel always translate join alias to table name, whenever I try to use add join condition or use alias in select method. I've replaced multiple join condition with simple filterBy method (I always use inner joins, so effect will be the same), but I'm still having issue with retreiving groupped column (p1.local_id).

Here's the code I work with atm:

PanelDataQuery::create()
            ->select(array( 'p1.localId'))
            ->withColumn('count(1)', 'amount')
            ->usePanelDataHasCodeQuery('pp1')->usePanelCodeQuery('p1')->groupByLocalId()->filterByType($type)->endUse()->endUse()
            ->usePanelDataHasCodeQuery('pp2')->usePanelCodeQuery('p2')->filterByType('model')->filterByLocalId($model)->endUse()->endUse()
            ->find();

Above statement returns an error:

Unable to execute SELECT statement [SELECT count(1) AS amount, panel_code.LOCAL_ID AS \"p1.localId\" FROM panel_data INNER JOIN panel_data_has_code pp1 ON (panel_data.PANEL_DATA_ID=pp1.PANEL_DATA_ID) INNER JOIN panel_code p1 ON (pp1.PANEL_CODE_ID=p1.PANEL_CODE_ID) INNER JOIN panel_data_has_code pp2 ON (panel_data.PANEL_DATA_ID=pp2.PANEL_DATA_ID) INNER JOIN panel_code p2 ON (pp2.PANEL_CODE_ID=p2.PANEL_CODE_ID) WHERE p1.TYPE=:p1 AND p2.TYPE=:p2 AND p2.LOCAL_ID=:p3 GROUP BY p1.LOCAL_ID] [wrapped: SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]The multi-part identifier \"panel_code.LOCAL_ID\" could not be bound.]

Obviously, select clause causes the problem. Any ideas how to force propel to use table alias instead of translating it to table name?

Thank you in advance for any help.

도움이 되었습니까?

해결책

Found partial solution:

PanelDataQuery::create()
    ->withColumn('p1.local_id','localId')
    ->withColumn('count(1)', 'amount')
    ->select(array( 'localId','amount'))
    [...]

Seems to work fine. Altought I'd appreciate any other solutions. Still would be great how to deal with similar problem in addJoinCondition.

When i replace:

->filterByType('model')->filterByLocalId($model)

From the original query, with:

->addJoinCondition('p2', 'p2.localId = ?', $model)
->addJoinCondition('p2', "p2.type = 'model'");

I got similar issue as stated in error: propel translates p2.localId to panel_code.local_id which isn't correct (multiple panel_code table joins with aliases are included in query).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top