Form data is hashed in CakeRequest after upgrade to cakephp2.x - cakephp

I just recently performed an upgrade from cakephp 1.3 to 2.06. However when I attempt to access form data in my controller, the data is hashed like so:
2015-10-15 08:43:57 Error: CakeRequest Object
(
[params] => Array
(
[plugin] =>
[controller] => custom_users
[action] => ajax_login
[named] => Array
(
)
[pass] => Array
(
)
[isAjax] => 1
)
[data] => e1fe8b4c1724c30148e306708f27b30909f23d45
I checked that the form is correctly formed on the front end.
The documentation indicates that I should be able to access my data this way, but for some reason this behaviour is happening in stead. Any ideas?
I've added the trace I get when I try and submit the form:
Notice: Array to string conversion in /var/www/html/academy/lib/Cake/Utility/Security.php on line 92
Notice: Undefined offset: 1 in /var/www/html/academy/lib/Cake/Network/CakeResponse.php on line 456
Warning: Illegal string offset 'CustomUser' in /var/www/html/academy/app/Controller/CustomUsersController.php on line 569
Warning: Illegal string offset 'username' in /var/www/html/academy/app/Controller/CustomUsersController.php on line 569
Warning: Illegal string offset 'CustomUser' in /var/www/html/academy/app/Controller/CustomUsersController.php on line 570
Warning: Illegal string offset 'password' in /var/www/html/academy/app/Controller/CustomUsersController.php on line 570
Warning: implode(): Argument must be an array in /var/www/html/academy/lib/Cake/Utility/Security.php on line 92
Fatal error: Call to undefined method CustomAuthComponent::getModel() in /var/www/html/academy/app/Controller/Component/CustomAuthComponent.php on line 242

Related

CakePHP3.4: Why Request::getHeader() returns an array?

I have the following request:
2017-04-05 06:53:31 Error: Cake\Http\ServerRequest Object
(
...
[_environment:protected] => Array
(
[HTTP_REGISTRATION] => eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux
...
)
[_detectorCache:protected] => Array
(
[ajax] =>
[get] => 1
[head] =>
[options] =>
[post] =>
)
[uri:protected] => Zend\Diactoros\Uri Object
(
[allowedSchemes:protected] => Array
(
[http] => 80
[https] => 443
)
...
)
...
)
$this->request->header['registration'] returns:
eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux
as string
$this->request->getHeader('registration') returns:
[Registration] => Array
(
[0] => eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux
)
Why does it return an array?
The HTTP standard allows multiple headers with the same field name to be present in case all values of that header can be expressed as a single, comma separated string (Set-Cookie is an exception to that rule, it cannot express multiple values in a single string, but in practice does still appear multiple times in order to define multiple cookies).
In order to support this in a convenient way, the PSR-7 standard (which \Cake\Http\ServerRequest complies to) defines the getHeader() method to return header values as arrays. The complement to that is the getHeaderLine() method, which is ment to return multiple values as a single, comma separated string.
See also
https://www.rfc-editor.org/rfc/rfc7230#section-3.2.2
http://www.php-fig.org/psr/psr-7/#headers-with-multiple-values

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

Kohana 3.2 Call to undefined method Database_MySQL_Result::offset()

That happens when I try to play around with DB::select instead of ORM.
The query is returned as an object, but the error appears.
Code:
$bd_userdata -> offset($pagination -> offset) -> limit($pagination -> items_per_page) -> find_all() -> as_array();
Error:
ErrorException [ Fatal Error ]: Call to undefined method Database_MySQL_Result::offset()
Does it mean I have to count rows, before I send them to the offset in pagination?
When I try $query->count_all() I get the error message:
Undefined property: Database_Query_Builder_Select::$count_all
I tried count($query) but instead I got:
No tables used [ SELECT * LIMIT 4 OFFSET 0 ]
Here is the solution:
$results = DB::select('*')
->from('users')
->where('id', '=', 1)
->limit($pagination->items_per_page)
->offset($pagination->offset)->execute();
And a counter:
$count = $results->count_all();
I was doing it before, the other way around. That is why it did not work.
As you can see, execute() returns Database_Result object, which has no QBuilder's functionality. You must apply all conditions (where, limit, offset etc) before calling execute.
Here is a simple example with pagination:
// dont forget to apply reset(FALSE)!
$query = DB::select()->from('users')->where('username', '=', 'test')->reset(FALSE);
// counting rows
$row_count = $query->count_all();
// create pagination
$pagination = Pagination::factory(array(
'items_per_page' => 4,
'total_items' => $row_count,
));
// select rows using pagination's limit&offset
$users = $query->offset($pagination->offset)->limit($pagination->items_per_page)->execute();

How can I use sign in array of initial facts with Perl module AI::ExpertSystem::Advanced

I am trying to use the Perl AI::ExpertSystem::Advanced module, and I try to use sign in the array of initial facts. The documentation of this module shows an example:
my $ai = AI::ExpertSystem::Advanced->new(
viewer_class => 'terminal',
knowledge_db => $yaml_kdb,
initial_facts => ['I', ['F', '-'], ['G', '+']);
but there is something wrong (syntax error). I thing that one ] missing at the end of code.
First question: What is the correct form? When I run the example my terminal shows me a lot of errors.
Second question: Can I use a file to stored initial facts?
Thanks for your answers.
Error log:
when I use example from documentation:
syntax error at mix.pl line 24, near "])"
Global symbol "$ai" requires explicit package name at mix.pl line 26.
Missing right curly or square bracket at mix.pl line 27, at end of line
Execution of mix.pl aborted due to compilation errors.
When I put ] in its correct place at the end of expression: initial_facts => ['I', ['F', '-'], ['G', '+']]);
Attribute (initial_facts) does not pass the type constraint because: Validation failed for 'ArrayRef[Str]' with value ARRAY(0x3268038) at C:/Perl64/lib/Moose/Meta/Attribute.pm line 1274.
Moose::Meta::Attribute::verify_against_type_constraint('Moose::Meta::Attribute=HASH(0x3111108)', 'ARRAY(0x3268038)', 'instance', 'AI::ExpertSystem::Advanced=HASH(0x30ef068)') called at C:/Perl64/lib/Moose/Meta/Attribute.pm line 1261
Moose::Meta::Attribute::_coerce_and_verify('Moose::Meta::Attribute=HASH(0x3111108)', 'ARRAY(0x3268038)', 'AI::ExpertSystem::Advanced=HASH(0x30ef068)') called at C:/Perl64/lib/Moose/Meta/Attribute.pm line 531
Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta::Attribute=HASH(0x3111108)', 'Moose::Meta::Instance=HASH(0x32673d8)', 'AI::ExpertSystem::Advanced=HASH(0x30ef068)', 'HASH(0x3118298)') called at C:/Perl64/lib/Class/MOP/Class.pm line 525
Class::MOP::Class::_construct_instance('Moose::Meta::Class=HASH(0x2eb2418)', 'HASH(0x3118298)') called at C:/Perl64/lib/Class/MOP/Class.pm line 498
Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x2eb2418)', 'HASH(0x3118298)') called at C:/Perl64/lib/Moose/Meta/Class.pm line 274
Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x2eb2418)', 'HASH(0x3118298)') called at C:/Perl64/lib/Moose/Object.pm line 28
Moose::Object::new('AI::ExpertSystem::Advanced', 'viewer_class', 'terminal', 'knowledge_db', 'AI::ExpertSystem::Advanced::KnowledgeDB::YAML=HASH(0x3118478)', 'verbose', 1, 'initial_facts', 'ARRAY(0x3268038)') called at mix.pl line 20
This is a bug in the documentation (and possibly in the module itself).
To set the object up with negative initial facts you need to create the dictionary object first.
my $initial_facts_dict = AI::ExpertSystem::Advanced::Dictionary->new(
stack => [ 'I', ['F', '-'], ['G', '+'] ]);
my $ai = AI::ExpertSystem::Advanced->new(
viewer_class => 'terminal',
knowledge_db => $yaml_kdb,
initial_facts_dict => $initial_facts_dict,
);

