Cakephp paginate displaying multiples of some records
This is my first time programming with CakePHP. I am using cakePHP 1.3.3. I've run into this issue that I can't seem to figure out what the problem is. My function is as follows:
function view_members(){
$data = $this->paginate('User',array('User.level <=' => '10'));
print_r($data);
}
At the beginning of my controller, paginate is set to:
var $paginate = array(
'limit' => 20,
'order' => array('User.id' => 'asc'),
'User' => array(
'fields' => array('User.id','User.level','User.name','User.status'),
'recursive' => 0
)
);
In my model, User is set up this way:
class User extends AppModel {
var $name = 'User';
var $hasOne = array(
'Users_detail' => array(
'className' => 'Users_detail',
'dependent' => true,
),
'Users_calendar' => array(
'className' => 'Users_calendar',
'dependent' => true,
),
);
var $hasMany = array(
'Users_transaction' => array(
'className' => 'Users_transaction',
'order' => 'Users_transaction.id ASC',
'dependent' => true,
),
'Users_notice' => array(
'className' => 'Users_notice',
'order' => 'Users_notice.id DESC',
'dependent' => true,
),
);
}
My problem is when I access that function, I am getting multiple instances of 1 record.
This is the Array that I get:
Array
(
[0] => Array
(
[User] => Array
(
[id] => 3
[level] => 5
[name] => Matthew
[status] => 1
)
)
[1] => Array
(
[User] => Array
(
[id] => 3
[level] => 5
[name] => Matthew
[status] => 1
开发者_JAVA技巧 )
)
[2] => Array
(
[User] => Array
(
[id] => 3
[level] => 5
[name] => Matthew
[status] => 1
)
)
[3] => Array
(
[User] => Array
(
[id] => 3
[level] => 5
[name] => Matthew
[status] => 1
)
)
[4] => Array
(
[User] => Array
(
[id] => 4
[level] => 5
[name] => Larry
[status] => 1
)
)
[5] => Array
(
[User] => Array
(
[id] => 5
[level] => 5
[name] => Brian
[status] => 1
)
)
);
In this example, Matthew appears 4 times in the Array even though there is only 1 record in the MySQL database. I've been trying to figure out why it's doing this but so far, I've not been successful. Has anyone experienced this before?
Without seeing the exact SQL Cake is executing it's a bit hard to find the exact answer to this, but my best guess is that it would have something to do with data existing in related tables. Does user Matthew have associated rows in one of the tables related to the User
table?
You're using recursive = 0
but this might not do what you expect (it actually does make joins to the other models it has an belongsTo
relationship with). If you don't want any joins with other tables, use recursive = -1
. See the documentation for recursive in the Cake manual to see how it works.
精彩评论