Reputation: 336
I'm a cakephp beginner with not much coding experiences. I am trying to assign empty array on the '$this->paginate()' function conditionally but it is not working. Below is my partial code in LogsController:
if ($privacyVisible['Title']['privacy'] == 1) {
$this->Log->recursive = 0;
$this->paginate = array(
'limit' => 10,
'order' => array(
'Log.id' => 'desc'
), 'conditions' => array('Title.slug' => $id));
$this->set('logs',$this->paginate());
} else {
$this->Log->recursive = 0;
$this->paginate = array(
'limit' => 10,
'order' => array(
'Log.id' => 'desc'
), 'conditions' => array('1' => '2'));
$this->set('logs',$this->paginate()); //here it should be empty
}
But in my view after doing debug($logs); in applying the else condition, i.e. privacy = zero, I am getting all values returned like below:
app\View\Logs\index.ctp (line 10)
Array
(
[0] => Array
(
[Log] => Array
(
[id] => 114
[title_id] => 16
[body] => hi!
[time] => 2011-11-15 21:19:10
)
[Title] => Array
(
[id] =>
[user_id] =>
[logname] =>
[slug] =>
[privacy] =>
)
)
[1] => Array
(
[Log] => Array
(
[id] => 113
[title_id] => 14
[body] => hey!
[time] => 2011-11-15 20:52:52
)
[Title] => Array
(
[id] => 14
[user_id] => 7
[logname] => college
[slug] => college
[privacy] => 1
)
)
[2] => Array
(
[Log] => Array
(
[id] => 112
[title_id] => 12
[body] => hey hi.
[time] => 2011-11-15 20:51:19
)
[Title] => Array
(
[id] =>
[user_id] =>
[logname] =>
[slug] =>
[privacy] =>
)
)
[3] => Array
(
[Log] => Array
(
[id] => 111
[title_id] => 12
[body] => that will be great.
[time] => 2011-11-15 20:51:13
)
[Title] => Array
(
[id] =>
[user_id] =>
[logname] =>
[slug] =>
[privacy] =>
)
)
)
If it was an empty array, I was willing to test it on if(empty($logs)) and show that "You are not allowed." But seems difficult for now. Any guess, what is wrong in here?
Upvotes: 0
Views: 1491
Reputation: 4411
I don't fully comprehend what you're trying to accomplish here and I have a feeling this can be done another, more elegant way. Have you considered using the Containable behaviour? Containable is often used to filter out unwanted results, including associated content.
To attempt to answer your question though, why not pass an empty array when using $this->set();
?
Calling $this->paginate()
executes the necessary find operations to display data. You could pass an empty array as the logs variable in the second part of your if/else statement, like so:
$this->set('logs',array());
The PaginationHelper
might break on empty arrays, but I'm not sure of that, so you might want to check for empty values in the relevant view.
Upvotes: 1
Reputation: 4313
$this->set('logs',$this->paginate())
Should be:
$this->set('logs',$this->paginate('Log'));
Alternatively, you could rewrite it as:
$logs = array();
if ($privacyVisible['Title']['privacy'] == 1) {
$this->paginate = array(...);
$logs = $this->paginate('Log');
}
$this->set(compact('logs'));
This way, you are only performing a database query when you really want to fetch back your data.
Upvotes: 1