zend pass variable from controller to form - php

i'm developing an app and i'm trying to pass a variable from the controller to the form but without success. I need this variable for executing queries and populate fields of the form.
This is my code:
CONTROLLER:
$cod = $this->_getParam('cod');
$form = new Application_Form_MylacForm($cod);
FORM:
public $_codanagrafica;
protected $_codcircuito;
public function __construct($codanagrafica = null, $param)
{
$this->_codcircuito = $param;
echo $this->_codcircuito;
$this->_codanagrafica = $codanagrafica;
$this->init();
$this->loadDefaultDecorators();
}
With this code the variable _codcircuito is null.
How can i solve it?
Thank's

You are passing only one variable to form object:
$form = new Application_Form_MylacForm($cod);
And then you set _codcircuito with second argument, which is not passed:
$this->_codcircuito = $param;
$param is null in your case. Try:
$this->_codcircuito = $codanagrafica;
instead.

Related

DB::select procedure return empty array from input request

I created this function in my controller:
public function productInvoiceListTest(Request $request){
$var = $request->client_id;
$date = "2019-12-30";
$sql = 'CALL proc_invoice_product_list("'.$var.'","i.invoice_date","2019-12-30","","")';
$productinvoice = DB::select($sql);
var_dump($productinvoice);
}
When i run the code its return empty array, but when i hardcode value of $var it works:
public function productInvoiceListTest(Request $request){
$var = 'dasdsdasdsadsadsa';
$date = "2019-12-30";
$sql = 'CALL proc_invoice_product_list("'.$var.'","i.invoice_date","2019-12-30","","")';
$productinvoice = DB::select($sql);
var_dump($productinvoice);
}
Why when i use input from request can't call my procedure? How do i read from input request?
You may need to first confirm the client_id is being passed to your productInvoiceListTest method by using the dd helper.
public function productInvoiceListTest(Request $request){
dd($request->all());
}
If the client_id is part of the the request instance passed in your productInvoiceListTest method, then you can access it using $request->input('client_id') method. If it is not part of the request instance, ensure that from the method calling or redirecting to productInvoiceListTest, the client_id is passed.

Codeigniter Database Error Number 1048 Values show NULL even though they are NOT NULL

