Re: Help with Model:find()

2008-08-21 Thread Jon Bennett

Hi lauren,

 I overrode the find() method in my Purchase model similar to what Nate
 Abele suggests in http://c7y.phparch.com/c/entry/1/art,mvc_and_cake

woops - knew I should have read that link :p

 However, I think the Containable Behavior is what I need.

yes, it's overkill in your situation to write a custom find method,
Containable does more than you need already.

glad you got it sorted.

cheers,

Jon

-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-21 Thread lauren

On Aug 21, 7:05 am, Jon Bennett [EMAIL PROTECTED] wrote:
 Hi lauren,

  I overrode the find() method in my Purchase model similar to what Nate
  Abele suggests inhttp://c7y.phparch.com/c/entry/1/art,mvc_and_cake

 woops - knew I should have read that link :p

:)

  However, I think the Containable Behavior is what I need.

 yes, it's overkill in your situation to write a custom find method,
 Containable does more than you need already.


Yes, in this instance I was most definitely over thinking it.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-20 Thread lauren

On Aug 18, 8:43 am, Jon Bennett [EMAIL PROTECTED] wrote:
 ...
 a far easier solution to the problem would be to store an interger for
 the status, you could assign things like so:

 0 - unpaid/unprocessed
 1 - paid/processed
 2 - dispatched
 3 - refunded


Before I finally discovered that there were replies to my
question :)I did end up going with a similar solution in order to
avoid hackiness.

Which actually brings me to another question:

Should the following work?:

Account has many Purchase
Purchase belongs to Account

$this-Account-id = 1;
$this-Account-read();

$this-Account-Purchases-find('unpaid');

From Purchases model (overriding find method taken from
http://c7y.phparch.com/c/entry/1/art,mvc_and_cake):
function find($type,
{
...
case 'unpaid':
$params = array_merge($params, array('conditions' =
array('Purchase.status' = 'pending')));
return parent::find('all', $params);
...
}

My hopes were that the above would produce the following SQL (it does
not):

SELECT Purchase.*
FROM purchases AS Purchase LEFT JOIN accounts AS Account ON
(Account.id = Purchase.account_id)
WHERE Purchase.status = 'pending' AND Account.id = 1;
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-20 Thread Jon Bennett

Hi lauren,

 Before I finally discovered that there were replies to my
 question :)I did end up going with a similar solution in order to
 avoid hackiness.

 Which actually brings me to another question:

 Should the following work?:

 Account has many Purchase
 Purchase belongs to Account

 $this-Account-id = 1;
 $this-Account-read();

 $this-Account-Purchases-find('unpaid');

there's no 'magic' method called 'unpaid', there's only:

first - grabs a single record
all - grabs all records
threaded - grabs a simple tree
list - gets a name/value pair array

what you want to do is easy though, with a single call, try:

$this-Account-Behaviors-attach('Containable');

$account = $this-Account-find('first', array(
   'conditions'=array('Account.id' = $account_id),
   'contain'=array(
   'Payment'=array('conditions'=array(
'Payment.status'='pending'
   )))
));

hth

jon


-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-20 Thread Jon Bennett

Hi Lauren,

I meant 'Purchase' not Payment!

$this-Account-Behaviors-attach('Containable');

$account = $this-Account-find('first', array(
   'conditions'=array('Account.id' = $account_id),
   'contain'=array(
   'Purchase'=array('conditions'=array(
'Purchase.status'='pending'
   )))
));

hth

jon

-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-20 Thread lauren

On Aug 20, 6:14 pm, Jon Bennett [EMAIL PROTECTED] wrote:
 there's no 'magic' method called 'unpaid', there's only:

Yes, I know. :)

I overrode the find() method in my Purchase model similar to what Nate
Abele suggests in http://c7y.phparch.com/c/entry/1/art,mvc_and_cake

