source: http://www.developer.com/lang/php/article.php/3636686

CREATE TABLE players (
   id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
   firstname VARCHAR(50) NOT NULL,
   position VARCHAR(25) NOT NULL,
   team_id SMALLINT(6)
)

CREATE TABLE teams (
   id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(50) NOT NULL, 
)
<?php
    class PlayersController extends AppController
    {
        var $scaffold;
    }
?>
<?php
    class TeamsController extends AppController
    {
        var $scaffold;
    }
?>
<?php
    class Player extends AppModel
    {
        var $name = 'Player';
 
        var $belongsTo = array('Team' =>
                 array('className'    => 'Team',
                     'conditions'   => '',
                     'order'        => '',
                     'foreignKey'   => 'team_id'
                 )
             );
    }
?>

Note the $belongsTo array has been defined, mapping the team_id column to the Team model. Finally, we need to define the Team model so CakePHP knows which column should be displayed as a result of mapping to the teams table. Place the following code in a file named team.php and save it to the /app/models/ directory:

<?php
    class Team extends AppModel
    {
        var $name = 'Team';
        var $displayField = 'name';
    }
?>

http://localhost/cake/teams/add

http://localhost/cake/teams/

  • info/tutoriel_cakephp_scaffold_join_tables.txt
  • Dernière modification: 2018/07/18 09:46
  • par radeff