Checking boxes with associated with HABTM in CakePHP - cakephp

So, I have models associated via HABTM. In one view, I'm using checkboxes to save associations. I can save data no problem. But what I can't determine is how cleanly check boxes of pre-existing associations, say, when I go onto an Edit page and want to edit associations.
In this case, I have a User model and a Survey model.
Here's the code for my view:
foreach ($users as $user) {
echo $this->Form->input('User.User.', array('type' => 'checkbox', 'value' => $user['User']['id'], 'hiddenField' => false, 'label' => false )) . ' ' . $user['User']['username'];
}
There must be a way to simply mark boxes as selected where appropriate.

Related

How to pull data for and display an Association as a multiple-select checkbox in CakePHP 3?

I'm setting up a user access model with Roles in a separate table, and linked to Users by a UserRoles table.
I currently have the following in Model/Table/UsersTable.php:
$this->belongsToMany('Roles', [
'through' => 'UserRoles'
]);
and the following in Model/Table/RolesTable.php:
$this->belongsToMany('Users', [
'through' => 'UserRoles'
]);
and the following in Model/Table/UserRolesTable.php:
$this->belongsTo('Users', [
'foreignKey' => 'user_id'
]);
$this->belongsTo('Roles', [
'foreignKey' => 'role_id'
]);
I have 3 different roles created, 'viewer', 'creator', and 'administrator'. I've successfully set privileges based on user types. Where I am getting stuck is adding roles to a user via an association form.
Right now I have given Administrator users the ability to edit user information. This works for basic information that I have in the Users table, but I can't figure out how to set up the form field for the associated Role. I would like it to be a checkbox where the Administrator can select each privilege for the user.
I'm currently doing this, which is not giving me what I want:
echo $this->Form->input('Users.role', ['type' => 'checkbox']);
This is giving me a single checkbox with the label "Role". I want to pull each row from my Roles table and list them all as options.
I have a few questions relating to this:
1) This seems really elementary but I'm just not finding it clearly stated. What code do I need in my UsersController to pull the list of all Roles? (not just those associated with the current User, but all objects in the Roles table.)
2) What form input code do I need to display checkboxes with all possible Roles, and show the current user privileges (in my UserRoles table) as already checked off? I think I need something like this in my form:
echo $this->Form->select('User.Role', $options, ['multiple' => 'checkbox']);
...but I can't tell what $options should be, and how to set already-selected values.
I am currently pulling Roles with my User object to be edited:
$user = $this->Users->get($id, [
'contain' => ['Roles'],
'Users.id' => $this->Auth->user('id')
]);
...but I'm having trouble converting it into a checkbox form selection.
Thanks so much.
In order to create a list of checkbox out of some options, you first need to send the options from the controller:
$this->set('roles', $this->Users->Roles->find('list'));
Then, in your template add a multiple => checkbox input:
echo $this->Form->input('roles', ['multiple' => 'checkbox', 'options' => $roles]);
It is not necessary to prefix your input names with User. just name your inputs as the properties of a User entity.

CakePHP how to change select box to checkbox ?

I have two tables store and users.They have one to one relation.After bake store I get user list in a select box.I want to make all selection to check box because farther I want to select multiple users for one store.Now I just need to convert this select box to check box.
I have tried
<?php
echo $this->Form->input('mad_stores_id');
?>
To
<?php
echo $this->Form->checkbox('mad_stores_id');
?>
But this is giving me only one check box.I need to display all option which have given in selection box.
Here is the controller find methods
$users = $this->UserStoreSelection->Users->find('list',array('fields' => array('id','username')));
How can I show all select option in check box ?
If you're relationship is base on One to One, then you shouldn't allow end users to select multiple users for store. That would be a hasMany relationship.
Anyway, here you go
<?php echo $this->Form->input('mad_stores_id', array(
'multiple' => 'multiple')); ?>
Edit: if its multiple checkboxed yo want, then its the following:
<?php echo $this->Form->input('mad_stores_id', array(
'multiple' => 'checkbox')); ?>

How can i save multiple checkbox value those are checked in cakephp hasMany relation?

Suppose I have 3 tables property_details, property_feature_details, property_feature_relations.
PropertyDetail Model has the hasMany relation with PropertyFeatureRelation.
AT the time saveing my data array looks like that:
$this->data = array('PropertyDetail' => array('name'=>'xyz'),'PropertyFeatureRelation' => array('0' => array('feature_id'=>1),'1' => array('feature_id'=>5),'2' => array('feature_id'=>0),'3' => array('feature_id'=>0)));
Those feature_ids values are coming out from the checkboxes. Those are checked they are contains ids value and non checked are having 0 value. But in the child table saves all the data zero and non zeros.
Actually I want to save those checkboxes values which are only checked. Please do not provide any manual controller logic. Help me.
I am explaining you with my example which is having question ,answer and its survey
you can use multiple checkbox syntax as below:
$answers=array();
$tmp_ans=$question['Answer'];
for($j=0;$j < count($tmp_ans);$j++)
$answers[$tmp_ans[$j]['id']]=$tmp_ans[$j]['answer'];
echo '<li>';
echo $this->Form->input('Answer', array(
'type' => 'select',
'name'=>'Answer['.$question['Question']['id'].']',
'class'=>'test',
'label'=>false,
'multiple' => 'checkbox',
'options' =>$answers ,
));
echo '</li>';
after that you will have only those answers which you have checked others will not come in array of post so to save data you can use below syntax
if(!empty($this->data['Answer'])){
foreach ($this->data['Answer'] as $key=>$val):
if(!empty($val)):
if(is_array($val)):
foreach ($val as $ans):
$post_data['UserAnswer']['id']='';
$post_data['UserAnswer']['survey_id']=$id;
$post_data['UserAnswer']['user_id']=$this->Auth->user('id');
$post_data['UserAnswer']['question_id']=$key;
$post_data['UserAnswer']['answer_id']=$ans;
$this->UserAnswer->save($post_data['UserAnswer']);
endforeach;
else:
$post_data['UserAnswer']['id']='';
$post_data['UserAnswer']['survey_id']=$id;
$post_data['UserAnswer']['user_id']=$this->Auth->user('id');
$post_data['UserAnswer']['question_id']=$key;
$post_data['UserAnswer']['answer_id']=$val;
$this->UserAnswer->save($post_data['UserAnswer']);
endif;
endif;
endforeach;
}

How to diaplay other tables attributes in single view form and also insert values in corresponding tables

I have two tables QBQuestion(Questionid,Question,OptionId) and Option(OptionId,Option). I want to display option form on view form of QBQuestion? I want to create multiple choice question. i.e.for single question we can add multiple options.For such purpose i want to cretae option field with add button si that when we click add button,we can insert more options and also want to display that all inserted options in table using grid.
So what should i do? please help me....
1) Add relations in the model for this two stuff.
public function relations() {
return array(
'valOptions' => array(self::BELONGS_TO, 'Option', 'OptionId'),
);
}
2) Use lazy loading in CGridView.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => new CActiveDataProvider('QBQuestion'),
'columns' => array(
'Questionid',
'Question',
'valOptions.Option',
),
));
I think that's what you need.

CakePHP: Retrieve multiple records from a single model to be edited in one form

Good day,
I have a model called ProjectRequirement which belongsTo Project, so Project hasMany ProjectRequirements
When I created the ProjectRequirement entries, I made use of this method:
<?php
echo $this->Form->inputs(array(
'legend => false,
'fieldset' => false,
'ProjectRequirement.1.description' => ...
'ProjectRequirement.2.description' => ...
'ProjectRequirement.3.description' => ...
));
?>
I did this so that I could make use of the saveMany() method to save multiple records at the same time. However, when I want to edit these records again on the same form, I cannot see, to be able to do it. I have kept the same field naming structure and tried to set the data as follows:
<?php
$this->request->data = $this->ProjectRequirement->find('all', array('conditions' => ...));
?>
A pr(); shows that the records are being returned, but they are not populating the form fields. If I remove the numbers and just have a single field like this:
<?php
echo $this->Form->inputs(array(
'legend => false,
'fieldset' => false,
'ProjectRequirement.description' => ...
));
?>
It works fine. How can I set the data so that multiple records from ProjectRequirement are set on multiple inout fields? Or can't I?
To reiterate: I do NOT have a problem saving multiple records, I have a problem retrieving multiple records to display.
Regards,
Simon
When you create the form to SAVE many fields, you name the fields like this:
ProjectRequirement.1.name...
ProjectRequirement.2.name...
ProjectRequirement.3.name...
...
However, when retrieving data, it is not the same scenario, and the general rules of arrays and how indexing works are applied here. So simply changing my fields to be like this:
ProjectRequirement.0.name...
ProjectRequirement.1.name...
ProjectRequirement.2.name...
...
Worked because I was only testing with one record in the database, and that row would have been at index 0, not 1.

Resources