public function find($type, $options = array()) {
switch ($type) {
  case popular:
return parent::find('all', array_merge(
  array(
'limit' = 10,
'order' = 'Post.view_count DESC'
  ),
  $options
);
  default:
return parent::find($type, $options);
}
}

However, I think the Containable Behavior is what I need.

Thank you both for your help!
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-18 Thread Kalt

For now, you defined the following relations :

- Account hasMany Purchase, Payment
- Purchase hasMany Payment

Right ? Now, try this :

// controllers/purchases_controller.php
function purchases_not_paid($account_id = null)
{
  // We trick the bindings
  $this-Purchase-unbindModel(array('hasMany' = array('Payment')));
  $this-Purchase-bindModel(array('hasOne' = array('Payment')));

  // Results
  $results = $this-Purchase-find('all', array('conditions' =
array('Purchase.account_id' = $account_id, 'Payment.id' = null)));
  $this-set(compact('results'));
}

If you look at the debug, you will see that the SQL query is something
like that :

SELECT Puchase`.`id`, 
FROM `purchases` AS `Puchase`
LEFT JOIN `payments` AS `Payment`
ON (`Payment`.`purchase_id` = `Payment`.`id`)
WHERE `Purchase`.`account_id` = 1
AND `Payment`.`id` IS NULL

On 18 août, 04:36, lauren [EMAIL PROTECTED] wrote:
 Hi,

 Using the Model::find() method seems to be preferred over using
 Model::query(). However, I can't seem to wrap my head around turning
 the following SQL query into params for the find() method:

 Pseudo code: select all the purchases that have not yet been paid for.

 SELECT p.*
 FROM accounts a, purchases p
 WHERE a.id = p.account_id AND p.id NOT IN (SELECT purchase_id FROM
 payments WHERE account_id = 1) AND a.id = 1;
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-18 Thread Jon Bennett

Hi Lauren,

providing you're using 1.2, a less hacky way would be to use the
Containable behaviour.

$this-Purchase-Behaviors-attach('Containable');

$results = $this-Purchase-find('all', array(
'conditions'=array('Purchase.account_id' = $account_id,
'Payment.id' = null),
'contain'=array(
'Payment'=array('conditions'=array(
'Payment.status'='unpaid'
)))
));

http://book.cakephp.org/view/474/containable

hth

jon


-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-18 Thread Kalt

@jon : the unpaid status should not be in the Payments but in the
Purchases

On 18 août, 14:03, Jon Bennett [EMAIL PROTECTED] wrote:
 Hi Lauren,

 providing you're using 1.2, a less hacky way would be to use the
 Containable behaviour.

 $this-Purchase-Behaviors-attach('Containable');

 $results = $this-Purchase-find('all', array(
         'conditions'=array('Purchase.account_id' = $account_id,
 'Payment.id' = null),
         'contain'=array(
                 'Payment'=array('conditions'=array(
                         'Payment.status'='unpaid'
         )))
 ));

 http://book.cakephp.org/view/474/containable

 hth

 jon

 --

 jon bennett
 w:http://www.jben.net/
 iChat (AIM): jbendotnet Skype: jon-bennett
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-18 Thread Jon Bennett

Hi Kalt,

 @jon : the unpaid status should not be in the Payments but in the
 Purchases

ahh, yes - she wants all Purchases that have no payments made - I was
really just trying to illustrate how containable can be used instead
of the old hasOne/belongsTo hack.

a far easier solution to the problem would be to store an interger for
the status, you could assign things like so:

0 - unpaid/unprocessed
1 - paid/processed
2 - dispatched
3 - refunded

no need to tunnel through any related tables, or make any
calculations. I would have thought going about things would greatly
reduce the number of queries/post processing required when outputting
a list of orders as well.

hth

jon



-- 

jon bennett
w: http://www.jben.net/
iChat (AIM): jbendotnet Skype: jon-bennett

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Help with Model:find()

2008-08-18 Thread Kalt

Of course your solution is better and this is what I do too. I just
wanted to show Lauren how to do with her own setup.

On 18 août, 14:43, Jon Bennett [EMAIL PROTECTED] wrote:
 Hi Kalt,

  @jon : the unpaid status should not be in the Payments but in the
  Purchases

 ahh, yes - she wants all Purchases that have no payments made - I was
 really just trying to illustrate how containable can be used instead
 of the old hasOne/belongsTo hack.

 a far easier solution to the problem would be to store an interger for
 the status, you could assign things like so:

 0 - unpaid/unprocessed
 1 - paid/processed
 2 - dispatched
 3 - refunded

 no need to tunnel through any related tables, or make any
 calculations. I would have thought going about things would greatly
 reduce the number of queries/post processing required when outputting
 a list of orders as well.

 hth

 jon

 --

 jon bennett
 w:http://www.jben.net/
 iChat (AIM): jbendotnet Skype: jon-bennett
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---