views:

109

answers:

4

OK, I am little bit lost...

I am pretty new to PHP, and i am trying to use CakePHP for my web-site. my DB is composed of two tables:

  • users with user_id, name columns
  • copies with copy_id, copy_name, user_id (as foreign key to users) columns.

and I have the matching CakePHP elements:

  • User and Copy as a model
  • UserController as controller
  • i dont use a view since i just sending the json from the controller.

i have added hasMany relation between the user model and the copy model see below.

var $hasMany = array(
    'Copy' => array(
        'className' => 'Friendship',
        'foreignKey' => 'user_id'
    )
); 

with out the association Every find() query on the users table works well, but after adding the hasMany to the model the same find() queries on the users stop working (print_r does show anything), and every find() query i am applying on the Copy model

$copy = $this->User->Copy->find('all', array(
    'condition' => array('Copy.user_id' => '2')
));

ignores the condition part and just return the whole data base.

how can I debug the code execution? when I am adding the debug($var) nothing happens.

sorry for the length, it is just I am a little bit lost with this..

i have a User model, and a users_controler, that correspond to the users table in my DB. i also have a Friendship model, and a friendship

when i am trying to use the find() function every thing works well, but when i am adding to the model a hasMany association suddenly the results are

+1  A: 

I'm not an expert, but you can start with the following tips:

  1. Try to follow the CakePHP database naming conventions. You don't have to, but it's so much easier to let the automagic happen... Change the primary keys in your tabel to 'id', e.g. users.user_is --> users.id, copies.copy_id -->copies.id.
  2. Define a view, just for the sake of debugging. Pass whatever info from model to view with $this->set('users', $users); and display that in a <pre></pre> block
  3. If this is your first php and/or CakePHP attempt, make sure you do at least the blog tutorial
  4. Make CakePHP generate (bake) a working set of model/view/controllers for users and copies and examine the resulting code
  5. There's good documentation about find: the multifunctional workhorseof all model data-retrieval functions
jodorovski
A: 

Your table name might be the problem too. I had a table named "Class" and that gave cake fits. I changed it to something like Myclass and it worked. Class was a reserved word and Copy might be one too.

Dan Berlyoung
+1  A: 

I think the main problem is this:

'condition' => array('Copy.user_id' => '2')

It should be "conditions".

Also, stick to the naming conventions. Thankfully Cake lets you override pretty much all its assumed names, but it's easier to just do what they expect by default.

  • The primary keys should be all named id
  • The controller should be pluralised: UsersController
nickf
Yup, this is the reason the poster's `find` query failed. Probably obviates the rest of the discussion...
Daniel Wright
+1  A: 

First off, try as much as possible to follow CakePHP convention.

var $hasMany = array(
    'Copy' => array(
        'className' => 'Friendship',
        'foreignKey' => 'user_id'
    )
);

Your association name is 'Copy' which is a different table and model then on your classname, you have 'Friendship'.

Why not

var $hasMany = array(
     'Copy' => array('className'=>'Copy')
);

or

var $hasMany = array(
     'Friendship' => array('className'=>'Friendship') 
);

or

var $hasMany = array(
    'Copy' => array('className'=>'Copy'),
    'Friendship' => array('className'=>'Friendship')
);

Also, check typo errors like conditions instead of condition

jpdelatorre