Cakephp 1.2 this->set not working - cakephp

So I am setting a variable to be used in view.
This happens during a form POST. Maybe this can give some hints to someone.
public function confirm_card(){
if(!isset ($this->data['UserPayment']) && empty($this->data['UserPayment'])){
$this->Session->setFlash(__d('payments', 'Select payment method', true), 'flash_error');
$this->redirect($this->referer());
}
else{
foreach($this->data['UserPayment'] as $key=>$up){
if(!empty($up)){
$this->set(array('paytype'=>$key));
return;
}
}
}
}
And in view
echo $paytype;
Result in view
Notice (8): Undefined variable: paytype
The key is returned as it should be so no empty values there.
This should be very basic... I am missing something here?

Try with
$this->set('paytype', $key);
Change
$this->redirect($this->referer());
The problem was !empty($up) vs $up != '' ?
$up is normally 0 or 1 ?

Related

Prestashop returns last value of imploded array from DB

I started fiddling with Prestashop 1.7 modules and I ran into this weird behavior.
I have this code to save values from form post to database (working ok)
protected function postProcess()
{
$form_values = $this->getConfigFormValues();
foreach (array_keys($form_values) as $key) {
Configuration::updateValue($key, Tools::getValue($key));
if($key == 'MSLT_MEGAMENU_CATEGORIES'){
$categories = implode(",",Tools::getValue($key));
Configuration::updateValue('MSLT_MEGAMENU_CATEGORIES', $categories);
}else{
$this->errors[]=$this->l('Please select categories to display');
}
}
}
And I use this code to fetch those values from database (works ok)
protected function getConfigFormValues()
{
$categories = explode(',',Configuration::get('MSLT_MEGAMENU_CATEGORIES', true));
return array(
'MSLT_MEGAMENU_LIVE_MODE' => Configuration::get('MSLT_MEGAMENU_LIVE_MODE', true),
'MSLT_MEGAMENU_CATEGORIES' => $categories,
'MSLT_MEGAMENU_ACCOUNT_EMAIL' => Configuration::get('MSLT_MEGAMENU_ACCOUNT_EMAIL', 'contact#prestashop.com'),
'MSLT_MEGAMENU_ACCOUNT_PASSWORD' => Configuration::get('MSLT_MEGAMENU_ACCOUNT_PASSWORD', null),
);
}
and helper to populate form with values
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
This is the var_dump() when trying to load values from database, for this case my DB value is (1,3,9)
array(1) { [0]=> string(1) "9" }
As you can see Configuration::get() only gets the last string value.
Interesting behavior is that when I update the data and stay in the same page, then everything is ok and data is fetched properly, but when I leave module configuration page and comeback, the issue happens. Maybe I am missing some little snippet of code? I am still a newbie. If needed I can provide more code.
I don't know what your specific goal is but in any case it is always convenient to use Prestashop's own functions.
To save and select the configuration variables
eg, save values in json format
Configuration::updateValue('MYVALUES', Tools::jsonEncode(Tools::getValue('MYVALUES')), true );
eg, get values
Tools::jsonDecode( Configuration::get( 'MYVALUES' );
Dirty workaround I found to get imploded values from DB correctly. Maybe someone will use it.
$sql = 'SELECT * FROM '._DB_PREFIX_.'configuration WHERE name = "MSLT_MEGAMENU_SELECTED_CAT"';
$value = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
$selected = explode(',',$value['value']);

Cakephp How to set find() to return blank array instead of empty array when no result matched

By default , cakephp would return empty array on find() when nothing founded.
but how to set it to show as blank array.
For example:
$customer = $this->Transaction->Customer->find(--conditions to return 0 result.--)
I want it to show as blank array like this.
array('Customer' => array('customer_id'=>null, 'name'=>null, 'lastname'=>null))
not just empty one like
array() or null
because I always got error shown in view that $customer['Customer']['name'] is undefined index. and I don't like to use isset() or is_null() to check before every time.
Use the afterFind callback method in your model. Something like this:
public function afterFind($results, $primary = false) {
if (empty($results)) {
$results = array('Customer' => array('customer_id'=>null, 'name'=>null, 'lastname'=>null))
}
return $results;
}
http://book.cakephp.org/2.0/en/models/callback-methods.html
If you really want/need to do this, you can use something like:
$default = array('Customer' => array('customer_id' => null, 'name'=>null, 'lastname' => null));
$customer = $this->Transaction->Customer->find(...)
$customer = array_merge($default, $customer);
This way, if the result is empty, it will use your default values.
However, this is not a good practice, because you might end up displaying "Welcome, NULL", in the page. You should use if (!empty($customer)) ... in your view.
Also, in this example, are you using find->('first') ?

Cakephp - how check row is null?

I want to set flash message if there's not fullfilled row "surname" in database. How can I check if row surname is null ?
I tried to make something like this:
if(!isset(['User']['surname'])
or
if($user(['User']['surname']) === NULL
it obviously doesnt work
You can check it like.
<?php
if(!isset($user['User']['surname']) || empty($user['User']['surename'])){
// your code goes here
}
if(empty($user['User']['surname'])) {
// your code goes here
}
you can check like this
if(isset($user['surname']) || empty($user['surname'])){
$this->Session->setFlash('No Data.');
}

pass parameter from url to cakephp

I need to send parameter from URL to cakephp controller. I have message table with two parameter 'ufrom' and 'uto'. in controller i want to save this values in message table.
I put in the URL:
http://localhost/ar/messages/add?ufrom=9&uto=3
in MessagesController i have function:
public function add() {
if(($this->request->query['uto'])and($this->request->query['ufrom'])){
$this->Message->create();
if ($this->Message->save($this->request->data)) {
$this->set('addMessage',TRUE);
$this->set('ufrom',$this->request->query['ufrom']);
$this->set('uto',$this->request->query['uto']);
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The message could not be saved. Please, try again.'));
}
$targets = $this->Message->Target->find('list');
$this->set(compact('targets'));
}
else{
$this->set('error',true);
}
}
and in add.ctp i have:
<?php
if(isset($error)){
echo('error');
}
else{
echo json_encode($ufrom);
echo json_encode($uto);
echo json_encode($addMessage);
}
?>
but when i use the above URL i see:
Notice (8): Undefined variable: ufrom [APP\View\Messages\add.ctp, line 6]null
Notice (8): Undefined variable: uto [APP\View\Messages\add.ctp, line 7]null
Notice (8): Undefined variable: addMessage [APP\View\Messages\add.ctp, line 8]null
and Nothing is stored in the database. I'm new in cakephp. please help.
here i can suggest you to use params like below
http://www.example.com/tester/retrieve_test/good/1/accepted/active
but if you need to use like this way
http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY
you can get the value like below
echo $this->params['url']['id'];
echo $this->params['url']['status'];
in your case would be like
echo $this->params['url']['uto'];
echo $this->params['url']['ufrom'];
The easiest way to pass parameters to the controller action would be to simply pass them through the action as arguments like so:
public function add($ufrom,$uto)
Your URL should look like:
http://localhost/ar/messages/add/9/3
Secondarily if the data is coming from the URL you wouldn't use this->request->data , simply :
$message = array("Message"=>array("ufrom"=>$ufrom,"uto"=>$uto));
$this->Message->save($message);

CakePHP saveField NULL value in a INT column

I want to update one column which is an INT with a NULL value. I'm using CakePHP 2.3 with the saveField('field', 'value') method.
Here is my code :
$this->Customer->id = $customer_id;
if ($this->Customer->saveField('account_id', 'NULL')) {
//Do some stuff
}
So when I put 'NULL', the row is updated to 0. I tried NULL without quotes and the row is not updated.
EDIT :
The field in the db accepts NULL value.
Do you have any idea ?
Thanks
Do not pass 'NULL' as a string but as null value:
if($this->Customer->saveField('account_id', null))
{
//do some stuff
}
else
{
//do you get anything here ?
debug($this->Customer->validationErrors);
}
I've had a similiar problem caused by a Upload Behaviour that had validations preventing the saving. Check your models, it might help. I assume saveField returns true even when validation rules prevent saving.

Resources