Wednesday, November 26, 2008

Re: Just getting started and have a problem with relationships

You do not use print_r in your controller unless you have turned off
the rendering of the view. Use debug and it will print it out the
variable at the top.
Do you really have a table named blogs? If you are trying to add the
users and posts you have told blog to use them.
Add var $uses = array('User','Blog'); to your controller. That is if
you really have a table named blogs which baffles me.

On Nov 26, 10:03 am, austin <awe...@gmail.com> wrote:
> can you point out which is line 7 of your controller, please?
>
> austin
>
> On Nov 23, 11:33 pm, "James.Diss" <James.D...@gmail.com> wrote:
>
> > Okay, well this gets weirder.
>
> > I've started on the blog tutorial on the website, and I'm working
> > through is successfully apart from validate isn't appearing in the
> > model!
>
> > Just to check I threw in a var into the model arbitrarily, and tried
> > to refer to it, but it says that it's not defined.  I have to say that
> > it's looking like my models are being completely ignored and it's just
> > using AppModel.  This is odd.
>
> > Model;
> > class Blog extends AppModel {
> >         var $name = 'Blog';
>
> >         var $validate = array(
> >                 'title' => array(
> >                         'rule' => array('minLength', 1)
> >                 ),
> >                 'body' => array(
> >                         'rule' => array('minLength', 1)
> >                 )
> >         );
>
> >         var $testingValue = "This is testing";
>
> > }
>
> > Controller;
>
> > class BlogsController extends AppController {
> >         var $name = 'Blogs';
>
> >         function index() {
> >                 print_r($this->Blog->testingValue);
> >                 $this->set('blogs', $this->Blog->find('all'));
> >         }
>
> >         function view($id=null) {
> >                 $this->Blog->id = $id;
> >                 $this->set('blog', $this->Blog->read());
> >         }
>
> >         function add() {
> >                 if (!empty($this->data)) {
> >                         if ($this->Blog->save($this->data)) {
> >                                 $this->flash('Your blog has been saved.', '/blogs');
> >                         }
> >                 }
> >         }
>
> > }
>
> > Error;
>
> > Notice (8): Undefined property:  AppModel::$testingValue [APP/
> > controllers/blogs_controller.php, line 7]
> > BlogsController::index() - APP/controllers/blogs_controller.php, line
> > 7
> > Object::dispatchMethod() - CORE/cake/libs/object.php, line 114
> > Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 259
> > Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 213
> > [main] - APP/webroot/index.php, line 90
>
> > Could this be down to permissions on the server?
>
> > On Nov 23, 8:10 pm, "James.Diss" <James.D...@gmail.com> wrote:
>
> > > controllers;
>
> > > users_controller;
>
> > > class UsersController extends AppController {
> > >         var $name = 'Users';
> > >         var $scaffold;
>
> > > }
>
> > > posts_controller;
>
> > > class PostsController extends AppController {
> > >         var $name = 'Posts';
> > >         var $scaffold;
>
> > > }
>
> > > 'hitting' the controllers means using the url.....http://<url>/posts
> > > or http://<url>/users
>
> > > As for using Bake...at the moment I'm just working with a couple of
> > > models and controllers, surely throwing views into the mix is going to
> > > complicate the problem I had, which isn't regarding speed or getting
> > > to grips with how this works (I have ten years of PHP and 2 years of
> > > Java) but being vaguely confused how a small 'unit test' of how cake
> > > is supposed to operate is failing so fundamentally.  What is this,
> > > fifteen lines of application code?
>
> > > Anyhow, thanks for the debug tip;  I can see from the resulting dump
> > > of the object that the relationships aren't working:-
>
> > >  [belongsTo] => Array
> > >                 (
> > >                 )
>
> > > [hasOne] => Array
> > >                 (
> > >                 )
>
> > > [hasMany] => Array
> > >                 (
> > >                 )
>
> > > [hasAndBelongsToMany] => Array
> > >                 (
> > >                 )
>
> > > On Nov 22, 7:33 pm, "php.baker" <php.ba...@gmail.com> wrote:
>
> > > > I very much doubt that the problem relates to php and/or apache.
> > > > Please post your controller classes.... Also, what url did u use to
> > > > "hit the post
> > > > controller"?
>
> > > > As far as debugging goes... Set debug to 3 (in .../config/core.php)
> > > > and you should be able to see very quickly if your models are linked.http://book.cakephp.org/view/155/Debugging
>
> > > > I would recommend you try using the bake console. Bake your models,
> > > > controllers, and views, and then look for differences between your
> > > > code and the code generated by bake. Bake is a great way for beginners
> > > > and experts to get apps going quickly(imho).
>
> > > > On Nov 22, 2:36 pm, "James.Diss" <James.D...@gmail.com> wrote:
>
> > > > > 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 <webwe...@gmail.com> 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" <James.D...@gmail.com> 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');
>
> > > > > > > }
>
> > > > > > > //    
>
> ...
>
> read more »
--~--~---------~--~----~------------~-------~--~----~
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: