質問

I have a database with a EAV structure, having this tables :

Entity (id, name)
Attribute (id, name)
AttributeValue(fk_id_entity, fk_id_attribute, value)

What I want to do is translate this following query in Propel:

SELECT entity.name, avx.value, avy.value
FROM Entity entity
INNER JOIN AttributeValue avx ON (avx.fk_id_entity = entity.id 
AND avx.name = 'X')
INNER JOIN AttributeValue avy ON (avy.fk_id_entity = entity.id 
AND avy.name = 'Y')

Any idea how could I do this in Popel? Thank you!

LE: The ideea is that each entity has two attributes X and Y, and I would like to display a list of all entities along with their X and Y attributes.

役に立ちましたか?

解決 2

What it actually worked is the following:

$result = Entity_EntityQuery::create()
->withColumn('alias1.value', 'xvalue')
->withColumn('alias2.value', 'yvalue')
->innerjoinValue('alias1')
->useValueQuery('alias1')
->filterbyFkAttribute('x attribute id')
->endUse()
->innerjoinValue('alias1')
->useValueQuery('alias1')
->filterbyFkAttribute('y attribute id')
->endUse()
...//conditions
->find()

Thanks everybody for viewing and replying!

他のヒント

So here's one way to just replicate what you have in the SQL, but as I mention in my comment, there may be a more efficient way to do it. If you provide an answer to my comment question above I'll see if I can improve this answer.

First, you will need a cross reference table in your schema. I'll assume that you do, and that it is called AttributeValue. Then you add your joins to the EntityQuery and add additional conditions.

Unfortunately, it looks like there may be a bug in Propel that will prevent the code below from producing the proper SQL.

$results = EntityQuery::create()
  ->join('Entity.AttributeValue avx')
  ->join('avx.Attribute xa')
  ->addJoinCondition('xa', 'xa.name = ?', 'X')
  ->join('Entity.AttributeValue avy')
  ->join('avy.Attribute ya')
  ->addJoinCondition('ya', 'ya.name = ?', 'Y')
  ->select(array('entity.name', 'avx.value', 'avy.value'));

This should produce the following SQL:

SELECT entity.name AS 'entity.name', 
       avx.value AS 'avx.value', 
       avy.value AS 'avy.value'
  FROM `entity` 
    INNER JOIN `attribute_value` `avx` ON (entity.id = avx.entity_id) 
    INNER JOIN `attribute` `ax` ON (avx.attribute_id = ax.id AND ax.name =  'X') 
    INNER JOIN `attribute_value` `avy` ON (entity.id = avy.entity_id) 
    INNER JOIN `attribute` `ay` ON (avy.attribute_id = ay.id AND ay.name =  'Y') 

Unfortunately, this is produced instead:

SELECT entity.name AS 'entity.name', 
       aatribute_value.value AS 'avx.value', 
       aatribute_value.value AS 'avx.value'
  FROM `entity` 
    INNER JOIN `attribute_value` `avx` ON (entity.id = avx.entity_id) 
    INNER JOIN `attribute` `ax` ON (avx.attribute_id = ax.id AND attribute.name =  'X') 
    INNER JOIN `attribute_value` `avy` ON (entity.id = avy.entity_id) 
    INNER JOIN `attribute` `ay` ON (avy.attribute_id = ay.id AND attribute.name =  'Y') 

(The problem is the SELECT line and the INNER JOIN on attribute which uses attribute.name = 'X' instead of the proper ax.name = 'X'.)

So, you could run the correct SQL using a manual query, or wait for the bug to be fixed (I'll try to write up an issue on guthub and work on a fix.)

Sorry I couldn't be of more help.

Information gathered from the Propel documentation on joining tables.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top