user2398182
user2398182

Reputation: 11

Cakephp: Associations not working

I want to do a simple cakephp association program but it's not working. I have two database tables: users and sec_datas. When i run this program it just shows the result of first row of users table, not the result of both tables which have same sec_id value.

Controller code:

<?php
class UsersController extends AppController
{
    public function  index()
    {
        $this->autoRender = FALSE;
        $this->loadModel('User');
        $storeDivisions = $this->User->find();
        echo "<pre>";
        print_r($storeDivisions);
        echo "</pre>";
    }
}

Model code:

<?php
class User extends AppModel {
    public $useTable='users';

    public $hasOne = array(
        'Sec_data' => array(
            'ClassName' => 'Sec_data',
            'Conditions' => array('User.sec_id=Sec_data.sec_id'),
            'Dependent' => false
        )
    );  
}
?>

Upvotes: 1

Views: 1421

Answers (3)

Sandeep Kapil
Sandeep Kapil

Reputation: 992

Study model association in cake php . Here is very good explanation http://www.phpsupercoder.com/model-association-cake-php

Upvotes: 0

lethal-guitar
lethal-guitar

Reputation: 4519

There are a couple of things going wrong here. You should read up on CakePHP's conventions.

  • Your associated model class should be called SecData, then it will automatically map to the sec_datas table.
  • No need to use loadModel.
  • No need to specify conditions. Set the correct recursive level instead.
  • For hasOne, only one table has a foreign key. By default, the other Model has it ("User hasOne SecData" -> SecData has foreign key). So in your example, you should remove the column sec_id from User, and add a user_id column to your SecData Model.

See also: CakePHP Book on associations

Updated Controller code:

<?php
class UsersController extends AppController {
    public $uses = array('User'); // Do this instead of loadModel

    public function  index() {
        $this->autoRender = FALSE;

        $this->User->recursive = 1; // Make User load associated records
        $storeDivisions = $this->User->find('all');
        pr($storeDivisions); // pr() is a Cake shorthand for print_r wrapped in <pre>
    }
}

For your model:

<?php
class User extends AppModel {
    // Unecessary. Convention is to use lowercase classname + 's', which
    // already gives us 'users'
    // public $useTable='users'; 

    public $hasOne = array(
        'SecData' => array( // Model class Sec_Data must be renamed accordingly
            'dependent' => false
        )
    );  
}
?>

Upvotes: 0

arilia
arilia

Reputation: 9398

If the primary key of User model is id then cake try to associate the foreign key in Sec_data with that key regardless the conditions you set.

First of all you should do domething like

public $hasOne = array(
    'Sec_data' => array(
        'ClassName' => 'Sec_data',
        'ForeignKey' => 'sec_id',
        'Dependent' => false
    )
);

But it would work only if sec_id is related to User.id

if you want Sec_data.sec_id related to User.sec_id (and User.sec_id is different from User.id) then you have to join the tables manually

edit: see comments

Upvotes: 1

Related Questions