Saturday, July 4, 2009

Re: Automagically filling table-fields?

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 <emil.kja...@gmail.com> wrote:
> Hi Brian,
>
> Thanks - will try that out right away! :)
>
> On Jul 4, 7:12 pm, brian <bally.z...@gmail.com> 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<emil.kja...@gmail.com> 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 cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: