The FK in posts table should be user_id, not username. The value should be taken from users.id.
CREATE TABLE `users` ( `id` int(11) NOT NULL auto_increment PRIMARY KEY, ... CREATE TABLE `posts` ( `id` int(11) NOT NULL auto_increment PRIMARY KEY, `user_id` int(11) NOT NULL, ... FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE ... The reason you store the *id* as a FK is because the other values in a table might change, leading to mis-matches. By using the id, the details of the association between 2 tables remain abstracted. So, once you've done that, your find results for posts will contain a user_id field. As that's not very informative on its own, you'll want to use a 'recursive' or (better) 'contain' option in order to fetch the data fro the users table, as well. On Sat, Jul 4, 2009 at 2:58 PM, eMilk<[email protected]> wrote: > > Alright, after some messing around, I cant seem to work this thing > out. I've attached some dumps: > > My 'posts' table, after running the > ALTER TABLE ORDERS > ADD FOREIGN KEY (`username`) REFERENCES users(`username`); > command: > > --------- > CREATE TABLE `posts` ( > `id` int(10) unsigned NOT NULL auto_increment, > `created` datetime default NULL, > `username` varchar(50) default NULL, > `name` varchar(15) NOT NULL, > `year` date NOT NULL, > `country` varchar(12) NOT NULL, > `origin` varchar(40) NOT NULL, > `video` varchar(60) NOT NULL, > `picture` varchar(100) NOT NULL, > `text` text NOT NULL, > `rating` varchar(3) NOT NULL, > `keywords` varchar(100) NOT NULL, > PRIMARY KEY (`id`), > KEY `username` (`username`) > ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ; > > > -- Dumping data for table `posts` > > INSERT INTO `posts` VALUES(1, '1984-10-26 00:00:00', 'Me', 'R2-D2', > '1977-05-25', 'USA', 'Star Wars', 'http://www.youtube.com/watch? > v=gQCWV4vuz', 'http://localhost/images/r2d2.jpg', 'R2-D2 (phonetically > spelled Artoo-Detoo)', '02', 'cyborg, assassin, military, war'); > > The 'posts' data is just some dummy data I typed in manually.. > > This is my PostsController::add(): > function add() { > if (!empty($this->data)) { > $this->data['Posts']['username'] = > $this->Auth->user > ('username'); > if ($this->Post->save($this->data)) { > $this->Session->setFlash('Your robot has been > saved.'); > $this->redirect(array('action' => 'index')); > } > } > } > > > And just in case, here's my users table: > CREATE TABLE `users` ( > `id` int(11) NOT NULL auto_increment, > `username` char(50) NOT NULL, > `password` char(50) NOT NULL, > `active` tinyint(4) NOT NULL default '0', > `email` varchar(255) NOT NULL, > `created` datetime default NULL, > `modified` datetime default NULL, > PRIMARY KEY (`id`) > ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; > > Thanks again - I really hope you can tell me where I've made a > mistake. > > > On Jul 4, 8:00 pm, eMilk <[email protected]> wrote: >> Hi Brian, >> >> Thanks - will try that out right away! :) >> >> On Jul 4, 7:12 pm, brian <[email protected]> wrote: >> >> > Add a user_id column to the posts table. You should use that as your >> > foreign key. >> >> > As for filling it in automatically, you can do that right before >> > save() in PostsController::add() >> >> > $this->data['Posts']['user_id'] = $this->Auth->user('id'); >> >> > On Sat, Jul 4, 2009 at 12:55 PM, eMilk<[email protected]> wrote: >> >> > > Hi all you experienced bakers. >> >> > > I'm new to the world of CakePHP, so forgive me if this is a very basic >> > > question. >> > > I followed a few tutorials, and have a question about getting >> > > information filled automagically into the different fields of the >> > > table. >> >> > > First off, I extended the blog tutorial with a simple implementation >> > > of Auth, based heavily on what I could find here: >> > >http://mrphp.com.au/code/code-category/cakephp/cakephp-1-2/simple-aut... >> >> > > Then, I've extended on the blog tutorial, and have been adding new >> > > fields to the "posts" table from the example. One of the new fields, I >> > > created is 'username'. >> >> > > Here's the deal: I'd really like to have the 'username' field being >> > > filled automagically based on what user is logged in, just like it >> > > does to both the 'modified' and 'created' fields by default in the >> > > blog-tutorial. >> >> > > From the blog-tutorial, bottom: [10.1.2 Creating the Blog Database] >> > > "Check out "CakePHP Conventions" for more information, but suffice it >> > > to say that naming our table 'posts' automatically hooks it to our >> > > Post model, and having fields called 'modified' and 'created' will be >> > > automagically managed by Cake." >> >> > > Thanks in advance! >> > > Best regards, >> > > Emil K > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
