문제

In the process of upgrading a 1.3 CakePHP app to 2.2.5 and I'm getting unexpected results on a find('list').

Does anyone know why I am getting 1 for the id value instead of the incremental values stored in the database, which is 1,2,3,4,5?

When I run the controller code (shown below), I get:

Array
(
     [1] => 5
)

What I want to get is:

Array
(
     [1] => 1 (urgent)
     [2] => 2
     [3] => 3
     [4] => 4
     [5] => 5

)

And if I change it to find('all'), I get: *Note the [id] value is always 1*

Array
(
[0] => Array
    (
        [Priority] => Array
            (
                [id] => 1
                [description] => 1 (urgent)
            )

    )

[1] => Array
    (
        [Priority] => Array
            (
                [id] => 1
                [description] => 2
            )

    )

[2] => Array
    (
        [Priority] => Array
            (
                [id] => 1
                [description] => 3
            )

    )

[3] => Array
    (
        [Priority] => Array
            (
                [id] => 1
                [description] => 4
            )

    )

[4] => Array
    (
        [Priority] => Array
            (
                [id] => 1
                [description] => 5
            )

    )

)

Already Tried

  • Renaming Priority to PriorityLevel in case Priority was a reserved word in CakePHP.

See related code below:

Controller Code

$priorities = $this->Ticket->Priority->find('list', array('fields' => array('Priority.id', 'Priority.description'),'order' => 'Priority.description'));

Model Code

class Ticket extends AppModel {
public $name = 'Ticket';
public $belongsTo = array('Affect', 'Priority', 'User', 'Status');
public $validate = array(
    'priority_id' => array(
        'rule' => 'notEmpty',
        'message' => 'Please assign a priority to this ticket'
    )
);
}

class Priority extends AppModel {
public $name = 'Priority';
}

View Code

echo $this->Form->input('Ticket.priority_id', 
                            array('type' => 'select',
                            'empty' => true,
                            'tabindex' => 2));
도움이 되었습니까?

해결책 2

Answer: Change the data type of the id field in the priorities table from tinyint(1) to tinyint(2).

Explanation: CakePHP seems to interpret tinyint(1) as a boolean. However, I was using this because I knew the priority list was only ever going to be from 1 to 5.

다른 팁

If Priority has many tickets then you should start with the Priority.php model and add the following:

public $hasMany = array(
    'Tickets' => array(
        'className' => 'Ticket',
        'foreignKey' => 'priority_id',
            )
);

Then your Ticket.php model class looks fine, however, I would write it in the following manner:

public $belongsTo = array(
    'Priority' => array(
        'className' => 'Priority',
        'foreignKey' => 'priority_id',
    )
);

You have to ensure that the table Tickets in your Db has a column named priority_id so we have the correct relationship set.

Then in your Ticket Controller something like this should easily work:

$priorities = $this->Ticket->Priority->find('list');

If this does not work, if you want to zip both of your model classes and your controller and send them to me I will be glad to look at it. To make things easier you can send along the structure of both the priority and ticket tables.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top