On Tue, Jul 20, 2010 at 4:33 AM, Andrew <[email protected]> wrote:
> Hi all,
>
> I am having a problem getting data from a model connected to a HABTM
>
> If I have two models, the first being hasMany to the second, then if I
> do a find on the second, I can normally access the first models
> fields, because a LEFT join is done? However, if the model is
> HasAndBelongsToMany then this is not the case??
>
> So, my problem now ---> I have four tables, one called Categories, one
> called Cities and the bridging table Categories_Cities
> And the fourth table is called Items .... where Categories HasMany
> Items.
> But when I do a find on Categories_Cities, it is not finding my Items
> table, like it would if it was a HasMany relationship.

You should run the find() on either Category or City, not the join model.


> Cities ----> Categories_City <----- Categories <--- Items
>
> Is that clear?
>
> The last part of my email contains my models:
>
> class Item extends AppModel
> {
>    var $name = 'item';
>
>
>                var $hasOne = array(
>        'Vote' => array(
>                'className' => 'Vote',
>                'foreignKey' => 'item_id'
>        )
>    );


You need to associate Item with Category.


> class City extends AppModel
> {
>    var $name = 'city';
>
>   var $hasMany = array(
>        'Item' => array(
>                'className' => 'Item',
>                'foreignKey' => 'city_id'
>        )
>    );


You showed, above, that Item should be associated with Category, not City.


>    var $hasAndBelongsToMany = array(
>        'Category' => array(
>                'className' => 'Category',
>                'foreignKey' => 'city_id'
>        )
>    );
>
>
>
>
> class Category extends AppModel
> {
>    var $name = 'category';
>
>    var $hasMany = array(
>        'Item' => array(
>                'className' => 'Item',
>                'foreignKey' => 'category_id'
>        )
>    );





>                var $hasAndBelongsToMany = array(
>        'City' => array(
>                'className' => 'City',
>                'foreignKey' => 'category_id'
>        )
>    );
>
>
>
>
> class CategoryCity extends AppModel
> {
>        var $name = 'CategoryCity';
>        var $useTable = 'categories_cities';
>
>        var $belongsTo = array
>        (
>                        'Category' => array
>                        (
>                                 'className'  => 'Category',
>                                 'foreignKey' => 'id'
>                        ),
>                        'City' => array
>                        (
>                                 'className'  => 'City',
>                                 'foreignKey' => 'id'
>                        )
>        );
>
> }

If the rest of your associations are correctly set up there shouldn't
be a need to explicitly create this model as Cake will do it
"virtually". There are cases where doing this is necessarry (or simply
helps) but I don't think you need it in this case.

Another thing you could look into (AFTER you have the associations
corrected) is ContainableBehavior, which makes selecting associated
data very easy.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to