Re: Make database calls inside of component

2006-04-08 Thread bigdog

I get it now!!  Using 'products_id' will not work with Cake.  You have
to use 'id' as your field name.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Make database calls inside of component

2006-04-08 Thread bigdog

Hi,

I am not having succes getting my component to update a record.

Insert works with this code if I don't pass a $product_id.  How do I
force an update?

class CostsController extends AppController
{
var $name = 'costs';
var $components=array("Findcost");


   function index()
   {
$col=array();
$col['products_id']=1;
$this->Findcost->calculate('Cost', $col);   }
   } //end of index


class FindcostComponent
{
   var $controller; //gives access to controller


   function startup(& $controller) //gives access to controller
   {
$this->controller =&$controller;
   } //startup

   function calculate(  $mymodel, $othercols=null  )
   {
   //do some calculations
  $myarray=array();
  $myarray['itemcost']=100;
  //merge product_id from othercols wth myarray to do the update
  if( isset( $othercols ) )
  $result = array_merge($myarray, $othercols);
  else  $result=$myarray;
   //I want to update the table here
   $this->controller->$mymodel->save($result);
   }//calculate



} //end of component


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Make database calls inside of component

2006-04-08 Thread bigdog

Thank you both for your assistance.  Last night I was just trying to
mock up what I could do.  The reason why I want to work solely in my
component is for reuse.  I want to be able to use this component in
more than 1 controller, but the controllers will use this component in
different ways.

I will get started now.  Thanks again,  Bill


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Make database calls inside of component

2006-04-08 Thread DingoNV

perhaps your cost calculation should be in the products model.
it's not taboo to put custom methods into the model, as long as it is
very closely related to the model data.

if you absolutely have to call it from the controller then give your
component a controller param, and in the calling controller do this
$this->Findcost->controller = $this;
then you have a reference back to the controller from in the component
then when you call the method from the controller
$this->Findcost->calculate

your component can access the model
$this->controller->MyModel->findAll()

it can be done, and that's how you do it.
but for the circumstance you give an example too, this is definitely
the wrong way to do it
it should either be controller method, which in this case seems best,
or in other circumstances a model method


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Make database calls inside of component

2006-04-07 Thread AD7six

Hi Bill,

First a double check: Is the component Findcost only related to the
controller Costs? If so it might be easier to manage and cleaner code
to write the functionality into a method on the Costs controller.

If it is used by more than one controller then I think will do what you
want:

class CostsController extends AppController
{
var $components = array('FindCost');

function index()
   {
$this->FindCost->calculate( ???);
   }
// Rest of code

class FindcostComponent
{
   var $model;
   var $myarray = array();

function startup($controller) {
$this-> model = new NameOfModel; // Not sure if there's a way
to tie this in automagially
}

   function calculate(  ???  )
   {
  $this->myarray[$itemcost]=100;
   $this->model->save($this->myarr);
}

Cheers,

AD7six
Looking for CakePHP info?
Find it faster here: http://www.cakephp.org/search/


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Make database calls inside of component

2006-04-07 Thread bigdog

Is there a way to access the Model, to make Database calls inside of a
component?

class CostsController extends AppController
{
var $name = 'Costs';
var $components=array("Findcost");


   function index()
   {
//Do i send a reference to model this way?
$this->Component->calculate( ???);
   }
}

class FindcostComponent
{
   var $model;
   var $myarray = array();

//Or is there a way to retrieve the model reference here
 //like you do
// with the startup function
//to access the controller?

   function calculate(  ???  )
   {
   //do some calculations
  $this->myarray[$itemcost]=100;
   //I want to save to table here
   $this->model->save($this->myarr);
}

Thanks for your help,

Bill


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---