2
votes

I have 2 tables, Militia and Injunctions. Militia can have many injunctions and injunctions only belong to one militia. I set those up in the models but when i call a find all for militia the injunctions aren't pulled out.

Militia model

class Militia extends AppModel {
    public $belongsTo = array(
        'User' => array(
            'className'    => 'User',
            'foreignKey'   => 'user_id'
        )
    );

    public $hasMany = array(
        'Injunction' => array(
            'className'     => 'Injunction',
            'foreignKey'    => 'militia_id'
            //'conditions'    => array("not" => array('Injunction.removed' => null))
        )
    );
}

Injunctions model

class Injunction extends AppModel {
    public $belongsTo = array(
        'Militia' => array(
            'className'    => 'Militia',
            'foreignKey'   => 'militia_id'
        )
    );
}

and the query getting the militia members

$user = $this->Session->read("User");

        $militias = $this->Militia->find('all',
            array(
                'conditions'=>array(
                    "Militia.user_id"   => $user['User']['id'],
                    "Militia.deleted"   => 0
                )
            )
        );

output

    /app/Controller/UsersController.php (line 41)
array(
    (int) 0 => array(
        'Militia' => array(
            'id' => '26',
            'first_name' => 'Chris',
            'last_name' => 'Morris',
            'created' => '2013-02-11 13:45:24',
            'user_id' => '2',
            'status' => '1',
            'deleted' => '0'
        )
    ),
    (int) 1 => array(
        'Militia' => array(
            'id' => '31',
            'first_name' => 'John',
            'last_name' => 'Smith',
            'created' => '2013-02-11 14:03:50',
            'user_id' => '2',
            'status' => '0',
            'deleted' => '0'
        )
    ),
    (int) 2 => array(
        'Militia' => array(
            'id' => '32',
            'first_name' => 'test',
            'last_name' => 'user',
            'created' => '2013-02-11 14:21:38',
            'user_id' => '2',
            'status' => '0',
            'deleted' => '0'
        )
    ),
    (int) 3 => array(
        'Militia' => array(
            'id' => '33',
            'first_name' => 'test',
            'last_name' => 'user',
            'created' => '2013-02-11 14:24:02',
            'user_id' => '2',
            'status' => '1',
            'deleted' => '0'
        )
    )
)

I've done the same thing before on other projects but for some reason this one time it's not pulling out the associated data. It's probably something stupid like a typo but I've been looking and testing and can't find anything wrong.

1
Which is the query you are trying to execute to retrieve the data? - Alvaro
Show the find statement - 93196.93
$user = $this->Session->read("User"); $militias = $this->Militia->find('all', array( 'conditions'=>array( "Militia.user_id" => $user['User']['id'], "Militia.deleted" => 0 ) ) ); - Chris Morris
I would have thought that would work. Have you tried using the Containable behavior? - 93196.93
Militia should be called MilitiasController and Militium. Also try doing a find without conditions. It could be possible that the Militium you are trying to access does not contain any Injunctions. In that case it could be empty as well. What version of Cake are you using? I assume 2.3.0? - Jelmer

1 Answers

0
votes
  1. Check ORM (hasOne, hasMany, belongsTo, hasAndBelongsToMany) definition,
  2. Check if foreign key ids are set right
  3. Check if there are data's in the table and its related table.
  4. Check the results of find without condition
  5. Check to see if recursive level is greater than 0
  6. To rule out your old cached query issue, set debug level higher to 0
  7. Look out in the bottom section of sql dump to see what query is being fired, if it feels ok try same query with SQL away from Cake/Php.