cakephp dropdown box and arrays - arrays

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

Related

Laravel 5.4 skip first value in Pluck array

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.

smarty get offset of array

I have some trouble in prestashop 1.6 using smarty.
I have an array, but its offset are not reset for each product.
so for the firdst product, with attrivute it has offset 1,2,3,4 Then for the next product it has offest 5,6,7,8 etc.
I have that kind of array
$combinations Smarty_Variable Object (3)
->value = Array (4)
5 => Array (14)
attributes_values => Array (1)
1 => "S"
attributes => Array (1)
0 => 1
price => 0
specific_price => Array (0)
ecotax => 0
weight => 0
quantity => 20
reference => ""
unit_impact => 0
minimal_quantity => "1"
date_formatted => ""
available_date => ""
id_image => -1
list => "'1'"
6 => Array (14)
I try to go trhough this array but it does not work when I put empty offset (it is inside a foreach)
{$combinations[]['quantity']}
How can I tell him to go trhough the first iteration, and then in the second automatically ?
This return to me the following errors.
Fatal error: Cannot use [] for reading in /htdocs/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 584
I can not tell him which offset to use, because for each product it goes up and is not reset to 0.
I would be very gratefull for anykind of help.
Here's how to do it, current return the first value of an array
{$combination = current($combinations)}
{$combination['quantity']}
in addition to #UnLoCo answer, if you need these keys 1,2 ... 7,8
{foreach from=$array key=key item=value}
{$key} => {$value}
{/foreach}
or
{foreach $array $key=>$value} {* like PHP style *}
{$key} => {$value}
{/foreach}
Also Smarty docs may help you http://www.smarty.net/docs/en/language.function.foreach.tpl

Calculation in virtual field not working

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);

CakePHP - can't email via SMTP

pulling my hair out with this one.
When I developed this cakephp (2.2) app on my local machine, I could email via gmail using smtp without a problem. Since then I've deployed it onto a 1and1 server and I can no longer send. What even more frustrating is that I cant seem to get any decent debugging info from cake.
Heres the code:
if($fu['name']) {
$url = Router::url('/admin/', true );
$ms = $url;
$ms = wordwrap($ms,1000);
//============Email================//
// SMTP Options
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host' => 'ssl://smtp.gmail.com',
'username'=>'myemail#gmail.com',
'password'=>'password'
);
$this->Email->template = 'newExpenseClaim';
$this->Email->from = 'Expense Tracker <myemail#email.co.uk>';
$this->Email->to = 'myemail#fakeemail.com';
$this->Email->subject = 'New Expense Claim Submitted - Please Review';
$this->Email->sendAs = 'both';
$this->Email->delivery = 'smtp';
// Set username & url in email
$this->set('user', $fu['name']);
$this->set('ms', $ms);
//$this->Email->send();
if(!$this->Email->send()) {
CakeLog::write('debug', $this->Email->smtpError);
}
$this->set('smtp_errors', $this->Email->smtpError);
//============EndEmail=============//
}
I get the following debug info each times it fails (printed on screen, nothing goes into the debug log which is massively anoying).
Connection refused
Error: An Internal Error Has Occurred.
Stack Trace
CORE/Cake/Network/Email/SmtpTransport.php line 95 → CakeSocket->connect()
CORE/Cake/Network/Email/SmtpTransport.php line 60 → SmtpTransport->_connect()
CORE/Cake/Network/Email/CakeEmail.php line 1059 → SmtpTransport->send(CakeEmail)
CORE/Cake/Controller/Component/EmailComponent.php line 345 → CakeEmail->send(null)
APP/Controller/ExpenseClaimsController.php line 236 → EmailComponent->send()
APP/Controller/ExpenseClaimsController.php line 278 → ExpenseClaimsController->notifyAdminsOfNewClaims()
[internal function] → ExpenseClaimsController->add()
CORE/Cake/Controller/Controller.php line 485 → ReflectionMethod->invokeArgs(ExpenseClaimsController, array)
CORE/Cake/Routing/Dispatcher.php line 186 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 161 → Dispatcher->_invoke(ExpenseClaimsController, CakeRequest, CakeResponse)
APP/webroot/index.php line 92 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Can anyone help? Thanks in advance
** Updated with wrapper and soctet info **
These are the wrappers and sockets I get returned from those functions:
Wrappers:
array(
(int) 0 => 'https',
(int) 1 => 'ftps',
(int) 2 => 'compress.zlib',
(int) 3 => 'compress.bzip2',
(int) 4 => 'php',
(int) 5 => 'file',
(int) 6 => 'data',
(int) 7 => 'http',
(int) 8 => 'ftp',
(int) 9 => 'zip'
)
Sockets
array(
(int) 0 => 'tcp',
(int) 1 => 'udp',
(int) 2 => 'unix',
(int) 3 => 'udg',
(int) 4 => 'ssl',
(int) 5 => 'sslv3',
(int) 6 => 'sslv2',
(int) 7 => 'tls'
)
In first you should check what your hosting support outgoing connection. Simple check is similar
echo implode("", file("http://google.com"));
In second you should check what PHP support SSL connection and it have stream wrapper for SSL.
I now not remember how you can check it. But search in google by keywords: PHP stream wrapper SSL

cake php element cache

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.

Resources