save image in database from session in cakephp - php

I am working in multistep form to submit data to database as well image,But i am stuck ,
View:
<?php
echo $this->Form->file('File',array(
'placeholder'=>'Upload Profile',
'accept'=>''
))
?>
Here is Controller code after user completed All step:
<?php
if (!empty($currentSessionData['Post']['File']) &&
is_uploaded_file($currentSessionData['Post']['File']['tmp_name'])) {
$fileData = fread(fopen($currentSessionData['Post']['File']['tmp_name'], "r"),
$currentSessionData['Post']['File']['size']);
$this->request->data[$table_name]['image_name'] = $currentSessionData['Post']['File']['name'];
$this->request->data[$table_name]['image_type'] = $currentSessionData['Post']['File']['type'];
$this->request->data[$table_name]['image_size'] = $currentSessionData['Post']['File']['size'];
$this->request->data[$table_name]['image_data'] = $fileData;
}
i am getting input using Post Model and saving data to other table which i have loaded using loadModel() method in cakephp.
Now here $currentSessionData is Session data where i am saving user's data of all step once user completed all steps then it comes to the save part in controller.
And $tablename is new table in phpmyadmin where i want to save image_name,type,size and data.
Now proble is is_uploaded_file() method is returning false ?
$currentSessionData returns :
'File' => array(
'name' => 'anonymous.png',
'type' => 'image/png',
'tmp_name' => 'C:\wamp\tmp\php2D7E.tmp',
'error' => (int) 0,
'size' => (int) 983
),
anonymous.png is selected file.
Any Help please I'm Stuck? Thanks in advance.

Related

How to fix status code 303 in codeigniter?

I am working on CI 3 when I post form data and insert into database then form a value that has been successfully save and I am also getting the last insert through $insert_id = $this->db->insert_id() following example as mention below:
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'password' => sha1($this->input->post('cpassword')),
'confirm_id' => sha1($this->input->post('email')),
'created_at' => date('Y-m-d H:i:s')
);
$success = $this->db->insert('instructor', $data);
$insert_id = $this->db->insert_id($success);
if($success == true)
{
$instructorData = $this->instructor_m->get_instructorData($insert_id);
$this->session->set_userdata('InstloginuserId',$instructorData->instructorID);
redirect(base_url('board'));
}
else
{
$this->session->set_flashdata('message', $this->lang->line('fail'));
redirect(base_url('register'));
}
If success condition is true then it redirects to board page but it throws status code: 303 I don't know why? Please help me to solve this.
The Session Library has been completely re-written in CodeIgniter 3 and now comes with a bunch of new features, but that also means that there are changes that you should make.
Read the Step 6: Update your Session library usage from CodeIgniter | Upgrading from 2.2.x to 3.0.x.

CakePHP: Check for line item in iframe before submitting parent form

