Retrieving Data From two tables that are associated with a forign key in CakePhp

放肆的年华 提交于 2019-12-08 07:33:42

问题


I have two tables named login and userDetail

                        Login

                        login_id
                        uname
                        pswd
                        userdetail_id

and

                       userdetails

                          userdetail_id
                          name
                          address
                          email

the login table contain userdetails_id in the userDetail table. i want to get all data from Login table and userDetail table and save it to a variable if anyone knows, please answer me......


回答1:


First of all your table structure must be as below.

logins Table.
  Id auto_increment
  username
  password
userDetails Table.
  Id auto_increment
  user_id
  name
  address
  etc...

Now model for each table would be.

Login

<?php
class Login extends AppModel
{
    var $name = 'User';

    var $hasMany = array
    (
        'UserDetail' => array
        (
            'className' => 'UserDetail',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
}
?>

UserDetail

<?php
class UserDetail extends AppModel
{
    var $name = 'UserDetail';

    var $belongsTo = array
    (
        'User' => array
        (
            'className' => 'User',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => ''
        )
}
?>

And finally in controller where you need to fetch login detail.

$login_detail = $this->Login->find('all');

You will see userDetail table records in resulting $login_detail. use

pr($login_detail);
in controller to see it in action.

Cheers. Feel Free to ask.




回答2:


Make sure ContainableBehavior has been enabled. After that you can use following query:

$login = $this->Login->find('first', array(
    'contain' => array(
        'Userdetail.userdetail_id'
        'Userdetail.name',
        'Userdetail.address',
        'Userdetail.email'
    ),
    'fields' => array(
        'Login.login_id'
        'Login.uname',
        'Login.pswd'
    ),
    'conditions' => array(
        'Login.login_id' => 1
    )
));



回答3:


The query for this task would be:

SELECT Login.*, name,address,email
        FROM Login JOIN userdetails
        ON Login.userdetail_id=userdetails.userdetail_id

The results of this query could be saved to variables by looping in cakephp.



来源:https://stackoverflow.com/questions/14517134/retrieving-data-from-two-tables-that-are-associated-with-a-forign-key-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!