问题
I'd like some help from the community, if possible.
We have a project at work, and time has come for refactoring. We are using PHP 5 and Propel 1.6 as ORM layer. The idea, actually, is pretty simple: we are trying to obtain all relevant information at the same time.
However, this information is not always directly related to the main class (or, in other words, to the main table). For example:
return (
$this->leftJoin( "IES.Pessoa mant" )
->leftJoin( "mant.Papel subpapel" )
->where( "subpapel.tipo = ?", Tipo_papel::IES )
->leftJoin( "subpapel.RelacionamentoRelatedByIdSubPapel relm" )
->where( "relm.tipo = ?", Tipo_relacionamento::MANTENEDORA_IES )
->leftJoin( "relm.PapelRelatedByIdSuperPapel superpapel" )
->leftJoinWith( "superpapel.Pessoa iesm" )
->where( 'superpapel.tipo = ?', Tipo_papel::MANTENEDORA_IES )
);
This is the code of a function in a ModelCriteria from our model layer. The idea is to obtain 'iesm' related to one 'IES'.
But, here we have a problem. It happens that 'IES' is already related to one entity 'Pessoa'. So, when this code is applied, this object will be lost, giving space to the entity 'Pessoa' related to 'iesm'.
So the basic question is: I would like to set the property 'iesm' inside 'IES' object, and not Pessoa, which is set because of Propel mapping. That being said, how can I do that? Is it even possible? I'd like to set 'iesm' based on the alias created on leftJoinWith.
And another question, that showed immediately after this: if there is no direct relation in the database, how can I set this object, using the same idea? Trying to explain better: this 'iesm' is actually an object of type 'Pessoa_juridica'. And 'Pessoa_juridica' is not directly related to 'IES'. How could this 'Pessoa_juridica' be set inside 'IES' object?
I don't even know if this is the best way of using it, so any other ideas are welcome. If you want further explanation, just let me know.
回答1:
Ok so i think i undestand what your are dealing with: You what to optimize database connections but found that, because you need to join two different unrelated table with another one that joins them in some way.
My advise in this situation is:
Try using UNION with a custom query. So create two criterions and use UNION to merge results, for this to work the selected fields en both tables should be the same in size and type. I'm no sure how to do this using the Query objets, but i'm quite sure it can be accomplished using Peer classes. Check this link UNION query with Propel ORM
Dont hidrate all objets. Sometimes its far more efficient and quite more clear to use statements (like doSelectStmt()) and fetching information as arrays from separate database queries, manipulate them and then only hydrate the objects you are going to use.
Remember that symfoyn also has a cache (APC for example), that's always a good idea when dealing with really big tables, complex information and repeated information.
来源:https://stackoverflow.com/questions/8166377/can-someone-tell-me-how-to-obtain-indirectly-related-classes-using-propel-modelc