Instance for File is not working in cakephp

i am getting the error as
Array to string conversion [CORE/cake/libs/file.php, line 96]
$path = array(
"name" => "23_50_11[1].gif",
"type" => "image/gif",
"tmp_name" => "/tmp/phpbBWxAT",
"error" => 0,
"size" => 25230
)
$create = false
$mode = 493
dirname - [internal], line ??
File::__construct() - CORE/cake/libs/file.php, line 96
FormsController::add() - APP/controllers/forms_controller.php, line 528
Object::dispatchMethod() - CORE/cake/libs/object.php, line 117
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 245
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 211
require - APP/webroot/index.php, line 88
[main] - CORE/index.php, line 61
Notice (8): Array to string conversion [CORE/cake/libs/file.php, line 97]
$path = array(
"name" => "23_50_11[1].gif",
"type" => "image/gif",
"tmp_name" => "/tmp/phpbBWxAT",
"error" => 0,
"size" => 25230
)
$create = false
$mode = 493
is_dir - [internal], line ??
File::__construct() - CORE/cake/libs/file.php, line 97
FormsController::add() - APP/controllers/forms_controller.php, line 528
Object::dispatchMethod() - CORE/cake/libs/object.php, line 117
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 245
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 211
require - APP/webroot/index.php, line 88
[main] - CORE/index.php, line 61
Warning (2): basename() expects parameter 1 to be string, array given [CORE/cake/libs/file.php, line 98]
The code that i have used is,
<h1>Form Fill</h1>
<?=$form->create('Form',array('type'=>'file'));?>
<?=$form->input('date',array('label'=>'Publication Date '));?>
<?=$form->input('headline');?>
<?=$form->input('content');?>
<?=$form->input('image',array('type'=>'file'));?>
<?=$form->end('Submit');?>
in the Formscontroller
function add($formid)
{
echo "Imaage ".$this->data['Form']['image'];// shows me the Imaage Array
if ($this->data['Form']['image']) {
$file = new File($this->data['Form']['image']);// this is not working
//$ext = $file->ext();
//echo "Extension ".$ext;
echo "Fiel ".$file;
$date = $this->data['Form']['date'];
$filename = $date['year'].'-'.$date['month'].'-'.$date['day'].'-form-image.'.'gif';
$data = $file->read();
echo "Data ".$data;
$file->close();
$file = new File(WWW_ROOT.'/img/'.$filename,true);
$file->write($data);
$file->close();
}
}
Instance for File is not working?? Please help me . actually i m trying to upload the image FIle and i m using Ubuntu linux machine ..
To recap:
$this->data['Form']['image']; // shows me the Imaage Array
$file = new File($this->data['Form']['image']); // this is not working
Error: Array to string conversion
The problem:
new File() expects a string as argument, but you're giving it an array.
Solution:
Supply a string as argument for new File(), preferably the path of the file:
$file = new File($this->data['Form']['image']['tmp_name']);
To be a little more detailed and help you fix these kinds of problems yourself next time (hopefully), it's not actually File that expects a string argument. If you look at the trace again:
dirname - [internal], line ??
File::__construct() - CORE/cake/libs/file.php, line 96
FormsController::add() - APP/controllers/forms_controller.php, line 528
...
This says the problem actually occurred in dirname, which is a PHP internal function. It was called on line 96 in File::__construct, which in turn was called from FormsController::add etc. pp.
Since PHP is a dynamic language it doesn't usually crap out completely when you supply arguments of the wrong type. Rather it'll try to make the best out of it and actually try to convert an array to a string, and just notify you that it did so with Notice (8): Array to string conversion. Since the result of an Array -> String conversion is literally the string "Array", your program will usually crap out later, since now you're trying to do things like is_dir("Array"), which is nonsense.
As you can see the program actually struggles on, with quite a few notices, and basename() on line 98 even complaining a little harder with a Warning:
94: function __construct($path, $create = false, $mode = 0755) {
95: parent::__construct();
96: $this->Folder =& new Folder(dirname($path), $create, $mode);
97: if (!is_dir($path)) {
98: $this->name = basename($path);
Warning (2): basename() expects parameter 1 to be string, array given [CORE/cake/libs/file.php, line 98]
Now, it would be good style for File to check if the parameter actually is a string or not and fail gracefully, but it doesn't bother. Because as a developer you should read the documentation to see what the function expects. If it's not on the website, look directly in the file /cake/libs/file.php:
/**
* Constructor
*
* #param string $path Path to file
* #param boolean $create Create file if it does not exist (if true)
* #param integer $mode Mode to apply to the folder holding the file
* #access private
*/
function __construct($path, $create = false, $mode = 0755) {
...

Resources