How to add multiple entries of same object into session in cakePHP - cakephp

I have a addstudent.ctp file to add students
echo $this->Form->create('Student');
echo $this->Form->input('FirstName');
echo $this->Form->input('LastName');
echo $this->Form->end('Register');
echo $this->Form->create('Address',array('controller'=>'addresses','action'=>'addaddress'));
echo$this->Form->end('NextAdressDetails',array('controller'=>'addresses','action'=>'addaddress'));
after clicking on it is going to add method of StudentsController.it has to add student object to session and should redirect to same page.like this i can able to add as many students as possible ,so my problem is to how to add the multiple students into session whenever clicks on register button.

$students = $this->Session->read('Students');
if (!$students) {
$students = array();
}
$students[] = /* data */;
$this->Session->write('Students', $students);

Related

Multiple values in cakephp session

I would like to store multiple values in cakephp session where some value come through database and some from manually
I have created a session in controller which is like this.
$this->Session->write('Cart.'.$count,$this->Product->findById($id[0], array('id','category','name','price'))); //controller
And I have wrote this code in view page
foreach( $this -> Session -> read(Cart) as $value)
{
echo $value['Product']['id'];
echo $value['Product']['category'];
echo $value['Product']['name'];
echo $value['Product']['price'];
}
by using this line i can print all information id,category,name,price which is stored from database in session
but i want to add one more variable which is $quantity and it will manually not coming from database so how can i add this field in session and how can i print this.
Thanks in advance
When you write session do somthing like given below:
$sessionInfo = $this->Product->findById($id[0], array('id','category','name','price'));
foreach($sessionInfo as $key=>$value){
$sessionInfo[$key]['Product']['quantity'] = $count;
}
$this->Session->write('Cart',$sessionInfo); //controller
View session data:
foreach( $this->Session->read('Cart') as $value)
{
echo $value['Product']['id'];
echo $value['Product']['category'];
echo $value['Product']['name'];
echo $value['Product']['price'];
echo $value['Product']['quantity'];
}

CakePHP remove empty relations

I have relation Holiday hasMany Place (Place belongsTo Holiday).
I prepare some form in Holiday view:
echo $this->Form->input('name');
echo $this->Form->input('Place.0.name');
echo $this->Form->input('Place.1.name');
echo $this->Form->input('Place.2.name');
Now, when I add Holiday with 2 places, I can't save because the one is empty.
How remove empty record in model?
Loop through them, if it's empty, unset it.
foreach ($data['Place'] as $key => $place) {
if (empty($place['name'])) {
unset($data['Place'][$key]);
}
}
Not sure about the paths, just update them accordingly to what your post data looks like.

blackhole cakephp 2 associated entities

My Goal:
Reuse of a contact form to be related to several different entities I call "Parents" ie Group has contact information, Member has contact info etc....
The way I tried doing it was:
1. Creating a view file for contact, named "form.ctp" which doesn`t create a new form, nor submits, just echo's the contact's fields.
2. Calling this file using requestAction
My Problem:
The form's _Token get crumbled.
Parent add.ctp example
<?php echo $this->Form->create('Group');?>
<fieldset>
echo $this->Form->input($field_prefix.'contact_id',array('type'=>'hidden'));
<?php echo $this->requestAction(array('controller' => 'contacts', 'action' => 'form'), array('named' => array('index'=>'0','parent'=>'Group',
'fields'=>array(
'email'=>array('value'=>'xx#yy.com','hidden'=>1)
))));
inside the form.ctp I have:
//Associated Model
echo $this->Form->input('Contact.0.city',array('type'=>'hidden'));
echo $this->Form->input('Contact.0.postcode');
echo $this->Form->input('Contact.0.phone');
echo $this->Form->input('Contact.0.cellphone');
echo $this->Form->input('Contact.0.email',array('value'=>""));
echo $this->Form->input('Contact.0.id',array('type'=>'hidden'));
?>
Looking at the HTML source code that is generated, I see that whether I use the request action or just copy the contect of the form.ctp into the "Parent's" add file, I get the same HTML result.
HOWEVER!!! when I use the form.ctp Action Request, I get the blackhole, the tokens are being messed up!!!
Any Ideas?
Thanks in advance
Orly
If your problem is solely reusing a form, you can use the form as a Element, and then you could call it multiple times, substituting in the exact values you need.
As for SecurityComponent, I would recommend (at least as a temporary fix) disabling SecurityComponent for that specific action by using $this->Security->unlockedActions(); in your controller's beforeFilter()

Cakephp: Designpattern for creation of models in hasMany relationship

I have two Models: Car and Passenger.
Car hasMany Passenger
Passenger belongsTo Car
I managed to create add functionality for each model and managed to resolve the hasMany relationship between them.
Now I'm trying to create a addCar function and view that allows the user to create a new car and automatically generate Passengers.
I thought of something like this
The view asks the user enter the car information
The view has some field that allows to temporarily add new passengers and remove remove them
When the user saves the new car, the entities for the passengers are created, the entity for the car is created and the passengers are linked to the car.
If the user decides to cancel everything, the DB remains unchanged.
Now my question is: What is the best way to do this? Is there a pattern / tutorial to follow for such a entity and associated subentity creation?
To clarify: The passengers associated with each car do not exist prior to the existence of the car.
Use the following code in the view views/passengers/add_cars.ctp:
<?php
echo $this->Form->create('Car');
echo $this->Form->input('name');
echo $this->Form->input('model');
echo $this->Form->input('Passenger.0.name');
echo $this->Form->input('Passenger.0.age');
echo $this->Form->input('Passenger.1.name');
echo $this->Form->input('Passenger.1.age');
echo $this->Form->end();
?>
This basically starts off with the necessary fields to input the car information. Then, you can start a loop with a general format of Passenger.{$i}.name to add passengers dynamically when a new car is added. You can use jQuery to create "add" and "remove" buttons that will add or remove rows of Passenger form entries.
In your passengers_controller.php (assuming this is the controller you want add_cars), use the saveAll function:
function add_cars() {
if (!empty($this->data)) {
if ($this->Passenger->Car->saveAll($this->data)) {
$this->Session->setFlash('Success');
$this->redirect('/');
} else {
$this->Session->setFlash('Error');
}
}
}
Hope that helps.

How to save multiple individual records for a single model

I have a model(friends), where user can export his friend's info from facebook. I want save friends info(id and name) into the mysql database.
I am having trouble creating a form for the same using form helper as i don't know the exact number of friends for each user.
Is there a easier way to save user's friends into the database? (save multiple records for the same model)
Basically you should know how much is the number of friends for the current user, right?
If so, do a loop with following in your view:
echo $this->Form->create('User');
for($i=0;$i<$number_of_current_user_friends;$i++){
echo $this->Form->input('Friend.'.$i.'user_id', array('value'=>$current_user_id));
echo $this->Form->input('Friend.'.$i.'friend_name', array('value'=>$friends[$i]['name']));
echo $this->Form->input('Friend.'.$i.'fb_id', array('value'=>$friends[$i]['fb_id']));
}
echo $this->Form->end('Submit');
then in the controller save it with:
$this->Friend->saveAll($this->data);

Resources