I have situation where codeigniter shows database Error Number 1048. It seems Values NULL but when I try to check it usign var_dump($_POST) Values are not NULL.
Controller : Jurusan.php
public function simpan()
{
$this->form_validation->set_rules('code','Kode','required|integer');
$this->form_validation->set_rules('jurusan','Jurusan','required');
$this->form_validation->set_rules('singkatan','Singkatan','required');
$this->form_validation->set_rules('ketua','Ketua','required');
$this->form_validation->set_rules('nik','NIK','required|integer');
$this->form_validation->set_rules('akreditasi','Akreditasi','required');
if($this->form_validation->run() == FALSE)
{
$isi['content'] = 'jurusan/form_tambahjurusan';
$isi['judul'] = 'Master';
$isi['sub_judul'] = 'Tambah Jurusan';
$this->load->view('tampilan_home',$isi);
} else {
$this->model_security->getSecurity();
$key = $this->input->post('code');
$data['kd_prodi'] = $this->input->post['code'];
$data['prodi'] = $this->input->post['jurusan'];
$data['singkat'] = $this->input->post['singkatan'];
$data['ketua_prodi'] = $this->input->post['ketua'];
$data['nik'] = $this->input->post['nik'];
$data['akreditasi'] = $this->input->post['akreditasi'];
$this->load->model('model_jurusan');
$query = $this->model_jurusan->getdata($key);
if($query->num_rows()>0)
{
$this->model_jurusan->getupdate($key,$data);
} else {
$this->model_jurusan->getinsert($data);
}
redirect('jurusan');
}
}
Model : model_jurusan.php
class Model_jurusan extends CI_model {
public function getdata($key)
{
$this->db->where('kd_prodi',$key);
$hasil = $this->db->get('prodi');
return $hasil;
}
public function getupdate($key,$data)
{
$this->db->where('kd_prodi',$key);
$this->db->update('prodi',$data);
}
public function getinsert($data)
{
$this->db->insert('prodi',$data);
}
}
Here is the error shown :
Here is the database structure :
You have a wrong syntax in these lines:
$key = $this->input->post('code');
$data['kd_prodi'] = $this->input->post['code']; // <-- use ('code')
$data['prodi'] = $this->input->post['jurusan']; // <-- use ('jurusan')
Change this to
$this->input->post['array_key'];
this
$this->input->post('array_key');
Read : Input Class in Codeigniter
Well the problem lies in your way of accepting input parameters.
$this->input->post
is a method which accepts the variable name, not an array. So all the input parameters need to be passed as a function parameter to post method. These lines need to be altered to.
$data['kd_prodi'] = $this->input->post('code');
$data['prodi'] = $this->input->post('jurusan');
$data['singkat'] = $this->input->post('singkatan');
$data['ketua_prodi'] = $this->input->post('ketua');
$data['nik'] = $this->input->post('nik');
$data['akreditasi'] = $this->input->post('akreditasi');
Hope this solves the problem.
EDIT:
You did a var_dump($_POST) which works as it is supposed to and it will read the values of the post parameters. So either you fetch the parameters from $_POST array, or you use the $this->input->post() method. But I would suggest using the $this->input->post() method as it provides additional sanitization such as xss attack handling etc, which could be turned on an off from the config.
i have tried your code...it works. I think there some mistakes in your <input> tags, You must use <input name=""> not <input id=""> or something else. Hope it can help you out
You are try to get value from post is wrong. You should use at this way
$_POST['array value'];

Can't get the set value of varible in one function in other diff func in the same class php

I am having a problem with this variable "admin_user_id".. i want to set the val of the var in one function and use that var value in other function here..
But problem is that it gives null. i think its bcz of diff reference of the class..
class ChangeRequestProcessController extends AppController{
static $admin_user_id;
public function ajax_list_of_change_request(){
$this->autoRender = false;
$params = array();
$params[] = $this->Auth->user('id');
//debug($params);
$result = $this->AppProcess->callProcedure('ListOfChangeRequestProcess', $params);
//echo("here:"+$result);
$admin_user_id=$params[0];
//debug( $this->admin_user_id);
echo json_encode($result);
}
public function application_attachment_upload_with_title() {
$this->autoRender = FALSE;
$attachment_array[]=$this->admin_user_id;///p_admin_user_id
//$attachment_array[]="name";///p_uploaded_by
$attachment_array[]=$_POST['title'];///p_attachment_title
// $attachment_array[] = $loged_on_id;
//$attachment_array[] = $_FILES[0]['name'];
//$attachment_array[] = $_POST['title'];
debug($attachment_array);
$result = $application->AttachmentTemp->callProcedure('ChangeRequestAttachmentFinalAddWithTitle', $attachment_array);///call procedure
echo json_encode($result);///result
}
how to solve this problem in php so that the value persists(i think static var will solve it..but it also gives undefined var error inside function)???
Thanks in advance
(this doesn't fit the comment box, so here i go...)
Assuming
// your code to get this...
$admin_user_id = $params[0];
You can store it in a session using this:
if(!session_id()){
session_start();
}
$_SESSION['admin_user_id'] = $admin_user_id; // again, assuming this is the value!
To read it (in your second call/method), simply do:
if(!session_id()){
session_start();
}
$admin_user_id = $_SESSION['admin_user_id'];
For more info, see the php.net: sessions and $_SESSION

Creating default object from empty value (adding an item to a database for a new user in Laravel)

I have a function I am using to add
I have an array $data that contains the user data I am trying to put into the db. Everything works except for the "makeUrlTag" function portion:
public function makeUrlTag() {
$url_tag = '';
if(isset($this->data['user']['first_name'])) {
$url_tag = $url_tag . $this->data['user']['first_name'];
}
if(isset($this->data['user']['last_name'])) {
$url_tag = $url_tag.$this->data['user']['last_name'];
}
$fan->url_tag = $url_tag;
}
public function createFan() {
$fan = new Fan;
$fan->fbid = isset($this->data['user']['id']) ? $this->data['user']['id'] : '';
$fan->email = isset($this->data['user']['email']) ? $this->data['user']['email'] : '';
$fan->first_name = isset($this->data['user']['first_name']) ? $this->data['user']['first_name'] : '';
$fan->last_name = isset($this->data['user']['last_name']) ? $this->data['user']['last_name'] : '';
$this->makeUrlTag();
$fan->save();
}
I call createFan with:
$this->createFan();
When I run this, I get the error:
Creating default object from empty value
in reference to the makeUrlTag(); portion. Particularly the line:
$fan->url_tag = $url_tag;
Any idea what's going on here? Again, taking out the makeUrlTag portion works fine. Thank you.
It's because your makeUrlTag() method doesn't know about the Fan which is in the $fan variable you created in the createFan() method and trying to use a non-existing object in the scope of makeUrlTag() method using this:
$fan->url_tag = $url_tag;
So, you need to make your $fan object available to makeUrlTag() and to do this you may add a protected property in your class:
class YourClass {
protected $fan = null;
public function makeUrlTag(){
$url_tag = '';
// ...
$this->fan->url_tag = $url_tag;
}
public function makeUrlTag(){
$this->fan = new Fan;
// rest of your code
// but use $this->fan instead of $fan
$this->fan->save();
}
}
So, now you can access the $fan object from any method of your class ussing $this->fan, that's it.

How to alter Symfony form array value before saving?

I have a form and after submission, I can view the form values using: var_dump($this->form->getValues();. One of my form values (from a multi-select widget) is this:
["cat_list"]=>
array(1) {
[0]=>
string(1) "1"
}
I wish to append a value to this array before saving the form. How do I do this? I thought I can do this:
$values = $this->form->getValues();
array_push($values['cat_list'], '99'); // <--- 99 is the number I want to append
$this->form->setCatList($values['cat_list']);
$this->form->save();
But that doesn't work:
Call to undefined method FrontendOfferForm::setCatList.
Any clues?
In the action where you initialize the form before the validation (normally in either the create or the update function), just pass to the form an empty object with the value you want to add arbitrarily already set.
Let's call your form myModelForm and your model myModel.
In the action (before the processForm)
$obj = new myModel();
$obj->setCatList(array(99));
$this->form = new myModelForm($obj);
This should work
You should override doUpdateObject function in the form calss.
protected function doUpdateObject($values)
{
parent::doUpdateObject($values);
if (isset($values['cat_list']))
{
$carList = is_array($values['cat_list']) ? $values['cat_list'] : array($values['cat_list']);
array_push($catList, 99);
$this->getObject()->setCatList($catList)
}
}
And that's it. You should call only $this->form->save() in the action.
You should have an other method than the one from #1ed.
When you save the form, it returns the object. So, you have to save the form first and then, update the cat_list:
$values = $this->form->getValues();
$object = $this->form->save();
array_push($values['cat_list'], '99'); // <--- 99 is the number I want to append
$object->setCatList($values['cat_list']);
$object->save();
By the way, if you choose the solution from #1ed, you should use an external parameter to define your 99, if you want to be able to use something else than 99.
Like:
$this->form->catListExtraValue = 99;
$this->form->save();
Then:
public $catListExtraValue = null;
protected function doUpdateObject($values)
{
parent::doUpdateObject($values);
if (isset($values['cat_list']) && null !== $this->catListExtraValue)
{
$carList = is_array($values['cat_list']) ? $values['cat_list'] : array($values['cat_list']);
array_push($catList, $this->catListExtraValue);
$this->getObject()->setCatList($catList)
}
}

Categories