Re: Getting Data from 2 tables

2008-01-07 Thread Travis

Hi Adam,

I am running 1.2 ...

I tried using bind(), but that seems to not work.  The array I am
getting returned contains no Customer records, which is weird.

On Jan 7, 9:17 am, Adam Royle <[EMAIL PROTECTED]> wrote:
> You didn't mention what version you are running, but if you using
> latest 1.2 code you can use:
>
> http://bakery.cakephp.org/articles/view/bindable-behavior-control-you...
>
> If you using earlier version the just use unbindModel and bindModel
> customising fields, etc, before you do your query. Also you might run
> into probs with bindings being reset when using paginate by default.
>
> If you're still not convinced then just write a small sql query and
> call it like:
>
>  $data = $this->Customer->query($sql);
>
> Adam
>
>  On Jan 7, 11:27 pm, Travis <[EMAIL PROTECTED]> wrote:
>
> > bump :)
>
> > On Jan 6, 10:31 pm, Travis <[EMAIL PROTECTED]> wrote:
>
> > > Hey everyone,
>
> > > I'm trying to generate a list of employees and in that list I want to
> > > show the customer which that employee belongs to.  In my db I have an
> > > employees table and a customers table and each employee has a
> > > customer_id field.  The problem is, I want to show the customer.name
> > > field, not the customer_id.
>
> > > How do I run a select statement that joins these two tables on the
> > > customer_id and retrieves only the customer_name from the customers
> > > table.
>
> > > I need to do this from inside the employees controller. Here's my
> > > models and controller code.
>
> > > class EmployeeModel extends AppModel
> > > {
> > > var $name   = "Employee";
> > > var $belongsTo= "Customer";
> > > var $recursive  = 1;
>
> > > }
>
> > > class Customer extends AppModel
> > > {
>
> > > var $name   = "Customer";
> > > var $primaryKey = 'id';
> > > var $hasMany= array('Employee','Transaction');
> > > var $recursive  = 2;
>
> > > }
>
> > > class EmployeesController extends AppController
> > > {
>
> > > var $name = "employees";
> > > var $helpers = array('DatePicker');
> > > var $uses =array('Employee','Customer');
> > > var $paginate = array(
> > > 'limit' => 25,
> > > 'order' => array('Employee.id' => 'DESC')
> > > );
> > > /*
> > >  *
> > >  *
> > >  *
> > >  */
> > > function index(){
>
> > > //retrieve all of the customers for drop down box and set 
> > > the data
> > > $customerList = 
> > > $this->Customer->findAll(NULL,array('id','name'));
> > > foreach ($customerList as $customer)
> > > $customers[$customer['Customer']['id']] = 
> > > $customer['Customer']
> > > ['name'];
> > > $this->set('customers', $customers);
>
> > > //if data is posted, retrieve all employees specific to 
> > > the customer
> > > id
> > > if(!empty($this->data)){
>
> > > $this->layout = 'default';
> > > $dataSet = 
> > > $this->paginate('Employee',array('customer_id' => 
> > > $this->data['Employee']['customer_id']));
>
> > > $this->set(compact('dataSet'));
> > > }
>
> > > //if no data is posted, retrieve all employees
> > > else{
> > > $this->layout = 'default';
> > > $dataSet = $this->paginate('Employee');
> > > $this->set(compact('dataSet'));
>
> > > }
> > > }
>
> > > Thanks for the help.
--~--~-~--~~~---~--~~
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: Getting Data from 2 tables

2008-01-07 Thread Travis

Thanks Chris, but the problem is that when I do a findAll() or a
pagninate() I'm not getting data from both tables even though I have
set my recursive var and my associations.  Any suggestions?

On Jan 7, 9:13 am, "Chris Hartjes" <[EMAIL PROTECTED]> wrote:
> On Jan 6, 2008 11:31 PM, Travis <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hey everyone,
>
> > I'm trying to generate a list of employees and in that list I want to
> > show the customer which that employee belongs to.  In my db I have an
> > employees table and a customers table and each employee has a
> > customer_id field.  The problem is, I want to show the customer.name
> > field, not the customer_id.
>
> You could do this another way by doing a find() for the data set that
> you are looking for, and then use Set::extract to pull out just want
> you want.
>
> Check out this link:
>
> http://www.thinkingphp.org/2007/02/24/cake-12s-set-class-eats-arrays-...
>
> Hope that points you in the right direction.
>
> --
> Chris Hartjes
> Internet Loudmouth
> Motto for 2008: "Moving from herding elephants to handling snakes..."
> @TheKeyBoard:http://www.littlehart.net/atthekeyboard
--~--~-~--~~~---~--~~
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: Getting Data from 2 tables

2008-01-07 Thread Chris Hartjes

On Jan 6, 2008 11:31 PM, Travis <[EMAIL PROTECTED]> wrote:
>
> Hey everyone,
>
> I'm trying to generate a list of employees and in that list I want to
> show the customer which that employee belongs to.  In my db I have an
> employees table and a customers table and each employee has a
> customer_id field.  The problem is, I want to show the customer.name
> field, not the customer_id.

You could do this another way by doing a find() for the data set that
you are looking for, and then use Set::extract to pull out just want
you want.

Check out this link:

http://www.thinkingphp.org/2007/02/24/cake-12s-set-class-eats-arrays-for-breakfast/

Hope that points you in the right direction.

-- 
Chris Hartjes
Internet Loudmouth
Motto for 2008: "Moving from herding elephants to handling snakes..."
@TheKeyBoard: http://www.littlehart.net/atthekeyboard

--~--~-~--~~~---~--~~
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: Getting Data from 2 tables

2008-01-07 Thread Adam Royle

You didn't mention what version you are running, but if you using
latest 1.2 code you can use:

http://bakery.cakephp.org/articles/view/bindable-behavior-control-your-model-bindings

If you using earlier version the just use unbindModel and bindModel
customising fields, etc, before you do your query. Also you might run
into probs with bindings being reset when using paginate by default.

If you're still not convinced then just write a small sql query and
call it like:

 $data = $this->Customer->query($sql);

Adam


 On Jan 7, 11:27 pm, Travis <[EMAIL PROTECTED]> wrote:
> bump :)
>
> On Jan 6, 10:31 pm, Travis <[EMAIL PROTECTED]> wrote:
>
> > Hey everyone,
>
> > I'm trying to generate a list of employees and in that list I want to
> > show the customer which that employee belongs to.  In my db I have an
> > employees table and a customers table and each employee has a
> > customer_id field.  The problem is, I want to show the customer.name
> > field, not the customer_id.
>
> > How do I run a select statement that joins these two tables on the
> > customer_id and retrieves only the customer_name from the customers
> > table.
>
> > I need to do this from inside the employees controller. Here's my
> > models and controller code.
>
> > class EmployeeModel extends AppModel
> > {
> > var $name   = "Employee";
> > var $belongsTo= "Customer";
> > var $recursive  = 1;
>
> > }
>
> > class Customer extends AppModel
> > {
>
> > var $name   = "Customer";
> > var $primaryKey = 'id';
> > var $hasMany= array('Employee','Transaction');
> > var $recursive  = 2;
>
> > }
>
> > class EmployeesController extends AppController
> > {
>
> > var $name = "employees";
> > var $helpers = array('DatePicker');
> > var $uses =array('Employee','Customer');
> > var $paginate = array(
> > 'limit' => 25,
> > 'order' => array('Employee.id' => 'DESC')
> > );
> > /*
> >  *
> >  *
> >  *
> >  */
> > function index(){
>
> > //retrieve all of the customers for drop down box and set 
> > the data
> > $customerList = 
> > $this->Customer->findAll(NULL,array('id','name'));
> > foreach ($customerList as $customer)
> > $customers[$customer['Customer']['id']] = 
> > $customer['Customer']
> > ['name'];
> > $this->set('customers', $customers);
>
> > //if data is posted, retrieve all employees specific to the 
> > customer
> > id
> > if(!empty($this->data)){
>
> > $this->layout = 'default';
> > $dataSet = 
> > $this->paginate('Employee',array('customer_id' => 
> > $this->data['Employee']['customer_id']));
>
> > $this->set(compact('dataSet'));
> > }
>
> > //if no data is posted, retrieve all employees
> > else{
> > $this->layout = 'default';
> > $dataSet = $this->paginate('Employee');
> > $this->set(compact('dataSet'));
>
> > }
> > }
>
> > Thanks for the help.
--~--~-~--~~~---~--~~
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: Getting Data from 2 tables

2008-01-07 Thread Travis

bump :)

On Jan 6, 10:31 pm, Travis <[EMAIL PROTECTED]> wrote:
> Hey everyone,
>
> I'm trying to generate a list of employees and in that list I want to
> show the customer which that employee belongs to.  In my db I have an
> employees table and a customers table and each employee has a
> customer_id field.  The problem is, I want to show the customer.name
> field, not the customer_id.
>
> How do I run a select statement that joins these two tables on the
> customer_id and retrieves only the customer_name from the customers
> table.
>
> I need to do this from inside the employees controller. Here's my
> models and controller code.
>
> class EmployeeModel extends AppModel
> {
> var $name   = "Employee";
> var $belongsTo= "Customer";
> var $recursive  = 1;
>
> }
>
> class Customer extends AppModel
> {
>
> var $name   = "Customer";
> var $primaryKey = 'id';
> var $hasMany= array('Employee','Transaction');
> var $recursive  = 2;
>
> }
>
> class EmployeesController extends AppController
> {
>
> var $name = "employees";
> var $helpers = array('DatePicker');
> var $uses =array('Employee','Customer');
> var $paginate = array(
> 'limit' => 25,
> 'order' => array('Employee.id' => 'DESC')
> );
> /*
>  *
>  *
>  *
>  */
> function index(){
>
> //retrieve all of the customers for drop down box and set the 
> data
> $customerList = 
> $this->Customer->findAll(NULL,array('id','name'));
> foreach ($customerList as $customer)
> $customers[$customer['Customer']['id']] = 
> $customer['Customer']
> ['name'];
> $this->set('customers', $customers);
>
> //if data is posted, retrieve all employees specific to the 
> customer
> id
> if(!empty($this->data)){
>
> $this->layout = 'default';
> $dataSet = 
> $this->paginate('Employee',array('customer_id' => 
> $this->data['Employee']['customer_id']));
>
> $this->set(compact('dataSet'));
> }
>
> //if no data is posted, retrieve all employees
> else{
> $this->layout = 'default';
> $dataSet = $this->paginate('Employee');
> $this->set(compact('dataSet'));
>
> }
> }
>
> Thanks for the help.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---