Re: Simple HABTM Question
Hi Dee, The -> Just reverences a Variable or method on an object (if it has () it't a method). In the example above, contractor has a variable Servic, which is set to a Service object. It the chain of object could be avoided by using: var $uses ("Contractor","Service"); $this->Service->find("service_name = 'Web Design'"); Happy coding, Cheers, AD7six --~--~-~--~~~---~--~~ 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: Simple HABTM Question
AD7six, Thanks for this post! I ran into the same problem as brandags. I have a question though. Yes this does work, but why does it work? I thought the arrow (->) referenced a member of the class like a method or member. Is that right? If so how did Service become a member of Contractor? Can you give a brief explanation? Thanks, Dee --~--~-~--~~~---~--~~ 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: Simple HABTM Question
Ha! What do you know - that was it! Thank you! --~--~-~--~~~---~--~~ 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: Simple HABTM Question
ThereĀ“s a reason I wrote "something like" :). I use code very similar to this, but I wrote it here off the top of my head. Try: $result = $this->Contractor->Service->findById($service_id); Note the lowercase D, I don`t know if that's the reason for the underscore, but it could be. Cheers, AD7six --~--~-~--~~~---~--~~ 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: Simple HABTM Question
Sorry for the double post there. I also tried querying by the service_name rather than the id, and that seemed to work better. I also changed the loop a little bit (because of how I'm grabbing the data in my view). This seems to work great. (Although, as you mentioned, it's not sorted) $result = $this->Contractor->Service->find("service_name = 'Web Design'"); if ($result['Contractor']) { foreach ($result['Contractor'] as $r) { $data[]['Contractor'] = $r; } $this->set('data', $data); } else { $this->set('data', Array()); } --~--~-~--~~~---~--~~ 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: Simple HABTM Question
I get a very strange result when I do that. I did have to singularize the Model names as well: $result = $this->Contractor->Service->findByID($service_id); SQL Error in model Service: 1054: Unknown column 'Service.i_d' in 'where clause' The query that's run is this: SELECT `Service`.`id`, `Service`.`service_name` FROM `services` AS `Service` WHERE (`Service`.`i_d` = 2) LIMIT 1 Why is the 'id' split up like 'i_d'? --~--~-~--~~~---~--~~ 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: Simple HABTM Question
I get a very strange result when I do that. I did have to singularize the Model names as well: $result = $this->Contractor->Service->findByID($service_id);The query that's run is this:SELECT `Service`.`id`, `Service`.`service_name` FROM `services` AS `Service` WHERE (`Service`.`i_d` = 2) LIMIT 1Why is the 'id' split up like 'i_d'? --~--~-~--~~~---~--~~ 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: Simple HABTM Question
Hi Brendon, Try something like this: // What do I need to put here to return all the contractors linked to $service_id? $result = $this->Contractors->Services->FindByID($service_id); if ($result) { foreach ($result['Contractor'] as $Contractor) { $data[] = Array('Contractor'=>$Contractor); } $this->set('data', $data); } else { $this->set('data', Array()); } This gives me an opportunity to ask a question of my own: There is no sort order applied at all to the sql for this kind of search - how can the results be sorted. Cheers, AD7six --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Simple HABTM Question
I have two tables: contractors = id first_name last_name services == id service_name Then a table to join them together in a many-to-many relationship: contractors_services === id contractor_id service_id In other words, a contractor can be associated with many services, and a service can be associated with multiple contractors. All I want is a list of all the contractors for a particular service. In regular SQL, the query would look like this: SELECT c.* FROM contractors c JOIN contractors_services cs ON cs.contractor_id = c.id WHERE cs.service_id = 2; /* Where 2 is the service_id you're looking for */ So, how do I get this to work using the Cake method? I have tried: $data = $this->Contractor->findAll('contractors_services.service_id = '.$service_id); But it doesn't like that. Look at the query it wrote, it didn't actually join the services table at all. Here is my code: class Contractor extends AppModel { var $name = 'Contractor'; var $hasAndBelongsToMany = 'Service'; } class Service extends AppModel { var $name = 'Service'; // required for php4 var $hasAndBelongsToMany = 'Contractor'; } class ContractorsController extends AppController { var $name = 'Contractors'; // for php4 var $helpers= array('Html', 'Javascript'); function index($service_id='') { // What do I need to put here to return all the contractors linked to $service_id? $data = $this->Contractor->findAll(); //'service_id = '.$service_id); $this->set('data', $data); $this->render(); } } Can anyone see what I'm doing wrong or what more I need to do to be able to get this simple query to work? Thank you, Brandon --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---