dropdown select in cakePHP - cakephp

I am using CakePHP 1.2. I have a person model that hasMany 'Document'. When I edit a document, the select box for the owning person appears (echo $form->input('person') where person has been defined in the documents_controller like this:
$allPeople = $this->Document->Person->find('list', array('fields' => array('first_name')));
$this->set('people', $allPeople);
When I edit a document's record, I want the person owning the document to be selected and displayed in the box. Right now, the app just makes the list box but doesn't highlight the correct owner (though the DB has the person's id).
Thank you,
Frank Luke

In your edit view, you should add an extra parameter to your $form->select(), called $selected. This way, you can specify which item should be selected from the list.
Example (just an example, you should rewrite it for your own situation):
<?php echo $form->select('Document.person', $allPeople, $this->data['Document']['Person']['id']); ?>
More information:
http://book.cakephp.org/view/728/select
-- Bjorn

Related

Facing difficulty with Scaffloding concept in cakephp

I am preparing 3 pages one is questions for Quiz,one is for questions page and another one is for Options.I used relationship concept in Quiz model i used the following code
var $hasMany = array('question');
}
?>
and in Question model i used the following code
<?php
class question extends AppModel {
var $name='question';
var $belongsTo = array(
'quiz'=>array(
'className'=>'quiz',
'foreignKey'=>'quiz_id',
'conditions'=>null,
'fields'=>null
)
);
}
?>
and in Quizzes,Questions and Chapters controller, i added Scaffolding concept
public $scaffold;
Now I got add Question page,add quiz page and add options page but the problem is all the fields are of textbox but for some fields i need radio buttons and drop down buttons and image type.Is there any way to change the field type from default text boxes to our required format using scaffolding or any other way...
Please help me out with this problem.
Thank you.
If I understand your question correctly, then you are asking how to automatically change the form input types like textbox, radio, and dropdown in scaffolding/bake. This answer is for the current version of CakePHP 2.5.
CakePHP determines the form input type based on the database column data type.
So, for fields with a database column of char, varchar, or other string, CakePHP will output a text box in the form. If you change a column type to boolean, you will see a checkbox.
For radio buttons and drop-downs, CakePHP also needs to know the list of options to present on the form. Your database column therefore needs to be a foreign key. An example:
Let's pretend one of the Questions on your Quiz is "What country are you from?". So you need a table that lists all Countries. Then in the Question table you would have a column called country_id. Remember to update your Questions and Country model with the hasOne / belongsTo relationship. Then bake/scaffold will output a form input with a dropdown for selecting the Country. To change this to a radio button list (and more configuration options) see FormHelper.

How to Build one form with input of many views

I have 3 tables/models:
Customer
Address
City
Each one have their views,but I need to add Address and City to the Customer view and there will be all the inputs from each view and it will result in only one Form. I read some stuff about include these views in Customer's view using elements.
What is the best way for doing that?
If I'm reading you right, you have 3 tables, customer, address & city, and want to read them all when you have a user view?
Sounds like you want to set up A CakePHP association, likely a hasOne relationship.
Edit: Here's how you'd use an element to accomplish this:
// Form
<?php echo $this->element('cityselect'); ?>
// app/View/Elements/cityselect.ctp
<?php $cities = $this->requestAction('/cities/index'); ?>
// Use $cities to populate element form
You'll probably want to write an action for your cities/address controllers that returns key-pairs to easily populate your select field.

cake php auto select foreign keys when editing

I am using Cake PHP with scaffolding. I'm having a problem with the code that it generates and want to see if there is a way around it of if I should end up building custom views.
Lets say that I have two models Tests and Questions. Tests can have many Questions and a Question has only one test. I have setup the hasMany and belongsTo Associations.
Now, the scaffolded view that cake creates for Tests gives me a button at the bottom in the "Related Questions" to create a question. When I click this button, I get the 'Add' form for questions, but the right test is not auto selected.
Is there anyway I can make the button pass the test_id into the Question form and have that auto populate?
I see how you think that might work; but Cake doesn't know you want that behaviour out of the box.
You would need to adjust your Question Add method, or create a new one:
Example code:
// action: tests/view/1 (viewing test 1, and all related questions)
// create a link containing the ID of the current test as a param
<?php echo $this->Html->link('Add Question to Test',
array('controller'=>'questions',
'action' => 'add_question',
$test['Test']['id'])
);
?>
So - assuming you have access to id of the current test, you can pass it as a parameter to your questions controller (there are several ways to do this).
Then:
// view - questions/add_question/1
<h1>Adding A Question to Test 1</h1>
<?php
// create your add question form
$this->Form->input('test_id', array('type'=>'hidden',
'value' => $this->params['pass'][0]));
// create a hidden field with the value of the first passed param (the test id)
then in your controller, the test_id is already set, so when you save the question, it is saved with the appropriate test_id
If you want to apply this to all your CakePHP projects generated using cake bake, you can make a couple of small changes to the CakePHP core to enable this, as seen here:
https://github.com/srs81/cakephp/commit/7d92c8f676c79185fa6a74ab2070f240c555a2a0
Basically these two changes append the referring model/controller ID and name to the "add" action, and this is handled in the "add" action where the correct ID is selected.
This does NOT work for HABTM models, but should work fine for anything else.
You need to add var $uses = array('Question','Test'); to questions_controller.php

Convert a text box to dropdown box from Add view

Can you help me figure this out please? I have 4 form elements on my add view (app\views\tickets\add.ctp) but the 'status' input is on a text box. I want that to be converted to drop down box populated with data from a field in a table called Status. How do I go about doing it?
echo $this->Form->input('problemno');
echo $this->Form->input('status');
echo $this->Form->input('description');
echo $this->Form->input('user_id');
Thanks,
Lyman
If you want to force the type of input field, do not use the input() method, but use the method for the type you want.
To get a drop down list, you can use the select() method:
$options = array('status1' => 'status1', 'status2' => 'status2', ...);
$this->Form->select('status', $options);
See http://book.cakephp.org/view/1430/select
If the model associated with your 'Status' table is related to you current model ('Ticket', presumably) with a hasMany,hasOne, or belongsTo (...as long as your 'Status' model shows up when you debug $this->Ticket->read(null, <some_ticket_id>)) you can do
echo $this->Form->input('StatusModel.field')
and cake will automagic that field to be whatever it needs to be.
You will have to search around for how to make cakePHP give you a dropdown selection based on database field.
This is not exactly a correct way. It might be good, or you might find changing it later on to be a problem. But if you just want to create a dropdown with some options, here you go:
echo $this->Form->input('status', array('options'=>array('status1'=>'status1','status2'=>'status2','status3'=>'status3')));

CakePHP 1.3, Form Helper and Self Joined Table

My Self Joined Categories table is as follows:
id, name, description, parent_id
I used Cake Bake to generate the Model, Controller and Views. Model
has the $belongsTo and $hasMany association set up. In add() of the
controller,
$parentCategories = $this->Category->ParentCategory->find('list');
$this->set(compact('parentCategories'));
is present. In the add view, the cake bake generated form is:
<?php
echo $this->Form->input('name');
echo $this->Form->input('description');
echo $this->Form->input('isincome');
echo $this->Form->input('parent_id');
?>
When I run in browser, the parent_id field is getting a drop down, but
it is not being filled with any data. I used
<?debug($parentCategories);?>
in the add view, and it happily outputs
Array
(
[1] => Entertainment
[2] => Groceries
)
But this array is not being used for filling that drop down by the
Form helper. What should I do? Is this a bug with Cake's Form helper in 1.3? It never occurred in 1.2...
When adding an input for field_id, the form helper looks for a variable called $fields. I.e., the name without _id and pluralized. $parentCategories does not fit that description, so it's not used. $parents would be.
Second, $this->Category->ParentCategory is the same as $this->Category. Both reference the Category model. No need to go through ParentCategory.
Third, it's not usually a good idea to join a Tree model to itself. You'll understand why when you start to query with higher recursive settings. You should instead make it a proper Tree and use the TreeBehavior methods to query it.

Resources