+4 votes
1.4k views
in Programming by Expert (5.1k points)
edited by
how to use multiple JOINS in cake php?

1 Answer

+2 votes
by Expert (6.4k points)

An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.

Some of jines are following :

SQL Inner Join

SQL Left Join

SQL Right Join

SQL Full Join

SQL Union

This is a eg of join:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

Notice that the "CustomerID" column in the "Orders" table refers to the customer in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.

by Expert (5.1k points)
tell me how to use the third table..!!!
as per as the cake php syntax!!
by Expert (6.4k points)
If we talk about simple inner join of three tables so you can use this :

select * from tableA a
    inner join
    tableB b
    on a.common = b.common
    inner join
    TableC c
    on b.common = c.common

And for cake php i am giving you code see it you will get the point :
$this->bindModel(array(
        'belongsTo' => array(
            'Contact' => array(
                'className' => 'Contact',
                'foreignKey' => false,
                'conditions' => array(
                    'Message.user_id = Contact.user_id',
                    '`Message`.`mobileNo` IN (`Contact`.`mobileNo`,`Contact`.`workNo`,`Contact`.`homeNo`,`Contact`.`other`)'),
                'order'=>'Message.idTextMessage DESC',
            )
        )
    ), false);
    return $this->find('all', array('conditions' => array('Message.User_id' => $userid),
        'contain' => array('Contact' ),
        'fields' => array('Message.mobileNo',
            'Contact.mobileNo',
            'Contact.workNo',
            'Contact.homeNo',
            'Contact.other',
            'Contact.name',
            'Message.dateTime',
            'Message.type',
            'Message.body'),
        'group' => 'Message.mobileNo',
        'limit' => 6));
}

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated