Wednesday, February 27, 2013

Re: Calling functions in other controllers

Sorry - noticed a couple of typos
1. The $uses variable indicates which other models this controller will use

2. you may need to find the cart you are looking for and pass that information into the total() call

On Wednesday, February 27, 2013 3:03:24 PM UTC+2, david...@gmail.com wrote:

On Wednesday, February 27, 2013 2:55:53 AM UTC+2, lirc201 wrote:
I'm in 2 different controllers.  In the checkout controller, I'm trying to call a function in the cart controller.

Are you sure that the function is in the cart controller?  The $uses variable indicates which other models this model will use (not which other controllers).  If you want to call a method on another controller you will need to use either App::Import or RequestAction.  If the error you are getting is an SQL error rather than a method not found error it would seem like the get() method is on the model.

In general, many people will tell you that calling one controller from another is a bad idea and should only be done as a last resort.

If it is true that you are managing to call a controller method then it may be simpler to move these methods into the Cart model which you would then be able to access from the $uses variable. 

If the methods are really on the model (or if you choose to move them there) you will need to be acting on a particular instance of Cart so you would do something like:
$this->Cart->id = $CartID; // assumes you already have the id of the cart you want
$Cart = $this->Cart->get(); // assumes that the only state information that get needs is the id and will read any additional information for itself

Alternatively, you may need to find the cart you are looking for and pass that information into the get call.  I.e.
$Cart = $this->Cart->find('first', array('conditions' => array('Cart.customer_id' => $CustomerID)));
$Total = $this->Cart->total($Cart);

As a side note:
One debugging method you could use when confronted with this type of situation is to copy and paste the query that generated the error directly into something like PhpMyAdmin and see what part of the query is causing the SQL error.  My guess is that in your case the query has a where clause with 'WHERE Cart.id=' with no id specified or something like that.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: