Having Trouble Generating A Simple Select Dropdrown In CakePHP 2.0 - cakephp

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;

Related

Option usage configureListFields in SonataAdminBundle

unfortunately documentation doesn't cover how to use options available in configureListFields ListMapper when you add fields to the list.
This is my basic code
$listMapper
->add('myField', null, array(
'label' => LabelHelper::LABEL_MY_FIELD,
'code' => // what should I put here ... $this->methodName() is not working
))
I want to use 'code' option (docs - section 7.2.1), because I would like to customize just one filed final display. I don't want to rewrite the row template.
As stated in the code section I've tried simple method that returns string, but nothing happened in the list view (I've cleared cache etc.).
Answer is simple. You just put method name without brackets
$listMapper
->add('myField', null, array(
'label' => LabelHelper::LABEL_MY_FIELD,
'code' => 'methodName'
))
Method should be stored in corresponding Entity class

Settings page and multi edit rows

I would like to make a configuration page like this for my CMS
http://img4.hostingpics.net/pics/72087272ok.png
I want this page (admin/settings/index) gets me the various settings (ID 1 to 21) came to my table and in case of change, I can, in this form, do an update
After 3 days of not especially fruitful research I found something. I put in my SettingsController:
admin_index public function () {
if (!empty($this->data)) {
$this->Setting->saveAll($this->request->data['Setting']);
} else {
$this->request->data['Setting'] = Set::combine($this->Setting->find('all'),
'{n}. Setting.id', '{n}. Setting');
}
}
and my view:
<?php
echo $this->Form->create('Setting', array ('class' => 'form-horizontal'));
foreach($this->request->data['Setting'] as $key => $value) {
echo $this->Form->input('Setting.' . $key . '.pair');
echo $this->Form->input('Setting.' . $key . '.id');
}
echo $this->Form->end(array('class' => 'btn btn-primary', 'label' => 'Save',
'value' => 'Update!', 'div' => array ('class' => 'form-actions')));
?>
he is recovering well all my information, I can even update. The trouble is that I do not really know how I can do to get the same result as my first screenshot. He puts everything in the textarea while in some cases I want the checkbox and / or drop-down.
please help me or explain to me how to make a page that retrieves configuration information from my table and allows me to edit without use an address like admin/settings/edit/ID
My table settings is something like this here
http://img4.hostingpics.net/pics/724403settings.png
If you are asking about how to format the output to produce a "settings" page, you will need to do it manually or add some kind of metadata to your table schema, such as "field_type".
Now Cake will automatically generate a checkbox for a boolean or tiny int, for example, but as you have varchars, ints, texts and so on, this won't work for you (pair is presumably varchar or text to allow for the different possible values.
You cannot really automagically generate the outputs you want without telling Cake.
One option would be to add a field_type to the table, so:
key value field
site_name My Site input
site_online 1 checkbox
meta_desc [some text] textarea
and something like
foreach($settings as $setting) {
echo $this->Form->{$setting['field']}($setting['key'],
array('value' => $setting['value']));
}
might do what you are after.
Or if($key=='site_name') // output a textbox
but this is not exactly ideal.

Change individual labels with automagic form helper on a 'multiple' => 'checkbox'

I have the following line in my add/edit Course views:
echo $this->Form->input(
'Competency',
array(
'label' => 'Which competencies does this course address?',
'multiple' => 'checkbox'
)
);
(Modelled as Course hasMany Competencies)
I'd like to customise the label that gets output for each competency but can't seem to find a way of doing so - the 'label' field seems to work as a group heading rather than changing the label for the individual checkboxes.
(What I'm ideally after is rather than just displaying Competency.name I can display Competency.name plus the Competency.code as the label)
NB I thought about changing the displayField but that would change it everywhere and it's only here I'd like to be different.
Create a virtual field [details]:
//in your Competency model
var $virtualFields = array(
'name_code' => 'CONCAT(Competency.name, " ", Competency.code)'
);
Then in your controller, before retrieving the data, set your displayField to your just-created virtual field:
//in your controller prior to the find
$this->Competency->displayField = 'name_code';
Since you're setting displayField in the controller, it doesn't set it permanently, so no need to set it back, but if you're doing more finds immediately after this, you can always set back to name if you want.

CakePHP pre populate select box based on existing selection on related model

I am trying to pre-populate this State select box with the existing selection when editing records.
in a related model.
plan_details/view.ctp
echo $this->Form->input('State',array('empty' => false,'options' => $state));
plan_details_controller view function:
$state = $this->PlanDetail->Plan->State->find('list');
$this->set(compact('state', $state));
debug($state);
Array output in view.ctp (as expected):
Array
(
[1] => Oregon
[2] => Washington
)
My select box above defaults to 1 in the array. I need it to default to the already existing selected value.
For example, when I added a record and a selected Washington(2), then when viewing my edit screen, the pre-selected value should be Washington with value 2.
I'm stuck and cracking at this for a while. Any idea what I am doing wrong?
If you are editing a page you need to set $this->data to auto populate it. Within your edit action
if (empty($this->data)) {
$this->data = $this->PlanDetail->read(null, $id);
}
This should read in the record then ( note the plural )
$states = $this->PlanDetail->Plan->State->find('list');
$this->set(compact('states'));
In your form it should read
echo $this->Form->input('Plan.state_id',array('empty' => false));
I find your approach a bit confusing but the above should work. Is this form for editing a Plan, PlanDetail or both

CakePHP AutoComplete Question

I am working on a book review application and I am using autoComplete to search for titles in when creating a review. The review model has an associated book_id field and the relationship is setup as the review hasOne book and a book hasMany reviews.
I am trying to pass the Book.id (into the book_id field), but I want to display Book.name for the user to select from. With the default setup (accomplished via CakePHP's tutorial), I can only pass Book.name. Is it possible to display the name and pass the id?
Also, I am passing it via the following code in the create() action of the review controller:
$this->data['Review']['book_id'] = $this->data['Book']['id'];
Is that the proper way to do it in CakePHP? I know in Ruby on Rails, it is automatic, but I can't seem to make it work automagically in CakePHP. Finally, I am not using the generator because it is not available in my shared hosting environment... so if this is the wrong way, what do I need other than associates in my models to make it happen automatically?
Thanks for the help and I promise this is my question for awhile...
UPDATE- I tried the following, but it is not working. Any ideas why?
function autoComplete() {
$this->set('books', $this->Book->find('all', array(
'conditions' => array(
'Book.name LIKE' => $this->data['Book']['name'].'%'
),
'fields' => array('id','name')
)));
$this->layout = 'ajax';
}
The problem is that when I use the code above in the controller, the form submits, but it doesn't save the record... No errors are also thrown, which is weird.
UPDATE2:
I have determine that the reason this isn't working is because the array types are different and you can't change the array type with the autoComplete helper. As a workaround, I tried the follow, but it isn't working. Can anyone offer guidance why?
function create() {
if($this->Review->create($this->data) && $this->Review->validates()) {
$this->data['Review']['user_id'] = $this->Session->read('Auth.User.id');
$this->Book->find('first', array('fields' => array('Book.id'), 'conditions' => array('Book.name' => $this->data['Book']['name'])));
$this->data['Review']['book_id'] = $this->Book->id;
$this->Review->save($this->data);
$this->redirect(array('action' => 'index'));
} else {
$errors = $this->Review->invalidFields();
}
}
FINAL UPDATE:
Ok, I found that the helper only takes the find(all) type or array and that the "id" field wasn't passing because it only applied to the autoComplete's LI list being generated. So, I used the observeField to obtain the information and then do a database lookup and tried to create a hidden field on the fly with the ID, but that didn't work. Finally, the observeField would only take the characters that I put in instead of what I clicked, due to an apparent Scriptaculous limitation. So, I ended up going to a dropdown box solution for now and may eventually look into something else. Thanks for all of the help anyway!
First of all, $this->data will only contain ['Book']['id'] if the field exists in the form (even if it's hidden).
To select something by name and return the id, use the list variant of the find method, viz:
$selectList = $this->Book->find('list', array(
'fields' => array(
'id',
'name'
)));
$this->set('selectList', $selectList);
In the view, you can now use $selectList for the options in the select element:
echo $form->input('Book.id', array('type' => 'hidden'));
echo $form->input('template_id', array(
'options' => $selectList,
'type' => 'select'
));

Resources