Okay. I’ll try to explain it more. That virtual field definition is most definitely the cause of the problem that you are having. When you added the new relation, or perhaps when you expanded
recursive, the virtual field started to get used in a way that it never had before.
When Cake tries to load the Referrer relation, it includes the virtual field definition in the query, because that’s how virtual fields work. But the way it was defined referenced the table as “User”, and in the new query that happens for the
hasMany, the users table is given a different name (aka alias), Referrer. That caused the “Unknown column” SQL error.
You removed the User part of the definition, but that also breaks things because now it doesn’t know which
first_name out of several that might apply in the tables referenced by a query somewhere. That’s the source of your “Ambiguous field” SQL error.
So, as I said way back when, one way to fix this is to change the virtual field definition so that it includes whatever alias is being used, instead of User. Unfortunately, I don’t know how to do that in Cake 2. I expect this sort of thing is part of why things have changed very dramatically in Cake 3. Maybe you can use
beforeFind in that table to change the definition of
virtualFields on every call? That might work, or it might just break something else.
Alternately, I can’t say, based on the code shown, why Referrer is coming into it at all. If you don’t need the Referrer relation to be included in the function you’re working on, then you might change from using
recursive = 2 (which will indiscriminately pull in all possible relations, including Referrer) to a more targeted