Tuesday, March 19, 2013

Re: HABTM Inner Join with 3 models


Am Dienstag, 19. März 2013 00:42:28 UTC+1 schrieb cricket:
On Mon, Mar 18, 2013 at 12:40 PM, Jerome Walitzek
<jerome....@t-online.de> wrote:
> Hi there,
>
> i have a problem with INNER JOIN.
>
> Models
> Job
> User
> UsersEvent

What are the associations between models? Do you also have an Event
model? I ask because that last one looks like a join model between
User & Event. (Although for Cake the table would conventionally be
named events_users -- models are alphabetical.)

Of course i gave an Event Model to. Sorry i forgot.
Here are my associations between the models

Event.php
public $belongsTo = array(
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Dispatcher' => array(
'className' => 'User',
'foreignKey' => 'dispatcher_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Client' => array(
'className' => 'User',
'foreignKey' => 'client_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);

public $hasMany = array(
'Job' => array(
'className' => 'Job',
'foreignKey' => 'event_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Location' => array(
'className' => 'Location',
'foreignKey' => 'city_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);

public $hasAndBelongsToMany = array(
'Location' => array(
'className' => 'Location',
'joinTable' => 'locations_events',
'foreignKey' => 'event_id',
'associationForeignKey' => 'location_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'User' => array(
'className' => 'User',
'joinTable' => 'users_events',
'foreignKey' => 'event_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
);

Job.php
public $belongsTo = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'event_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Driver' => array(
'className' => 'User',
'foreignKey' => 'driver_id',
'conditions' => '',
'fields' => '',
'order' => '',
),
'UsersEvent' => array(
'className' => 'UsersEvent',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => '',
)
);

User.php
public $hasMany = array(
'Event' => array(
'className' => 'Event',
'foreignKey' => 'driver_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Job' => array(
'className' => 'Job',
'foreignKey' => 'driver_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'UsersEvent' => array(
'className' => 'UsersEvent',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
);

UsersEvent.php
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Event' => array(
'className' => 'Event',
'foreignKey' => 'event_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Job' => array(
'className' => 'Job',
'foreignKey' => 'event_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);

Any ideas how i could do it on CakeWay ?

Nice Greetings
Jerome

 

You can use joins for hasOne and belongsTo assoc. One workaround is to
unbind the normal assoc. and temporarily bind on another.

> In my Job View i would like to get all users which are assigned to an event.
> I only get successfully the IDs from UsersEvent but not the names from the
> User Model.
>
> In principle I would like to do following:
> SELECT A.`id`,(CONCAT(A.vorname, ' ',A.nachname)) AS name FROM users AS A
> INNER JOIN users_events AS U ON A.id = U.user_id WHERE U.event_id = 2
>
> But how i write it cake like ?
>
> When i try it with
> $this->Job->query("SELECT A.`id`,(CONCAT(A.vorname, ' ',A.nachname)) AS name
> FROM users AS A INNER JOIN users_events AS U ON A.id = U.user_id WHERE
> U.event_id = 2");
> i get the complete array in my select field and can´t select a name.
>
> Thanks for help
>
> --
> 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+u...@googlegroups.com.
> To post to this group, send email to cake...@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: