Re: Model associations problem

2010-06-17 Thread Jeremy Burns | Class Outfit
The  results are perfectly correct for the code you are running; you are 
finding all Trackers. Your code:

$this->set('trackers', $this->User->Tracker->find('all'));

... is saying "go to the User model, then leap out to the Tracker model and 
bring me back all Trackers".

To filter the search, pass in a condition:

$this->set(
'trackers',
$this->User->Tracker->find(
'all',
array(
'conditions' = array(
'Tracker.user_id' => $userId
// where $userId is 'this' user
)
)
)
);

Or (in my opinion the better strategy) use the containable behaviour: 
http://book.cakephp.org/view/1323/Containable.

Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
http://www.classoutfit.com

On 17 Jun 2010, at 00:43, ryanl wrote:

> I'm in the process of creating my first web application with CakePHP,
> however I've run into some problems. I'll just start with the code:
> 
> 
> class User extends AppModel
> {
>   public $name = 'User';
>   public $belongsTo = 'Group';
>   public $hasMany = array(
>   'Tracker' => array(
>   'className' => 'Tracker',
>   'foreignKey' => 'user_id',
>   'dependent' => true,
>   'order' => 'Tracker.created DESC'));
> 
>   public $validate = array(
>   'username' => 'alphaNumeric',
>   'password' => 'notEmpty'
>   );
> }
> 
> 
> class Tracker extends AppModel
> {
>   public $name = 'Tracker';
>   public $belongsTo = 'User';
>   public $hasMany = array(
>   'Log' => array(
>   'className' => 'Log',
>   'foreignKey' => 'tracker_id',
>   'dependent' => true,
>   'order' => 'Log.created DESC'));
> 
>   public $validate = array(
>   'name' => 'alphaNumeric',
>   );
> }
> 
> 
> class Log extends AppModel
> {
>   public $name = 'Log';
>   public $belongsTo = 'Tracker';
>   public $validate = array();
> }
> 
> 
> Basically you can see that each user has a tracker, and each tracker
> has a log. The problem is that when I'm displaying trackers, I want to
> display for each user ONLY the trackers that he or she owns. But
> instead of doing that, it returns back all the trackers, including
> ones that the user doesn't own. This is the code that I'm using:
> 
> // inside of app/controllers/user_controller.php
> 
> $this->set('trackers', $this->User->Tracker->find('all'));
> $this->set('totalTrackers', $this->User->Tracker->find('count'));
> 
> 
> Can someone help me solve the problem?
> Thanks.
> 
> Check out the new CakePHP Questions site http://cakeqs.org and help others 
> with their CakePHP related questions.
> 
> 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+unsubscr...@googlegroups.com For more options, visit this group at 
> http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Model associations problem

2010-06-17 Thread ryanl
I'm in the process of creating my first web application with CakePHP,
however I've run into some problems. I'll just start with the code:


class User extends AppModel
{
public $name = 'User';
public $belongsTo = 'Group';
public $hasMany = array(
'Tracker' => array(
'className' => 'Tracker',
'foreignKey' => 'user_id',
'dependent' => true,
'order' => 'Tracker.created DESC'));

public $validate = array(
'username' => 'alphaNumeric',
'password' => 'notEmpty'
);
}


class Tracker extends AppModel
{
public $name = 'Tracker';
public $belongsTo = 'User';
public $hasMany = array(
'Log' => array(
'className' => 'Log',
'foreignKey' => 'tracker_id',
'dependent' => true,
'order' => 'Log.created DESC'));

public $validate = array(
'name' => 'alphaNumeric',
);
}


class Log extends AppModel
{
public $name = 'Log';
public $belongsTo = 'Tracker';
public $validate = array();
}


Basically you can see that each user has a tracker, and each tracker
has a log. The problem is that when I'm displaying trackers, I want to
display for each user ONLY the trackers that he or she owns. But
instead of doing that, it returns back all the trackers, including
ones that the user doesn't own. This is the code that I'm using:

// inside of app/controllers/user_controller.php

$this->set('trackers', $this->User->Tracker->find('all'));
$this->set('totalTrackers', $this->User->Tracker->find('count'));


Can someone help me solve the problem?
Thanks.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Model associations problem

2009-09-21 Thread Aivaras
Hey folks,

There is this problem I am facing at the moment. Not sure how to solve it.

So, my Gallery

var $hasOne = array(
'MainPhoto' => array(
'className' => 'Upload',
'foreignKey' => false,
'fields' => array('MainPhoto.filename'),
'conditions' => array('Gallery.main_photo_id =
MainPhoto.id'),
)
);
var $hasMany = array(
'GalleryPhoto'
);

and my GalleryPhoto

var $hasOne = array(
'GP' => array(
'className' => 'Upload',
'foreignKey' => false,
'conditions' => 'GalleryPhoto.photo_id = GP.id',
'fields' => array('GP.id', 'GP.filename'),
)
);

However, when I do

$this->Gallery->recursive = 2;
$this->set('gallery', $this->Gallery->read(null, $id));

in my GalleriesController the result is not what I expected.

Queries:

SELECT `Gallery`.`id`, `Gallery`.`name`, `Gallery`.`main_photo_id`,
`MainPhoto`.`filename` FROM `galleries` AS `Gallery` LEFT JOIN `uploads` AS
`MainPhoto` ON (`Gallery`.`main_photo_id` = `MainPhoto`.`id`) WHERE
`Gallery`.`id` = 7 LIMIT 1
SELECT `MainPhoto`.`filename` FROM `uploads` AS `MainPhoto` WHERE 1 = 1
SELECT `GalleryPhoto`.`id`, `GalleryPhoto`.`position`,
`GalleryPhoto`.`gallery_id`, `GalleryPhoto`.`photo_id` FROM `gallery_photos`
AS `GalleryPhoto` WHERE `GalleryPhoto`.`gallery_id` = (7)
SELECT `GP`.`id`, `GP`.`filename` FROM `uploads` AS `GP` WHERE
`GalleryPhoto`.`photo_id` = `GP`.`id` 1054: Unknown column
'GalleryPhoto.photo_id' in 'where clause'

as far as you can see the last one is triggering a error, any ideas how do I
left join the photo filename I need?

Thanks in advance,
Aivaras

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Model associations problem

2007-08-07 Thread Luiz Paulo Nascimento

Did you already try 'conditions' => 'users.deleted = "0"' ?

The real table name is 'users', not 'User'...

[ ]'s
LP

2007/8/5, Ruud Gâst <[EMAIL PROTECTED]>:
>
> Hello there,
>
> I've got a simple construction of two models: users and familymembers;
> every user belongs to a familymember and a familymember has one user.
> So the database looks like this:
>
> `familymembers` (
>   `id` int(11) auto_increment,
>   `firstname` varchar(20),
>   `lastname` varchar(100),
>   `gender` tinyint(4),
>   `dob` date,
>   `email` varchar(100),
>   `created` datetime,
>   `deleted` tinyint(1)
> );
> `users` (
>   `id` int(11) auto_increment,
>   `familymember_id` int(11),
>   `username` varchar(20),
>   `password` varchar(255),
>   `deleted` tinyint(1),
>   `created` datetime
> );
>
> I'm using a hasOne association in the Familymember model, so it will
> only return one record ... great! But now I want to be able to keep
> old records, so if a user would be removed Users.deleted will be set
> to 1. Also great, so I thought, just fix the association so it will
> only return records where Users.deleted = 0. So I did that like this:
>
> var $hasOne = array(
>   'User' =>
> array(
>   'className' => 'User',
>   'conditions' => 'User.deleted = 0'
> )
>   );
>
> The CakePHP Manual says the following about hasOne associations:
>
> "We could use this to tell Cake to only associate a Profile that has a
> green header, if we wished. To define conditions like this, you'd
> specify a SQL conditions fragment as the value for this key:
> "Profile.header_color = 'green'"."  -http://manual.cakephp.org/
> chapter/models
>
> So in my case this model should only associate a Familymember that has
> not been deleted. But this simply doesn't work, Cake ignores the
> 'conditions'-part alltogether. I even tried to make conditions that
> would generate errors ... didn't get an error and none of the
> 'conditions' are included in the SQL-query.
>
> I've googled around to see if anyone else has a similar problem or
> similar system explained but haven't had any luck so far. So is there
> anyone who can tell me what I'm doing wrong, or even better, tell me
> how to do this the right way.
>
> Thanks!
>
> By the way: I'm using PHP 4.4 and CakePHP 1.1.14.4797
>
>
> >
>


-- 
Luiz Paulo Nascimento
[EMAIL PROTECTED]

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Model associations problem

2007-08-07 Thread Ruud Gâst

Well, the condition is not included in the SQL. So that is why I'm not
getting any errors when I create a condition that should create an
error...

I have removed all files in app/tmp/cache/models, if that is the right
way to clear the model caches then I have. If not, how can I do that?

Thanks for your reply,
Ruud


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Model associations problem

2007-08-06 Thread John David Anderson (_psychic_)


On Aug 6, 2007, at 4:49 PM, Ruud Gâst wrote:

>
> No-one has an answer? I'd say a lot of people use the same
> construction so it won't be that hard ... I just don't know how.

What happens when you turn DEBUG up to 2 or so? Does the SQL include  
the condition?

Have you tried clearing the model caches found in app/tmp/cache ?

-- John

>
> On Aug 6, 3:01 am, Ruud Gâst <[EMAIL PROTECTED]> wrote:
>> Hello there,
>>
>> I've got a simple construction of two models: users and  
>> familymembers;
>> every user belongs to a familymember and a familymember has one user.
>> So the database looks like this:
>>
>> `familymembers` (
>>   `id` int(11) auto_increment,
>>   `firstname` varchar(20),
>>   `lastname` varchar(100),
>>   `gender` tinyint(4),
>>   `dob` date,
>>   `email` varchar(100),
>>   `created` datetime,
>>   `deleted` tinyint(1)
>> );
>> `users` (
>>   `id` int(11) auto_increment,
>>   `familymember_id` int(11),
>>   `username` varchar(20),
>>   `password` varchar(255),
>>   `deleted` tinyint(1),
>>   `created` datetime
>> );
>>
>> I'm using a hasOne association in the Familymember model, so it will
>> only return one record ... great! But now I want to be able to keep
>> old records, so if a user would be removed Users.deleted will be set
>> to 1. Also great, so I thought, just fix the association so it will
>> only return records where Users.deleted = 0. So I did that like this:
>>
>> var $hasOne = array(
>>   'User' =>
>> array(
>>   'className' => 'User',
>>   'conditions' => 'User.deleted = 0'
>> )
>>   );
>>
>> The CakePHP Manual says the following about hasOne associations:
>>
>> "We could use this to tell Cake to only associate a Profile that  
>> has a
>> green header, if we wished. To define conditions like this, you'd
>> specify a SQL conditions fragment as the value for this key:
>> "Profile.header_color = 'green'"."  -http://manual.cakephp.org/
>> chapter/models
>>
>> So in my case this model should only associate a Familymember that  
>> has
>> not been deleted. But this simply doesn't work, Cake ignores the
>> 'conditions'-part alltogether. I even tried to make conditions that
>> would generate errors ... didn't get an error and none of the
>> 'conditions' are included in the SQL-query.
>>
>> I've googled around to see if anyone else has a similar problem or
>> similar system explained but haven't had any luck so far. So is there
>> anyone who can tell me what I'm doing wrong, or even better, tell me
>> how to do this the right way.
>>
>> Thanks!
>>
>> By the way: I'm using PHP 4.4 and CakePHP 1.1.14.4797
>
>
> >


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Model associations problem

2007-08-06 Thread Ruud Gâst

No-one has an answer? I'd say a lot of people use the same
construction so it won't be that hard ... I just don't know how.

On Aug 6, 3:01 am, Ruud Gâst <[EMAIL PROTECTED]> wrote:
> Hello there,
>
> I've got a simple construction of two models: users and familymembers;
> every user belongs to a familymember and a familymember has one user.
> So the database looks like this:
>
> `familymembers` (
>   `id` int(11) auto_increment,
>   `firstname` varchar(20),
>   `lastname` varchar(100),
>   `gender` tinyint(4),
>   `dob` date,
>   `email` varchar(100),
>   `created` datetime,
>   `deleted` tinyint(1)
> );
> `users` (
>   `id` int(11) auto_increment,
>   `familymember_id` int(11),
>   `username` varchar(20),
>   `password` varchar(255),
>   `deleted` tinyint(1),
>   `created` datetime
> );
>
> I'm using a hasOne association in the Familymember model, so it will
> only return one record ... great! But now I want to be able to keep
> old records, so if a user would be removed Users.deleted will be set
> to 1. Also great, so I thought, just fix the association so it will
> only return records where Users.deleted = 0. So I did that like this:
>
> var $hasOne = array(
>   'User' =>
> array(
>   'className' => 'User',
>   'conditions' => 'User.deleted = 0'
> )
>   );
>
> The CakePHP Manual says the following about hasOne associations:
>
> "We could use this to tell Cake to only associate a Profile that has a
> green header, if we wished. To define conditions like this, you'd
> specify a SQL conditions fragment as the value for this key:
> "Profile.header_color = 'green'"."  -http://manual.cakephp.org/
> chapter/models
>
> So in my case this model should only associate a Familymember that has
> not been deleted. But this simply doesn't work, Cake ignores the
> 'conditions'-part alltogether. I even tried to make conditions that
> would generate errors ... didn't get an error and none of the
> 'conditions' are included in the SQL-query.
>
> I've googled around to see if anyone else has a similar problem or
> similar system explained but haven't had any luck so far. So is there
> anyone who can tell me what I'm doing wrong, or even better, tell me
> how to do this the right way.
>
> Thanks!
>
> By the way: I'm using PHP 4.4 and CakePHP 1.1.14.4797


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---



Model associations problem

2007-08-05 Thread Ruud Gâst

Hello there,

I've got a simple construction of two models: users and familymembers;
every user belongs to a familymember and a familymember has one user.
So the database looks like this:

`familymembers` (
  `id` int(11) auto_increment,
  `firstname` varchar(20),
  `lastname` varchar(100),
  `gender` tinyint(4),
  `dob` date,
  `email` varchar(100),
  `created` datetime,
  `deleted` tinyint(1)
);
`users` (
  `id` int(11) auto_increment,
  `familymember_id` int(11),
  `username` varchar(20),
  `password` varchar(255),
  `deleted` tinyint(1),
  `created` datetime
);

I'm using a hasOne association in the Familymember model, so it will
only return one record ... great! But now I want to be able to keep
old records, so if a user would be removed Users.deleted will be set
to 1. Also great, so I thought, just fix the association so it will
only return records where Users.deleted = 0. So I did that like this:

var $hasOne = array(
  'User' =>
array(
  'className' => 'User',
  'conditions' => 'User.deleted = 0'
)
  );

The CakePHP Manual says the following about hasOne associations:

"We could use this to tell Cake to only associate a Profile that has a
green header, if we wished. To define conditions like this, you'd
specify a SQL conditions fragment as the value for this key:
"Profile.header_color = 'green'"."  -http://manual.cakephp.org/
chapter/models

So in my case this model should only associate a Familymember that has
not been deleted. But this simply doesn't work, Cake ignores the
'conditions'-part alltogether. I even tried to make conditions that
would generate errors ... didn't get an error and none of the
'conditions' are included in the SQL-query.

I've googled around to see if anyone else has a similar problem or
similar system explained but haven't had any luck so far. So is there
anyone who can tell me what I'm doing wrong, or even better, tell me
how to do this the right way.

Thanks!

By the way: I'm using PHP 4.4 and CakePHP 1.1.14.4797


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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
-~--~~~~--~~--~--~---