"Node no longer exists" Yii - session

I want to keep the data between 2 actions in Yii by session. i'm writing the data in one action
Yii::app()->session['resultsData'] = $data;
and try to get it in next action
$this->resultsData = Yii::app()->session['resultsData'];
but when i want print_r this data in view...
<? print_r($this->resultsData); ?>
i'm get
PHP warning
print_r() [<a href='function.print-r'>function.print-r</a>]: Node no longer exists
actually, any manipulate with this data array, like serialize or json_encode, returns this error. can it be because of corrupted data? thank you.

Yii::app()->session['resultsData'];No need to use $this in action just assign the variable as shown
Yii::app()->session['resultsData'] = $data;
resultsData =Yii::app()->session['resultsData'];
and access it in another action as
echo Yii::app()->session['resultsData'] ;
If U want to acces the resultData in another u need to pass that variable to that action as shown
$this->redirect(array('actionname','resultData'=>$resultData));
And in your actionname echo it or print_r and check
Send it to view as shown
$this->render('ViewName',array('resultData'=>resultData));
From view to action use this
Yii::app()->createAbsoluteUrl('ControllerName/actionName',array('data'->$data));

Maybe this helps you:
$model = new CompanyForm('step1');
if (is_array(Yii::app()->session['step1']))
$model->attributes = Yii::app()->session['step1'];
if (!is_array(Yii::app()->session['step1']))
$this->redirect(array('createStep1'));
$this->render('create', array(
'model' => $model,
'step' => 'step1'
));
maybe you are saving the data in the wrong place;
maybe you have a post or get action;
and also, print_r the $data and see wat you get before saving the data in session

Related

cakephp 3 form validation for associated model data

I need help with validation of associated model data. I have a form with User data (UsersTable) with additional fields from Model Companies (Users hasMany Companies).
My form view look like this:
echo $this->Form->create($contentData);
echo $this->Form->control('User.person_name');
echo $this->Form->control('User.email');
echo $this->Form->control('User.phone');
echo $this->Form->control('Companies.0.company_name');
//I try Companies.company_name too
echo $this->Form->control('Companies.0.nip');
Action in controller look like this:
$userTable = TableRegistry::get('Users');
$contentData = $userTable->newEntity(null, ['associated' => ['Companies']]);
if ($this->request->is(['post', 'put'])) {
$formData = $this->request->getData();
$contentData = $userTable->newEntity($this->request->getData(),
['validate' => true, 'associated' => ['Companies']]);
pr($contentData->getErrors());
die();
}
Unfortunately debug return error from UsersTable validation, except CompaniesTable.
Could U any idea, where did i go wrong ?
For the main model (User), you shouldn't need the model name at all in the form. For associated models, the convention is lower case plural.
echo $this->Form->control('person_name');
echo $this->Form->control('email');
echo $this->Form->control('phone');
echo $this->Form->control('companies.0.company_name');
echo $this->Form->control('companies.0.nip');
Unrelated notes: you initialize $formData but never use it, you shouldn't need to pass any parameters to the first newEntity call, and typical usage is to use patchEntity instead of newEntity inside the if.

Can be used exist validator with a DynamicModel in Yii2

I need validate a user input against a database, if the input exist, the field should be validate. I have created a Dynamic Model in this way:
$modelDynamic=new DynamicModel(['TIN','Business_Code']);
and added the next exist rule validator:
$modelDynamic->addRule('TIN','exist',[
'targetClass'=>'frontend\modules\profiles\models\PorfBusiness',
'targetAttribute'=>'TIN',
'skipOnEmpty'=>true
])->validate();
all of them in my controller ProfPersonController.
And create this form in a view called profile:
if (isset($modelDynamic)) {
$formDynamic = ActiveForm::begin([
'type'=>ActiveForm::TYPE_HORIZONTAL,
'formConfig'=>[],
]);
echo $formDynamic->errorSummary($modelDynamic);
echo $formDynamic->field($modelDynamic,'TIN')
->textInput()
->label(Yii::t('app','TIN'));
echo $formDynamic->field($modelDynamic,'Business_Code')
->textInput()
->label(Yii::t('app','Business Code'));
echo HTML::submitButton('Submit');
ActiveForm::end();
}
(Imge with a wrong code)
The validation makes nothing, nor if the input is correct, nor if is incorrect. No errorSummary is sended neither.
Any error? Any idea? Thanks in advance.
Change Your Controller Code as
$modelDynamic= new DynamicModel(['TIN','Business_Code']);
$modelDynamic->addRule('TIN','exist',[
'targetClass'=>'frontend\modules\profiles\models\PorfBusiness',
'targetAttribute'=>'TIN',
'skipOnEmpty' => true
]);
if($modelDynamic->load(Yii::$app->request->post())&& $modelDynamic->validate()){
//Model is Validated and ready to go throgh
}
You must validate() your model after load() of your DynamicModel

My data only shows if I debug it. What's going on?

I'm working with Stripe's API and am trying to retrieve data. I have the code below:
$data = \Stripe_Invoice::all(array(
"customer" => $user->customer_id
));
If I set the AJAX response equal to $data, the response is shown as empty ( {} ). If I debug it in the backend, I get a huge list of all kinds of awesome properties to use. All I do is this:
debug($data); // returns huge data set
The trouble is that I can't access the variable in the frontend. I want to use:
console.log(response);
html += response.url;
And things to that effect, but the data is completely empty when the front end interprets it, for some reason.
In the same effect, I can't set it as a session either (I used to set session logs to debug instead of using the debug feature).
$data // can be accessed on the frontend if we use just php to set a variable
$_SESSION['log'] = $data; // empty
What's going on? I'm using the PHP framework CakePHP 3 (latest version of Beta). I think it has something to do with returning the data as serialized (maybe?) but that wouldn't explain the session logging. This happens right before we send the data back:
$this->set(compact('data', $data));
$this->set('_serialize', 'data');
If $data is not empty than you should just use the set method in the controller
$this->set(compact('data', $data));
Than you should have the corresponding view at /src/Template/ControllerName/json/methodName.ctp (Change ControllerName and methodName to what you have)
This file should be this.
<?php
print json_encode($data);
?>
That is all. You should have your data on your client side as a json object.
Turns out the answer was that the values could not be displayed while the array was protected. Calling Stripe's method __toArray() on the Stripe object made the data accessible, and setting worked past this point.
$data = \Stripe_Invoice::all(array(
"customer" => $user->customer_id
));
$data = $data->__toArray();
$this->set(compact('data', $data));
$this->set('_serialize', 'data');

How can I debug $Model after validation?

I want to see the content of validationErrors => array(???) of the $Model after a failed validation, but there is no "afterValidation()" method.
Does anyone know how can I see that or at least how would it look exactely?
Thank's!
On Controller, you can validate data before you trying save:
$this->ModelName->set($this->request->data);
if ($this->ModelName->validates()) {
// success
} else {
// failed
$errors = $this->ModelName->validationErrors;
}
Reference:
Validating Data from the Controller
Use $this->ModelName->invalidFields() after you have made the save/whatever you're doing:
For example:
debug($this->ModelName->invalidFields());
If you have a redirect at some point after that call, you might not see the data in your view. In this case, you can always do die(); either right after or wrapped around your call like so:
die(debug($this->ModelName->invalidFields());

CodeIgniter problem retrieving and displaying data from DB

Here is my function. It is very simple.
function load_data() {
$query = $this->db->query('SELECT * FROM configurations WHERE username="' . $this->session->userdata('username') . '"');
return $query;
}
My controller has this line of code:
$data['query'] = $this->configurations->load_data();
In my view, I tried:
foreach($query->result_array() as $row) {
echo $row->first;
}
But I get an error that I am trying to get a property of a non-object. Isn't the query being returned from the model as an object?
You should use $query->result_array, row_array, result, or row otherwise your returning the object intead get the results. Check the CI manual.
You are returning the results as array and using $row as object!
Try:
foreach($query->result() as $row) {
Refer.
Try changing $this->load->model('login/configurations', '', TRUE); to $this->load->model('login/configurations', 'configurations', TRUE); and see if it works. If it does, it is related to how you're extending your model class. That is, it would be related to what name you give inside configurations.php.
Hope this helps.
Your undefined variable error tells me that your query might not be running correctly. To diagnose...enable the profiler to check your query.
From the documentation:
$this->output->enable_profiler();
Permits you to enable/disable the
Profiler, which will display benchmark
and other data at the bottom of your
pages for debugging and optimization
purposes.
To enable the profiler place the
following function anywhere within
your Controller functions:
$this->output->enable_profiler(TRUE);
When enabled a report will be
generated and inserted at the bottom
of your pages.
Your query will be shown at the end of
the page
Double check your query syntax to make sure it is running properly, and
your code in it's current state is returning an object of objects and arrays:
print_r($query)
CI_DB_mysql_result Object
(
[conn_id] => Resource id #29
[result_id] => Resource id #39
[result_array] => Array
(
)
[result_object] => Array
(
)
[current_row] => 0
[num_rows] => 3
[row_data] =>
)
you need to access the individual properties to get to the actual data.
$result=$query->result();
print_r($result);
should do it
Had this issue before - basic problem is that if the Query returns no result set then $query->result_array() == NULL
NULL is not a list and can't be processed in a foreach.
I have found that checking for this condition solves this issue.
From your question, you are getting the result as a pure array (result_array) but printing the data as object ($row->first).
result() or row() is returning in object but result_array is returning in array.
change your view part like,
foreach($query->result_array() as $row) {
echo $row['first'];
}
or if you want to use an object then,
foreach($query->result() as $row) {
echo $row->first;
}
Generating Query Results in Codeigniter

Resources