Question

I have two tables, namely 'Customer' and 'Event'. The relations between these two are as follows:

Event

var $belongsTo = array(
         ...
    'Customer'=>array(
        'className' => 'Customer',
        'foreignKey' => 'customer_id'
    )
    );

Customer

var $hasMany = array(
    'Event' => array(
        'className' => 'Event',
        'foreignKey' => 'customer_id',
        'dependent' => false,
    )
);

Each and every customer record holds a company id as well as currently logged in user. What I would like to achieve is to get all of the events for a day, related only to a particular company, but that is of course not working since the event table does not hold company id. Maybe the following find call will help to understand the issue better:

$conditions = array('company_id'=>CakeSession::read("Auth.User.company_id"), 'date'=>date("Y-m-d", $tomorrow));
$tomorrowsEvents = $this->find('all', array(
                                'conditions'=>$conditions, 'contain'=>array('User', 'Customer')));

Moreover, a customer belongs to a company and a company has many customers, just as follows:

Customer

var $belongsTo = array(
        'Company' => array(
        'className' => 'Company',
        'foreignKey' => 'company_id',
        'dependent' => false,
    ),
);

Company

    var $hasMany = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'company_id',
        'dependent' => false
    ),
    'Customer'=>array(
        'className' => 'Customer',
        'foreignKey' => 'company_id',
        'dependent' => false
    )
);

Any help is much appreciated.

Was it helpful?

Solution

I would actually do this query "backwards" by doing it from Customer, and using the containable behavior to contain the related Events:

$this->Customer->find('all', 
     array(
       'conditions' => array(
           'company_id' => CakeSession::read("Auth.User.company_id")
        ), 
       'contain' => array(
             'Event' => array(
                   'conditions' => array(
                         'Event.date' => date("Y-m-d", $tomorrow)
                    )
             )
        )
  ));

OTHER TIPS

In your case it seems that Events have many companies and companies have many events. This is ideal situation for hasAndBelongsToMany relationship. However, you have to focus on the className and joinTable properties to handle the non convenient naming of the join table customers.

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

you need to rethink the problem, because a user can be (customer or company) then they have a group or they are in a type of user. then a user belongs to a group or user type, this group can be a client or company .

users belongsto a group group has many user

users hasmany events events has many users

You can tell if is customer or company is the type of group that owns

table___group

id ____ category ____ subcategory

1 ____ customer ____ example

2 ____ company ____ example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top