Thursday, September 18, 2014

belongsTo proper usage to avoid 1052 ambiguity?

I am in the midst of migrating a large codebase from cake1.2 to the latest cake1.3 - all has been proceeding nicely until I hit an issue with one of the models and how 
find('all) is generating the SQL request.

The find('all') request is in the comments class controller and requests 3 comment records in their entirety. Each comment SQL record has an angler_id and an author_id foreign and the below stub is from the comments model.


class Comment extends AppModel {
 
var $name = 'Comment';
 
var $belongsTo = array(
 
Angler' => array( 'className' => 'Angler' ),
 Author'
=> array( 'className' => 'Angler' ),
 
);
}


The angler records have a field called 'favorite lure' among numerous other fields. Author and Angler both refer to angler records. This worked perfectly fine in the 1.2 code, but for some reason 1.3 is generating the following SQL request in which the 'favorite lure' field (only that one) is ambiguous and I get a 1052 error code. Note none of the other angler fields are complained about as ambiguous.

Query: SELECT `Comment`.`id`, `Comment`.`catch_id`, `Comment`.`angler_id`, `Comment`.`text`, `Comment`.`datetime`, `Comment`.`author_id`, `Comment`.`private`, `Angler`.`id`, `Angler`.`username`, `Angler`.`password`, `Angler`.`email`, `Angler`.`active`, `Angler`.`autologin_key`, `Angler`.`role`, `Angler`.`first_name`, `Angler`.`last_name`, `Angler`.`address`, `Angler`.`city`, `Angler`.`state`, `Angler`.`zip`, `Angler`.`phone`, `Angler`.`boat`, `favorite lure`, `Angler`.`created`, `Angler`.`ip`, `Angler`.`aoty_rank`, `Angler`.`aoty_points`, `Angler`.`aoty_events`, `Author`.`id`, `Author`.`username`, `Author`.`password`, `Author`.`email`, `Author`.`active`, `Author`.`autologin_key`, `Author`.`role`, `Author`.`first_name`, `Author`.`last_name`, `Author`.`address`, `Author`.`city`, `Author`.`state`, `Author`.`zip`, `Author`.`phone`, `Author`.`boat`, `Author`.`created`, `Author`.`ip`, `Author`.`aoty_rank`, `Author`.`aoty_points`, `Author`.`aoty_events` FROM `comments` AS `Comment` LEFT JOIN `anglers` AS `Angler` ON (`Comment`.`angler_id` = `Angler`.`id`) LEFT JOIN `anglers` AS `Author` ON (`Comment`.`author_id` = `Author`.`id`)  WHERE `Comment`.`private` = 0   ORDER BY `Comment`.`datetime` DESC  LIMIT 3

Both Author and Angler have all the same fields but for some reason things go awry with the 'favorite lure' field. Any advice on solving this is greatly appreciated.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: