Why getPostParameters doesn't work on symfony 1.4? - php

I have a form in symfony 1.4 which are created for each competence. My forms was created with success. But when I try to save my form I can't. I go to see my action code and the function getPostParameters seem dosn't work. I use getParameterHolder to see what's wrong in my parameters but after I put the good value the getPostParameters function doesn't work.
This is what I get from getParameterHolder:
sfParameterHolder Object
([parameters:protected] => Array
(
[professionnal_competence] => Array
(
[rayon_competence3] => 24
[rayon_competence9] => 22
[rayon_competence19] => 32
)
[module] => professionnal_subregion
[action] => saveCompetenceRadius
)
)
And my function:
public function executeSaveCompetenceRadius(sfWebRequest $request) {
$user = $this->getUser()->getGuardUser();
$q = ProfessionnalCompetenceQuery::create()
->addSelect('pc.*')
->where('pc.professionnal_id= ?', $user->getId());
$res = $q->execute();
$values = $request->getPostParameters(['professionnal_competence']);
$test = $request->getParameterHolder();
var_dump($values); print_r($values); print_r($request->getParameterHolder());
exit;
foreach ($res as $professionnalCompetence) {
foreach ($values['professionnal_competence'] as $k => $val) {
if ($k == 'rayon_competence' . $professionnalCompetence->getCompetenceId()) {
$professionnalCompetence->setRayonCompetence($val);
$professionnalCompetence->save();
}
}
}
return $this->renderComponent('professionnal_subregion', 'competenceRadius');
// return "test";
//return $this->renderPartial('professionnal_subregion/competenceradius');
}
This is my form:
class ProfessionnalCompetenceRadiusForm extends BaseProfessionnalCompetenceForm {
public function configure()
{
unset($this['rayon_competence']);
$this->widgetSchema['rayon_competence'.$this->object->getCompetenceId()] = new sfWidgetFormSelectUISlider(array('max'=>50,'step'=>1));
$this->widgetSchema->setHelp('rayon_competence'.$this->object->getCompetenceId(),'en kilomètres');
$this->widgetSchema->setLabel('rayon_competence'.$this->object->getCompetenceId(),'rayon');
$this->setValidator('rayon_competence'.$this->object->getCompetenceId(), new sfValidatorInteger(array('max'=>50)));
}
}
Someone has an idea or can help me ?? Because I try lot of thing but without success. Thank in advance :).

I think the error hides in this line:
$values = $request->getPostParameters(['professionnal_competence']);
You're passing an array to a function that takes a string. Try removing the brackets around 'professionnal_competence'.
EDIT: Scratch that. getPostParameters takes no parameters. getPostParameter, on the other hand, takes two - the first of which is the field name - a string. So, your code should be:
$values = $request->getPostParameter('professionnal_competence');

The error is here:
$values = $request->getPostParameters(['professionnal_competence']);
The function sfWebRequest::getPostParameters doesn't actually take parameters.
You can either access this array with [...], or use getPostParameter, which allows "safe" deep access:
$val = $request->getPostParameter('a[b]');
// basically the same as, but with error checks:
$val = $request->getPostParameters()['a']['b'];

Related

Can an Abstract Class in PHP Have a Default Method?

I've most certainly got something very basic wrong here.
Here is the code that is part of my Abstract Class:
private $outarray = null;
public function add_to_array($ahref, $docname, $description) {
$row = array('ahref' => $ahref, 'docname' => $docname, 'description' => $description);
if (!isset($this->outarray)) {
$this->outarray = array();
}
array_push($this->outArray, $row);
}
When I step through the code, though, the outArray remains null. It is never created and never populated.
I'm still green with PHP, but this help doc seems to leave me believing that this is OK to do:
http://www.php.net/manual/en/language.oop5.abstract.php
...particularly where they are declaring the Common method printOut() that performs some action.
I've got 5 elements I am trying to populate outArray with, but each of the 5 times I circle into this function, I come out with outArray being NULL.
Variables are case sensitive. You have in one place $this->outarray and in array_push you have $this->outArray
Ugh.
PHP is case sensitive, but it does not complain about it because it was assuming I had another variable declared on the fly.
Correct way:
public function add_to_array($ahref, $docname, $description) {
$row = array('ahref' => $ahref, 'docname' => $docname, 'description' => $description);
if (!isset($this->outarray)) {
$this->outarray = array();
}
array_push($this->outarray, $row);
}

Too many parameters for model creation in controller?