The app I'm working on (an order form) allows the user to enter multiple sub-records within an iframe. These sub-records are joined to the main record via a foreign key.
main_records line_items
----------- ----------
id int(11) PK etc. id int(11) PK etc.
main_record_id (FK)
I need the app to check whether at least one line item exists within this iframe before form submission. I would like to take advantage of the $validate functionality within the model, but I'm unsure how to proceed. Here's what I've tried in the Main model:
App::uses('AppModel', 'Model', 'LineItem');
public $hasMany = array(
'LineItem' => array(
'className' => 'LineItem',
'foreignKey' => 'main_record_id',
'dependent' => false
)
);
public $validate = array(
'main_record_id' = array(
'allowEmpty' => false,
'rule' => 'checkForLineItem',
'message' => 'You must enter at least one line item!'
)
);
//Check to make sure there is at least one line item before saving changes/submitting for approval
function checkForLineItem($id) {
$lines = $this->LineItem->find('all', array(
'fields' => array('LineItem.main_record_id'),
'conditions' => array('LineItem.main_record_id'=>$id, 'LineItem.deleted_record'=>0))
);
if(!empty($lines)) {
return true;
} else {
return false;
}
}
I also track whether the line item has been deleted. If it has, then it is not added to $lines.
I know I can accomplish this in the Controller, but as far as I know, that would require the form to post, and the user would lose any changes upon postback (I haven't yet implemented jQuery on this form). Am I on the right track with how to do this? What changes should I make to get this to work?
Your code looks about right, but validation indeed happens in form submit. If you want to check it prior to that you have to do in JavaScript (jquery). E.g. create a controller action that return if there are existing line items for given main record id and call it via AJAX.

Get data from database not from form

I'm having a problem when trying to compare a data database, with data input by form.
I have a "pedido", this has many "items". I need compare "item" by "item" if this is modifies in the form.
Then I need get original data from database and data modified from form.
The problem is when i try get original data from database.
Always get the data modified by form.
How Can i get the original data from database after the submit the form?
NOTE: i have tried get PedidoAuxiliar before and after HandleRequest.This doesn´t work!
UPDATE CODE: Input how compare the items
This is my controller editAction:
public function editarAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$pedido = $em->getRepository('PedidosBundle:Pedido')->find($id);
//$pedidoAuxiliar = $em->getRepository('PedidosBundle:Pedido')->find($id);
$formulario = $this->createForm(new PedidoType(), $pedido, array(
'action' => $this->generateUrl('my_routing', array('id' => $id)),
'attr' => array(
'novalidate' => 'novalidate'
),
'method' => 'POST',
));
$formulario->handleRequest($request);
if($formulario->isValid()){
$pedidoAuxiliar = $em->getRepository('PedidosBundle:Pedido')->find($id);
foreach($pedido->getArticulos() as $articulo){
foreach($pedidoAuxiliar->getArticulos() as $articuloAuxiliar){
if($articuloAuxiliar->getId() == $articulo->getId()){
if($articuloAuxiliar->getCantidad() == $articulo->getCantidad()){
//Some code...
To get data from db u can use EntityManager::refresh($entity) its overwrite entity data using db. So u must use data from form, not entity to compare.
But u always can ust Doctrine to check changes eg: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/change-tracking-policies.html
because Doctrine hold info about old and new value, but is not so easy to get (outside listeners)
I have found a solution. I have created a new connection entity manager in my config.yml.
Now I call my entity data from my new entity manager and i get datas from database! Thanks!

Yii2: How to show checked values in CheckboxList

I want to show checked values in my checkboxlist in Yii 2.0. Following is my code:
Main Array:
<?php
$featureArr = array(
'Change Requester' => 'Change Requester',
'Clone Request' => 'Clone Request',
'Suspend Request' => 'Suspend Request',
'In-Process Requests Open in Edit Mode' => 'In-Process Requests Open in Edit Mode',
'Allow On-the-Fly Notifications' => 'Allow On-the-Fly Notifications',
'Additional Comments Field' => 'Additional Comments Field',
'Do Not Validate Draft Requests' => 'Do Not Validate Draft Requests',
'(Web-Only) Allow File Attachments' => '(Web-Only) Allow File Attachments',
);
Getting data from table to display checked items:
$formFeatureModel = FormFeatureForm::find()->where("form_id=" . $model->id)->all();
$checkedFeatureArr = array();
foreach ($formFeatureModel as $data) {
$checkedFeatureArr[$data->feature] = $data->feature;
}
?>
Checkbox field
<?= $form->field(new FormFeatureForm, 'feature')->checkboxList($featureArr, ['class' => 'featureCls']) ?>
I am getting checked item in $checkedFeatureArr from database. Now I just want to show checked items. So, Where should I pass $checkedFeatureArr array? Getting stuck in this.
Any help would be appreciated.
When using it with model, you should fill feature model property with selected values instead.
$model->feature = $checkedFeatureArr;
Then it will be checked automatically.
Additional tips:
It's better avoid concatenation in SQL, replace ->where("form_id=" . $model->id) with ->where(['form_id' => $model->id]).
It's better create ActiveForm before rendering ActiveField.

CodeIgniter Get Value From Form Field

I know this is a stupid question but after messing around, I am confused what is the right way of doing.
Below are my code :
View registration_form.php :
$input_data = array(
'name' => 'user_name',
'id' => 'user_name',
'value' => set_value('user_name'),
'maxlength' => MAX_CHARS_4_USERNAME
);
Controller register.php:
$this->load->model('user_model');
$this->user_model->create_user( 'customer', array() );
Form Validation Config :
$config['customer_creation_rules'] = array(
array(
'field' => 'user_name',
'label' => 'USERNAME',
'rules' => 'trim|required|alpha|strtolower'
)
);
Model user_model.php :
public function create_user( $role, $insert_array = array() )
{
// The form validation class doesn't allow for multiple config files, so we do it the old fashion way
$this->config->load( 'form_validation/administration/create_user/create_' . $role, TRUE );
$this->validation_rules = config_item( $role . '_creation_rules' );
$form_username = $this->config->item($role . '_creation_rules', 'field');
echo $form_username;
}
What I want to do right now is to check the username that the user input and auto add a number to the input username before inserting into database.
Initially I thought of getting the inputted username from the Form Validation, after messing around, I can't get the value no matter what I have tried.
Am I doing it wrongly? Do I just get from $_POST instead?
Hope you guys can help me out on this. Thanks in advance!
This shows the full list (waaaay at the bottom): http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
I don't see that one there, but it says you can use any PHP function that requires only one parameter so that should work, but you'll have to look deeper into the docs to figure out why it isn't.
I don't think you can set rules for a form, you have to set them per variable, as the docs show:
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]');

Categories