I can't make element caching work:
echo $this->element('categorytree', array(
'cache' => array(
'key'=>'categorytree-cache','time' => '+1 hour'
)
))
I checked core.php:
// In development mode, caches should expire quickly.
$duration = '+999 days';
/*if (Configure::read('debug') >= 1) {
$duration = '+10 seconds';
}*/
And commented as you see - to prevent short caching in debug mode...
But still when I refresh page the SQL query that cached element does by requestAction() is shown... So no caching... why?
I may be wrong here, but I think the correct call would be:
echo $this->element('categorytree', array(), array(
'cache' => array(
'key'=>'categorytree-cache','time' => '+1 hour'
)
))
Notice the second argument is element params while the third is element options (what you need here).
You can see the function signature here.
Related
I have this query
$orderStates = OrderState::listsTranslations( 'states' )->pluck( 'states', 'id' )->toArray();
which will output something like
array:3 [▼
1 => "waiting"
2 => "agreed"
3 => "canceled"
]
I need to skip the first one to be something like
array:3 [▼
2 => "agreed"
3 => "canceled"
]
How this can be done please?
There is multiple ways you can achieve this. One that doesn't seem very consistent to me would be to use skip(1) (this expects the query to sort by id and that always the entry with id = 1 needs to be skipped):
$orderStates = OrderState::listsTranslations( 'states' )
->skip(1)
->pluck( 'states', 'id' )
->toArray();
An alternative might be to use whereNotIn() to exclude specific ids:
$orderStates = OrderState::listsTranslations( 'states' )
->whereNotIn('id', [1])
->pluck( 'states', 'id' )
->toArray();
You could also use where('id', '!=', 1) instead of whereNotIn('id', [1]) or skip(1), but personally I think that whereNotIn offers the most extensibility for the future.
First thanks for Namoshek guide but skip(1) didn't work with this but slice(1) did
Here is the last query
$orderStates = OrderState::listsTranslations( 'states' )
->pluck( 'states', 'id' )
->slice( 1 )
->toArray();
Worked fine.
I am trying to calculate distance between two geographical locations. one location is entered by user.
User entered location is calculated by model and is stored in the variable.
$ulatitude=$userLocation['Location']['latitude'];
$ulongitude=$userLocation['Location']['longitude'];
I have used below code to calculate the distance.
$this->User->virtualFields['lowestDistance'] = 'round(3959 * acos(cos(radians($ulatitude)) * cos(radians(Profile.latitude )) * cos(radians(Profile.longitude) - radians($ulongitude)) + sin(radians($ulatitude)) * sin(radians(Profile.latitude)))) ';
$order = "User.lowestDistance";
$options = array(
'fields' => '
User.*, Profile.*,
round(3959 * acos(cos(radians($ulatitude)) * cos(radians(Profile.latitude )) * cos(radians(Profile.longitude) - radians($ulongitude)) + sin(radians($ulatitude)) * sin(radians(Profile.latitude)))) AS lowestDistance,
',
'order' => array($order =>'asc'),
);
Above code throws error : unknown column $ulatitude.
Is there any other way of doing this ?
Single quotes are not interpreted
Consider:
<?php
$foo = "A VARIABLE";
echo 'foo is $foo' . "\n";
echo "foo is $foo" . "\n";
This would output:
-> php example.php
foo is $foo
foo is A VARIABLE
In the question everything is in single quotes, they are literal strings.
Fields should be an array
Also note that this:
'fields' => 'User.*, Profile.*, ...
Is normally an array, which makes more sense when there's multiple values:
'fields' => [
'User.*',
'Profile.*',
...
]
But since you're declaring all fields plus a virtual field, it'd be included in a find call anyway - just remove the fields from the find parameters:
$this->User->virtualFields['lowestDistance'] = "round(...";
$options = [
//'fields' => [], Not necessary
'order' => [$order =>'asc']
];
$withLowestDistance = $this->User->find('all', $options);
Below cakephp query returns an empty array result but if I use only $max_results it works.
I want to search for a record from two limits (0,10).
This doesn't work.
$from = 0;
$max_results = 10;
$this->Model->find('all',array(
'conditions'=>$condition,
"order"=>'Model.id DESC',
'limit'=>"$from,$max_results"
));
You need to use offset options-
$from = 0;
$max_results = 10;
$this->Model->find('all',array(
'conditions'=>$condition,
"order"=>'Model.id DESC',
'limit'=>"$max_results",
'offset' => "$from"
));
Hi all I have a dropdown box on a view. I have a find statement that returns a list of templates, when I debug the find, it prints out the correct list of template.id and template.name however when I submit the form it carries across the number it is in the list for example if I select the 5th template in a list it saves the template_id = 5 NOT the actual template id number.
//Retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
//conditions for template arrays
$conditions=
array('AND' => array(
array('Template.account_id' => $accountid),
array('Template.active' => 1)
));
//An array of all Templates belonging to the current User's Account
$templates=$this->Template->find('list', array(
'conditions' => $conditions));
when I debug $templates I get this print out
array(
(int) 1 => 'Consulting- Bus',
(int) 2 => 'Consulting- Pers',
(int) 7 => 'ClientEdge',
(int) 8 => '11111',
(int) 9 => 'testSUn',
(int) 10 => 'Test Bruce Review',
(int) 11 => 'Test Bruce 3 = Final'
when I select for example 'Test Bruce Review' and hit submit and debug the value it prints out '6', the 6th item in the dropdown box when I want it printing out 10 when I debug it.
here is the snippet from the form relating to this dropdown box
<tr><td><?php echo "Template: "?></td>
<td><?php echo $this->Form->input('template_id', array('label'=>false,'type'=>'select','options'=>$templates));?></td></tr>
how can I remedy this issue?
I had an issue with merging my arrays that caused the death of my template.id
I newbie in cakePHP.
I has some problem on display default current year in index.ctp
This code of index.ctp.
<?php
echo $html->tableCells (array (array ('Course',
$form->select('id',$courses)
),
array ('Location',
$form->select('id',$locations)
),
array ('Month',
$form->month('mob')
),
array ('Year',
$form->year('year',2011,2012)
),
)
);
?>
Thank you for your advanced :)
Try $form->year('year',2011,2012, date('Y'))