0

Hello CakePhp developers,

CakePHP 2.5.2

I have a database with an Agreements table. It belongs to Adults, Children, Groups, Prices and Courses tables.

My Controller has 3 actions: index, add and edit. The first one, index, works fine, displays all related tables in one page. But problem is with the edit and add actions.

My Controller has arrays with related data, and its arrays do not have real names of Children, Adults, Groups and so on. Only the numbers 1,2,3, etc.

My AgreementsController.php is sending data to View by this commands:

$this->Agreement->recursive = 1;
$adults = $this->Agreement->Adult->find('list');
$childrens = $this->Agreement->Children->find('list');
$groups = $this->Agreement->Group->find('list');
$prices = $this->Agreement->Price->find('list');
$courses = $this->Agreement->Course->find('list');
$this->set(compact('adults', 'childrens', 'groups', 'prices', 'courses'));

I think that this arrays $adults, $children, have only numbers. Why they do not retrieve real item numbers. My Agreements/add.ctp is:

<?php echo $this->Form->create('Agreement'); ?>

    <?php
        echo $this->Form->input('date_of_agreement');
        echo $this->Form->input('number');
        echo $this->Form->input('year');
        echo $this->Form->input('adult_id', array (                      
                        'type' => 'select', 
                        'options' => $adults 
                        ) 
                );
                print_r($adults);
        echo $this->Form->input('children_id');
        echo $this->Form->input('status_id');
        echo $this->Form->input('city_id');
        echo $this->Form->input('price_id');
        echo $this->Form->input('course_id');
        echo $this->Form->input('group_id');
    ?>

<?php echo $this->Form->end(__('Submit')); ?>

Even adding 'options' parameter is not solving problem. I've spent 50h on that and i do not know why?

I have inspected array it looks like this : Array ( [1] => 1 [2] => 2 )

Can you help me with this?

caitlin
  • 2,769
  • 4
  • 29
  • 65
tomas3man
  • 29
  • 7
  • Why is the index code in the question? if your problem is that (e.g.) `$adults` is a numeric array, *it's irrelevant* how you refer to that variable it's going to be numeric. The thing that matters is what is the displayField for the models you're calling find list on. Rephrasing your question it is `Why is this a numeric array: $adults = $this->Agreement->Adult->find('list');` <- reduce your question to only include what's relevant please. – AD7six Jun 16 '14 at 13:09
  • Thank you very much for help. I did not know what to include in question. – tomas3man Jun 16 '14 at 13:18
  • Instead of Array ([1] => 1 [2] => 2) you want eg Array ([1] => 'Tomas' [2] => 'Anna') ? – Salines Jun 16 '14 at 13:40
  • Yes. For examle in Agreement table have Children_id, it contains number, like 1,2,3,4. In Childrens table have ['Children']['first_name']. I want to show array Children_id => ['first_name'], etc. – tomas3man Jun 16 '14 at 13:58
  • [code] CREATE TABLE IF NOT EXISTS `agreements` ( `id` int(11) NOT NULL AUTO_INCREMENT, .......... `adult_id` int(11) DEFAULT NULL, `children_id` int(11) NOT NULL, `status_id` int(11) NOT NULL, `city_id` int(11) NOT NULL, `price_id` int(11) DEFAULT NULL, `course_id` int(11) DEFAULT NULL, `group_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; [/code] – tomas3man Jun 16 '14 at 14:00
  • @Salines CREATE TABLE IF NOT EXISTS `childrens` ( `id` int(11) NOT NULL AUTO_INCREMENT, `children_first_name` varchar(18) DEFAULT NULL, `children_last_name` varchar(15) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; – tomas3man Jun 16 '14 at 14:01
  • @tomas3man it's much better. It's still IMO simply way too much text given that `I have inspected array it looks like this : Array ( [1] => 1 [2] => 2 )` is the whole question. On an unrelated point the singular of children is child - i.e. the model should be named Child, the table children. – AD7six Jun 16 '14 at 21:02

1 Answers1

0

In your db tables rename fields:

  • adult first_name > first_name,
  • adult_last_name > last_name,
  • children_first_name > first_name,
  • children_last_name > last_name,
  • group > name
  • ...

In your models create virtual fields:

public $virtualFields = array(
'name' => "CONCAT(Adult.first_name, ' ', Adult.last_name)"
);

clear cache and test.

EDIT

Or you can in your find list method add options, eg:

$adults = $this->Agreement->Adult->find('list',array(
        'fields'=>array('Adult.id','Adult.first_name')
        ));
Salines
  • 5,674
  • 3
  • 25
  • 50
  • 1
    renaming the fields, while good advice, is not fundamental to the answer. The relevant point is that if no fields list is passed to the find list call, the model primary key and display field are used - which [defaults to the primary key fieldname](https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/Model.php#L880) - resulting in an array of `pk => pk`. – AD7six Jun 16 '14 at 21:05