[BNM] CakePHP problem with HABTM findAll() function
Ali
ali at nubz.com
Mon Mar 31 10:45:41 BST 2008
not knowing Cake very well but the error looks self-explanatory, it would
seem the parameter populating $conditions is SQL and your value
EventTypes.id is not matched with a valid column - maybe it should be
$conditions = array('EventType' => 1);
----- Original Message -----
From: "Alex Bovey" <alex at bovey.co.uk>
To: "Brighton New Media" <bnmlist at brightonnewmedia.org>;
<underscore at under-score.org.uk>
Sent: Sunday, March 30, 2008 3:23 PM
Subject: [BNM] CakePHP problem with HABTM findAll() function
> Hi all,
>
> I have a CakePHP (1.2) problem with a findAll() query with a HABTM
> relationship and I'm sure there must be a simple answer but I've had
> no luck with Google or elsewhere, so I'm hoping someone here can help!
>
> I have an Events model and an EventTypes model and a HABTM
> relationship defined. The relationship appears to be working
> correctly as I can add / edit Events and the link table
> (event_types_events) is updated correctly. I can also use
> $this->Event->findAll() and I can see that it is correctly fetching
> the associated EventTypes data.
>
> The problem comes when I try to find all Events of a particular
> EventType, something that should be straightforward. In my Events
> controller I am using:
>
> $this->Event->recursive = 1;
> $conditions = array('EventType.id' => 1);
> $this->Event->findAll($conditions);
>
> Basically I'm getting an SQL error:
>
> SQL Error: 1054: Unknown column 'EventType.id' in 'where clause'
>
> Looking at the query that's produced there are no joins to the EventTypes
> table.
>
> My models are pretty straightforward:
>
> class Event extends AppModel {
>
> var $name = 'Event';
> var $useTable = 'events';
>
> var $hasAndBelongsToMany = array(
> 'EventType' => array('className' => 'EventType',
> 'joinTable' => 'event_types_events',
> 'foreignKey' => 'event_id',
> 'associationForeignKey' => 'event_type_id',
> 'unique' => true,
> )
> );
>
> }
>
> class EventType extends AppModel {
>
> var $name = 'EventType';
> var $useTable = 'event_types';
>
> //The Associations below have been created with all possible keys,
> those that are not needed can be removed
> var $hasAndBelongsToMany = array(
> 'Event' => array('className' => 'Event',
> 'joinTable' => 'event_types_events',
> 'foreignKey' => 'event_type_id',
> 'associationForeignKey' => 'event_id',
> 'unique' => true,
> )
> );
>
> }
>
> Help!
>
> Thanks,
>
> Alex
>
> --
> Alex Bovey
> Web Developer | Alex Bovey Consultancy Ltd
> Registered in England & Wales no. 6471391
> alex at bovey.co.uk | t 0117 904 1753 | m 07828 649386 | f 0870 288 9533
> PHP | MySQL | AJAX | XHTML | CSS | Javascript | XML | W3C Accessibility
> --
>
> BNM Subscribe/Unsubscribe:
> http://www.brightonnewmedia.org/options/bnmlist
>
> Archive Search - http://icanhaz.com/bnmarchive
>
> BNM powered by Wessex Networks:
> http://www.wessexnetworks.com
More information about the BNMlist
mailing list. Powered by Wessex Networks