Wednesday, November 26, 2008

Re: Just getting started and have a problem with relationships

Still having problems with this. I've now deinstalled and reinstalled
PHP5 and Apache2 (mainly to cure an unrelated problem connected with
the non-retention of sessions, but that's another container of marine
vertebrates).

So I've gone from enthusiasm to abject disappointment over Cake. It's
a shame, because the approach is sound enough.

On Nov 23, 11:41 pm, "James.Diss" <James.D...@gmail.com> wrote:
> It's not permissions.  Answered my own question there.
>
> 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'          
>
> ...
>
> 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: