How to disable optgroup from combobox - cakephp

HI,
I want to remove this optgroup from my cakephp combobox. How can i do this?
Regards,
karn

Well, just don't add it if you don't want it. Basically the select boxes are filled with single array like this:
$options = array(1=>'Text 1', 2=>'Text 2', ...);
Having optgroup require two dimensional array like:
$options = array(
'Opt Group 1'=>array(
1=>'Text1',
2=>'Text2',
...
),
'Opt group 2'=>array(
1=>'Text1',
2=>'Text2',
...
)
);
Most likely in your controller instead of:
$this->set('options', $this->YourModel->find('list'));
you are using
$this->set('options', $this->YourModel->find('all'));
Notice the parameter in the find function. Take a look in that article for Options parameter

Related

CakePHP 3 dropdown values

I have a dropdown that only shows the id of a table row.
I like to show multiple values from that table to the user in the dropdown.
Do I need to add an array with columnames in the variabele from the controller or something like that?
Edit:
In function add I've this variable
$addresses = $this->Users->Addresses->find('list', ['limit' => 200]);
In the view
echo $this->Form->input('address_id', ['options' => $addresses]);
I only want to show the values address_id, city, street, zipcode in the dropdown.
Use a virtual field:
in your Addresses entity file you can do
function _getFullName()
{
$ret = $this->city.' '.$this->street.' '.$this->zipcode;
return $ret;
}
and then in your controller you can do
$addresses = $this->Users->Addresses->find(
'list',
['limit' => 200, 'valueField' => 'full_name']
);
The Form::Select() only needs one array for it's values to display. It uses the index to define the value and the the value of the array to display.
What you could do is fetch the needed columns from the table and use concat() to create one string from those two columns which you then put into an array.
Alternatively you could take a look at the displayField() in the Table class which accepts an array. The downside of doing this is when you use $table->find('list'); you'll get something along the lines of Sevvlor;1 which is probably not what you want.

CakePHP hasMany checkbox

I have two tables: Ingredients and Customers. The relationship between them is that Customers hasMany Ingredients. By default when doing the cakebake using the console, the only way to change them is by assigning an ingredient to the customer in the Ingredients page. However, I want to have in Customers page a checkbox list of Ingredients that can be assigned. Is it possible to do this? If yes, how?
edit:
What I have done until now is that I add this code to my add.ctp:
echo $this->Form->input('Ingredient',
array('label'=>'',
'type'=>'select',
'multiple'=>'checkbox',
'options'=>$ingredients));
However, it gives me "Undefined variable: ingredients" error when I tried to open the add view.
You want and need a HABTM relationship. Different customers can access and use the same ingredients. Look at the docs here yours would be very similar to different Posts using same Tags.
If you are getting this error:
"Undefined variable: ingredients"
It sounds like you haven't declared this variable in your controller, and set it so that the view can use it. Without knowing your code, you would probably need do do something like this (please note I am guessing what your application structure looks like and have not tested this code).
CustomersController.php
// The controller action for your view
public function view() {
// Get the ID and name of all your ingredients
$ingredients = $this->Ingredient->find('all', array(
'fields' => array('id', 'name'),
'order' => 'name',
'recursive' => -1
));
// We will use this array to store all the HTML select options
$ingredientOptions = array();
// Loop through all the ingredients and add them to the select
// options in a format that is suitable for CakePHP to use in
// the view to build your HTML select menu.
foreach ($ingredients as $i) {
$ingredient = $i['Ingredient'];
$ingredientOptions[$ingredient['id']] = $ingredient['name'];
}
// Make the variable available to the view
$this->set('ingredients', $ingredientOptions);
}

cakephp sum() on single field

I have searched high and low on how to just get a total of a field called points. I just need one total figure but the best I can get is a list of records from the Points table with the associated records from the Members.
$totalPoints = $this->Member->Point->find('all', array(
array('fields' => array('sum(Point.points) AS Point.ctotal'))));
Why not using virtualFields as documented and suggested by the docs?
http://book.cakephp.org/2.0/en/models/virtual-fields.html
$this->Member->Point->virtualFields['total'] = 'SUM(Point.points)';
$totalPoints = $this->Member->Point->find('all', array('fields' => array('total')));
This is way cleaner.
Also note the double array you got there in your $options array (...find('all', array(array(...). And how I used only a single/flat array.
This is the reason why your SUM() call as fields does not work.
mark's answer above is right. I just want to add that you can do this:
$totalPoints = $this->Member->Point->find('first', array(
array('fields' => array('sum(Point.points) AS Point__ctotal'))));
$totalPoints will be have this:
$totalPoints['Point']['ctotal']
Another clean way without outputting an array
$this->Member->Point->virtualFields = array('total' => 'SUM(Point.points)');
$total = $this->Member->Point->field('total');

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.

Having Trouble Generating A Simple Select Dropdrown In CakePHP 2.0

I'm pretty sure I don't have this problem in Cake 1.3, but:
I have a form input based on an is_live db field (containing 1 or 0 as its value).
The following creates a correctly populated checkbox:
echo $this->Form->input('is_live', array('label'=>'Status'));
However, the following does not seem to create a correctly populated dropdown (the first option is always selected, even though selecting an item and submitted the form does update correctly):
echo $this->Form->input('is_live', array(
'label'=>'Status', 'type'=>'select' , 'options'=>array(1=>'Live', 0=>'Pending')
));
Is there anything simple I can do to make the dropdown populate based on the value of is_live in CakePHP 2.0? Or is there a workaround?
I had the same issue with using 1 and 0 before.
My solution is to use the following
$options = array(1=> 'Live', 0=>'Pending');
echo $this->Form->input('YourModel.is_live',
array(
'options' => $options,
'label' => 'Status',
'selected' => intval($defaultValue), // make sure you set a default value
)
);
Can you change the content length of that field? If you can, change it to 2. This will get around the problem.
ALTER TABLE `your_table` CHANGE `is_live` `is_live` TINYINT(2) NULL DEFAULT NULL;

Resources