11.02 php saved
lloydhome
Tags add more
633 recursive query  
Note
RE: Trac#633: recursive association query
  1. suppose models User, Subarea, Area, Deal
  2.  
  3.  Area hasMany Subarea
  4.  Subarea hasOne Area
  5.  User hasOne  Subarea
  6.  User hasMany Deal
  7.  Deal hasOne  User
  8.  Deal hasOne  Subarea
  9.  
  10.   $this->User->findAll("Deal.Subarea.Area.name = 'SouthEast'")
  11. is different than
  12.   $this->User->findAll("Subarea.Area.name = 'SouthEast'")
  13. so doing
  14.   $this->User->findAll("Area.name = 'SouthEast'")
  15. is ambiguous (unless the association names were different but unlikely).
  16.  
  17. But fully naming the path this way has the problem of naming in the query unless it is fully escaped in the query.  The dbo should find these and escape properly.
  18.  
  19. Then do you return all of the fields from all of the joins?  We should be able to be smart enough to see what joins are needed ignoring the recursion.  So recursion is set to '1' but Subarea and Area needed to be joined but this requires a bit of parsing not previously performed.  A recursive descent parsing is required anyway to link up all the joins.
  20.  
  21. The list of linked tables needs to be maintained so they are not requeried when running down the recursive levels to get data if we return the fields on the first query.
  22.  
  23. Now if we do all this work to join and return fields and place fields in the data array based on recusion depth, why not do this in many other cases where no query is specified?  Currently, $this->User->findAll() with recursive=2 invloves many separate calls that we might be able eliminate if a join is used or maybe it reduces the number by joining some and then separate queries for others.
  24.  
Parsed in 0.054 seconds, using GeSHi 1.0.7.14

Modify this Paste