CakePHP: BelongsTo Relationship

I want to simulate the following simple relationship:

One passenger belongs to a car; One car has many passengers.

The passenger table has an identifier and a Car_id column, the Car table has one identifier column.

My models look like this:

<?php
class Passenger extends AppModel {
   var $name = 'Passenger';
   var $belongsTo = 'Car';
} ?>

and

<?php
class Car extends AppModel {
   var $name = 'Car';
   var $hasMany = array (
      'Passenger' => array (
            'className' => 'Passenger',
            'foreignKey' => 'car_id'
         )
   );
}
?>

and my add Passenger.ctp looks like this:

<?php
echo $this->Form->create('Passenger');
    echo $this->Form->input('car_id');
    echo $this->Form->end('Save');
?>

BUT, when I go to the page to add a passenger, all I see is an empty drop-down list. Is there another step I must take to populate Dropbox with all the machines?

+5
source share
3 answers

First, you forgot to mention the relationship belongsToin your Passenger model:

<?php
class Passenger extends AppModel {
   var $name = 'Passenger';
   var $belongsTo = array('Car');
}
?>

, list ($ ). :

$cars = $this->Passenger->Car->find('list');
$this->set(compact('cars'));

car_id .

.

+2

, - .

$this->Car->find('list');

:

$this->set('cars',$cars);

$cars $options :

echo $this->Form->input('car_id', array('options' => $cars));

- :

echo $this->Form->input('Car.id', array('options' => $cars));
+2
$this->CompanyCashback->bindModel(array('belongsTo' => array(
        'CompanyBranch' => array('className' => 'CompanyBranch', 'foreignKey' => false, 'conditions' => array('CompanyCashback.publisher_id = CompanyBranch.publisher_id && CompanyBranch.branch_type = "online" ')),
        'PersonalInformation' => array('className' => 'PersonalInformation', 'foreignKey' => false, 'conditions' => array('CompanyCashback.publisher_id = PersonalInformation.user_id')),
        'Country' => array('className' => 'Country', 'foreignKey' => false, 'conditions' => array('PersonalInformation.country_id = Country.id')),
        'User' => array('className' => 'User', 'foreignKey' => false, 'conditions' => array('PersonalInformation.user_id = User.id')))
));
0
source

All Articles