I actually tried this first in an effort to get it working.

Currently the model for 'Post' looks like;

//      app/models/Post.php
class Post extends AppModel {
        var $name = 'Post';
        var $validate = array();
        var $belongsTo = array('User'=>array
('className'=>'User','foreignKey'=>'user_id'));
}

The model for 'User' looks like;

//      app/models/User.php
class User extends AppModel {
        var $name = 'User';
        var $validate = array();
        var $hasMany = array('Post');
}

The thing is that I suspect that I'm keeping to the conventions with
the following DDLs;

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` varchar(100) default NULL,
  `email` varchar(150) default NULL,
  `firstname` varchar(60) default NULL,
  `lastname` varchar(60) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

CREATE TABLE `posts` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `date` datetime default NULL,
  `content` text,
  `user_id` int(11) default NULL,
  PRIMARY KEY  (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

(before anyone points out the index 'user_id' after the primary key,
it's not that, I checked.  I would have been disappointed if an index
threw things out of kilter, though)

At this moment I'm leaning towards something being wrong with the
install, and I'm humble enough to think it's PHP/Apache2.  Is there
any configuration in either of those can stop associations being made?
(Although they're both as vanilla as possible for deploying code on
live servers)

TIA

On Nov 22, 3:42 pm, Rob <[EMAIL PROTECTED]> wrote:
> You probably want to add the foreigh_key to your hasMany and
> belongsTo, especially if they don't follow the cake conventions.
>
> In my models, I have ID columns like 'user_id', and the foreign_key is
> typically the same name, so for instance in my Users to Groups
> relationship I have something like:
>
> // Users model ...
>     var $hasMany = array(
>         'UserGroup' => array(
>             'className'     => 'UserGroup',
>             'foreignKey'    => 'user_id',
>             'limit'         => '5',
>             'dependent'     => true
>         )
>     );
>
>     // Link to groups
>     var $hasAndBelongsToMany = array(
>         'Group' =>
>         array(
>                 'className'             => 'Group',
>                 'joinTable'             => 'user_groups',
>                 'foreignKey'            => 'user_id',
>                 'associationForeignKey' => 'group_id',
>                 'conditions'            => '',
>                 'order'                 => '',
>                 'limit'                 => '',
>                 'unique'                => true,
>                 'finderQuery'           => '',
>                 'deleteQuery'           => '',
>                 'insertQuery'           => ''
>         )
>     );
>
> // Groups model
>     var $hasMany = array(
>         'UserGroup' => array(
>             'className'     => 'UserGroup',
>             'foreignKey'    => 'group_id',
>             'limit'         => '5',
>             'dependent'     => true
>         )
>     );
>
>     // Link to users
>     var $hasAndBelongsToMany = array(
>         'User' =>
>             array(
>                 'className'             => 'User',
>                 'joinTable'             => 'user_groups',
>                 'foreignKey'            => 'group_id',
>                 'associationForeignKey' => 'user_id',
>                 'conditions'            => '',
>                 'order'                 => '',
>                 'limit'                 => '',
>                 'unique'                => true,
>                 'finderQuery'           => '',
>                 'deleteQuery'           => '',
>                 'insertQuery'           => ''
>         )
>     );
>
> On Nov 22, 9:45 am, "James.Diss" <[EMAIL PROTECTED]> wrote:
>
> > The failing isn't really in understanding the relationships, just
> > something odd is happening that I want to get a perspective on as a
> > beginner.
>
> > I have two models with backing database tables.  The models are;
>
> > //      app/models/User.php
> > class User extends AppModel {
> >         var $name = 'User';
> >         var $validate = array();
> >         var $hasMany = array('Post');
>
> > }
>
> > //      app/models/Post.php
> > class Post extends AppModel {
> >         var $name = 'Post';
> >         var $validate = array();
> >         var $belongsTo = array('User');
>
> > }
>
> > I also have two controllers that basically use the scaffold.  The
> > users controller does what it's supposed to do, but hitting the post
> > controller doesn't show the user select box, which indicates to me
> > that the relationship isn't being built. The foreign key in the posts
> > table is 'user_id'.
>
> > As I'm just getting started, I'm at a loss to debug what appears to be
> > a very basic piece of code.
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to