tags:

views:

163

answers:

2

hello!

can you help me how to use CakePHP's displayField directive, I can not figure out how to use it...

so, in a model file, i have following code...

<?php

class Task extends AppModel {

var $name = 'Task';
var $displayField = 'projectName';

//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''

etc...

how can i use this, to display fied projectName in select form field?

thank you in advance!!!

A: 

You can basically do this (in controller):

$this->set('tasks', $this->Task->find('list'));

And make an input with name task_id, and make sure to force it to be a select box, so (in views):

echo $form->input('task_id', array('label' => 'youLabelHere', 'type' => 'select'));

displayField gives you a chance to choose which field will be used as a displayed option (one of) in the select box, if it's not provided in model the script will look for 'name' or 'title'.

If you wan't to display projects in the users forms (add, edit option) then your associations aren't right. Always make sure that there is an association (with good, conventional tables and keys names) between two model when you want to make a select box, Cake makes it as easy as can it gets.

PawelMysior
Still not understand... can you help me again, step by step?
Maybe explain once again what is it that you want to do. Show me involved models and I can surely help you from there.
PawelMysior
So I have two tables - `projects` and `tasks`. Of course, table `task` have secondary key, named `project_id`.All I want is to build simple windows form, which will be used for task insert/edit actions. That form will contain select-option form field, with items from table `projects`.Problem is that table `project` have field called `projectName`, and I wolud love to put that field into select-option menu.Everything will be simple if `projectName` field is called `name` or `title`...Hope you understand me...
+1  A: 

So, you have Task belongsTo Project (FK: project_id). You want to make a project select box in tasks/add and tasks/edit views.

The problem is that the projects table doesn't have a field called name or title so the select box is empty. You wouldn't have any problems if there was a name or a title field, right?

Well, here's the solution, in the Project model add this:

var $displayField = 'projectName';

http://book.cakephp.org/view/71/Model-Attributes

So you were going in the right direction, just messed up the models a bit. I hope you understand it now ;]

PawelMysior
That's right, it works! Thank you!Just tell me, every time I recreate models, I'll have to define var $displayField = 'projectName'; ?
What do you mean by recreating models? Models for tables that have a `title` or `name` field won't need that, also if you're not going to be using them like: `ModelName->find('list');` you don't have to set this variable (still it's a good idea to do so). Please check this as an answer so this question becomes answered.
PawelMysior