A have a lot of controllers where I must to save/create new models, it looks like this:
public Controller_Test extends Controller_Template {
if ($post = $this->request->post()) {
$model = ORM::factory('model');
$model->param1 = $post['Param1'];
$model->param2 = $post['Param26'];
$model->param3 = $post['Param31'];
$model->param4 = $post['Param13'];
$model->param5 = $post['Param2'];
$model->param6 = $post['Param35'];
$model->param7 = $post['Param10'];
$model->param8 = $post['Param22'];
$model->param9 = $post['Param3'];
$model->save();
}
}
Is it possible to unify (create a method) thats will save all array?
I know about $model->values($post)->create();, but still can't understand how really same it works, as u can see I have different keys of posted parameteres and this might be considered.
In many examples all the data assignemnts take place in controller, but they're really small, in my case I'll suppose to have a huge controllers with a lot of data assignment strings and it will be a bad style coding I think.
Whatever you do you need to map the key names in your $_POST variable to model property names.
$model_post_map = array(
'param1' => 'Param1',
'param2' => 'Param26',
'param3' => 'Param31',
'param4' => 'Param13',
'param5' => 'Param2',
'param6' => 'Param35',
'param7' => 'Param10',
'param8' => 'Param22',
'param9' => 'Param3',
);
$post_model_map = array_flip($model_post_map);
function rekey($arr, $map) {
$newarr = array();
foreach ($arr as $k => $v) {
if (isset($map[$k])) {
$newarr[$map[$k]] = $v;
}
}
return $newarr;
}
$modeldata = rekey($post, $post_model_map);
$model->values($modeldata);
You should name your form fields the way you do your models to reduce the impedance mismatch.
You should also use the second argument to $model->values() to restrict what a form can change.

How to write a specific model method as the callback in call_user_func_array()?

I'm been trying to send a dynamic number of arguments to a function in Codeigniter and have been running into a wall with call_user_func_array.
One error I'm getting "Undefined property: Account::$callShowMessage" is making me wonder if it's even possible to refer to a function in another file with call_user_func_array/CodeIgniter.
If anyone could help point me into the right direction, it would be greatly appreciated.
Array:
Array ( [0] => Current password is incorrect [1] => New passwords don't match )
Controller Excerpt:
$this->session->set_flashdata('msg', call_user_func_array($this->generic_model->callShowMessage, $msg));
Function:
public function callShowMessage()
{
$args = func_get_args();
foreach ($args as $key => $value) {
$msg .= "$value<br />";
}
$msg = addslashes($msg);
return "<script type='text/javascript'>$(document).ready(function() { showMessage('$msg') });</script>";
}
call_user_func_array(array($this->generic_model, 'callShowMessage'), $msg)
To pass a method of an object as callback, use the above array syntax with the object and the name of the method. See http://php.net/manual/en/language.pseudo-types.php#language.types.callback(dead link).

How to determine user defined function parameters names before calling it?

In PHP, I have created a user defined function. Example:
<?php
function test($one, $two) {
// do things
}
?>
I would like to find the names of the function parameters. How would I go about doing this?
This is an example of what I would like:
<?php
function test($one, $two) {
// do things
}
$params = magic_parameter_finding_function('test');
print_r($params);
?>
This would output:
Array
(
[0] => one
[1] => two
)
Also this is very important that I am able to get the user defined function parameter names outside the scope of the function. Thanks in advance!
You can do this using reflection...
$reflector = new ReflectionFunction('test');
$params = array();
foreach ($reflector->getParameters() as $param) {
$params[] = $param->name;
}
print_r($params);
you can use count_parameter a php built in function

decode mysql query before returning it to view

I'm running a query to mysql that returns encrypted data. I'd like, if possible, to decode the results before sending it to the view. It seems like better form to handle the decoding in the controller (or even the model) rather than inside the view.
I can't seem to wrap my head around how to do it, though.
I was thinking I could iterate through the object, decodode it, and push it to another array that would be sent to the view. Problem with this is I won't know (and need to keep) the indexes of the query.
So the query might return something like:
[id] => 742
[client_id] => 000105
[last] => dNXcw6mQPaGQ4rXfgIGJMq1pZ1dYAim0
[first] => dDF7VoO37qdtYoYKfp1ena5mjBXXU0K3dDlcq1ssSvCgpOx75y0A==
[middle] =>iXy6OWa48kCamViDZFv++K6okIkalC0am3OMPcBwK8sA==
[phone] => eRY3zBhAw2H8tKE
Any ideas?
Ended up with:
function name(){
$data['e_key']=$this->e_key;
$clid = $this->uri->segment(3);
$name = $this->Clients_model->getNameData('*','client_id='.$clid,'');
$nameArray= array();
foreach ($name->result() as $row){
$x = $row;
$keys = array('id','client_id');
$unenc = array();
foreach ($x as $key=>$value){
if(! in_array($key, $keys)){
$unenc[$key]=$this->encrypt->decode($value,$this->e_key);
}else{
$unenc[$key]=$value;
}
}
array_push($nameArray,$unenc);
}
$data['name'] = $nameArray;
$this->load->view('names/name_view',$data);
}
Assuming you know how to decrypt the data, it's but a matter of iterating over the object, decrypting the encrypted fields.
If $YOUR_OBJECT is your object and your function for decryption is decode() then the following code should do the trick.
// The keys corresponding to the encrypted fields
$encoded = array('last', 'first', 'middle', 'phone');
$decoded = array();
foreach($YOUR_OBJECT as $key => $value)
{
if (in_array($key, $encoded))
{
$decoded[$key] = decode($value);
}
}
if it's a particular index, you could decode it like
$result['last'] = base64_decode($result['last']);
or in the model, use mutators and accessors:
public function setUp() {
$this->setTableName('tablename');
$this->actAs('Timestampable');
$this->hasMutator('last', '_encode64');
$this->hasAccessor('last', '_decode64');
}
protected function _encode($value) {
$this->_set('last',base64_encode($value));
}
protected function _decode($value) {
return base64_decode($value); // not sure on this one - might have to
// return $this->set('last', base64_decode($value));
}

Categories