Re: HABTM confusion

2009-07-21 Thread stevel

I have solved my problem. Basically I changed my code from

$advertisers = $this-paginate('TblDetail', array
('TblDetail.ap_advertiser LIKE'=%.$searchstr.%));

 to

$advertisers = $this-paginate($this-TblDetail-TblAdCategory, array
('TblDetail.ap_advertiser LIKE'=%.$searchstr.%));

and I got the result I was looking for.



On Jul 20, 9:46 pm, stevel isig...@gmail.com wrote:
 Been trying to make sense of HABTM relationship. Hope someone could
 help throw some light on this.
 I have the following models defined:

 class TblCategory extends AppModel {

         var $name = 'TblCategory';
         var $hasAndBelongsToMany = array('TblDetail'=array(
                                                 'className' =
 'TblDetail',
                                                 'joinTable' =
 'tbl_ad_categories',
                                                 'with' =
 'TblAdCategory',
                                                 'foreignKey' =
 'tbl_category_id',

 'associationForeignKey' = 'tbl_detail_id'
                                                ));

 }

 class TblDetail extends AppModel {

         var $name = 'TblDetail';
         var $hasAndBelongsToMany = array('TblCategory'=array(
                                                'className' =
 'TblCategory',
                                                'joinTable' =
 'tbl_ad_categories',
                                                'with' =
 'TblAdCategory',
                                                'foreignKey' =
 'tbl_detail_id',
                                                'associationForeignKey'
 = 'tbl_category_id'
                                                ));

 }

 class TblAdCategory extends AppModel {

         var $name = 'TblAdCategory';
         var $belongsTo = array('TblDetail','TblCategory');

 }

 In my controller I have the following statement:

 $advertisers = $this-paginate('TblDetail', array
 ('TblDetail.ap_advertiser LIKE'=%.$searchstr.%));

 where $searchstr is merely a value passed in from a form.

 When I do a pr($advertisers) it shows the [TblDetail] Array as
 expected. But the associated [TblCategory] shows an empty array even
 though there is related TblCategory records.

 Been trying to figure out where I have gone wrong. How do I get the
 associated TblCategory record as well?
 Any help is greatly appreciated.
 Thanks.
--~--~-~--~~~---~--~~
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 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



HABTM confusion

2009-07-20 Thread stevel

Been trying to make sense of HABTM relationship. Hope someone could
help throw some light on this.
I have the following models defined:

class TblCategory extends AppModel {

var $name = 'TblCategory';
var $hasAndBelongsToMany = array('TblDetail'=array(
'className' =
'TblDetail',
'joinTable' =
'tbl_ad_categories',
'with' =
'TblAdCategory',
'foreignKey' =
'tbl_category_id',
 
'associationForeignKey' = 'tbl_detail_id'
   ));
}

class TblDetail extends AppModel {

var $name = 'TblDetail';
var $hasAndBelongsToMany = array('TblCategory'=array(
   'className' =
'TblCategory',
   'joinTable' =
'tbl_ad_categories',
   'with' =
'TblAdCategory',
   'foreignKey' =
'tbl_detail_id',
   'associationForeignKey'
= 'tbl_category_id'
   ));
}

class TblAdCategory extends AppModel {

var $name = 'TblAdCategory';
var $belongsTo = array('TblDetail','TblCategory');

}


In my controller I have the following statement:

$advertisers = $this-paginate('TblDetail', array
('TblDetail.ap_advertiser LIKE'=%.$searchstr.%));

where $searchstr is merely a value passed in from a form.

When I do a pr($advertisers) it shows the [TblDetail] Array as
expected. But the associated [TblCategory] shows an empty array even
though there is related TblCategory records.

Been trying to figure out where I have gone wrong. How do I get the
associated TblCategory record as well?
Any help is greatly appreciated.
Thanks.


--~--~-~--~~~---~--~~
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 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



HABTM confusion was: How to add item using HABTM relation

2008-12-04 Thread Zeugme

Hi Rob and thanks,

I did already what you show but the issue was on client side, I found  
the solution : name the HTML element with data[Person][Person][0] on a  
contract creation form witch is really not intuitive !

Now back the controller I have to delete a contract. I read here
http://bakery.cakephp.org/articles/view/add-delete-habtm-behavior
a very interesting article but I still don't understand the difference  
between add and delete.

Add :  if I put the right ID in data[Person][Person][0] it work fine
Delete look like an update, so the idea is to put in ata[Person] 
[Person][0], ata[Person][Person][1], ... the ID you want to keep.
= this ends up with an update : fine but then, adding should remove  
the previously associated element, how it can work ???



On 4 Dec 2008, at 03:15, Rob wrote:


 You need to structure the data so that it has the right data and keys
 for each record that will be added.

 Your $this-data['Contract']['Person'] would be an array of the data
 required for the person.

 Since it appears you are doing this in your contracts controller, you
 would need to get the ID for the person, then save the contract just
 as you've shown.

 I do something similar in my code: I have a signup sheet that uses
 the currently logged in user's ID to add a row to the signup slots:

$this-User-create();
if ( $this-User-save($this-data) ){
$this-data['User']['user_id'] = $this-User-
 getLastInsertId();
$this-data['UserSlot']['user_id'] = $this-data
 ['User']['user_id'];

$this-User-UserSlot-create();

if ($this-User-UserSlot-save($this-data
 ['UserSlot'])) {
$user_slot_id = $this-User-UserSlot-
 getLastInsertId();


 Basically I create the user, get the user ID, set the UserSlot's
 user_id, and save the UserSlot ...

 So as long as you set the data for the HATBM id's correctly, the rows
 will be created.

 On Dec 3, 9:57 am, Zeugme [EMAIL PROTECTED] wrote:
 Hi,

 How to add an item to a HABTM relation ?

 I have a Person and I have to add a Contract to that person.
 Here are the data on a view :

 data[Contract][type] : 12
 data[Contract][name] : contract-AAaa
 data[Contract][Person] : 26 // Should I put the person id here ?? or
 something else ??

 In fact, that contract added to person 26 could be later on added to
 another Person, that's why its HABTM.
 Currently, I'm trying to add a new contract to an existing person, so
 the question, but later I'm not sure how to just link an existing
 contract to an existing person ...

 Here is the code on the controller to receive that data :
  function add() {
  if (!empty($this-data)) {
 $this- Contract-create();
 if ($this- Contract-save($this-data)) {
 $this-set('returncode', 'Contract '. 
 $this- Contract-id.'
 saved');
 } else {
 $this-set('returncode', 'ERROR :  
 Contract '.$this-
  data['Contract']['name'].' not saved');
 }
  }
  }

 Thanks !
 


--~--~-~--~~~---~--~~
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: HABTM confusion was: How to add item using HABTM relation

2008-12-04 Thread Rob

Because the HABTM relationship causes Cake to do its' magic, you have
to build the structure with all of the associated data you want to
save.

That article is showing you a behavior that will help you do that more
easily, by giving you methods to do so.

If you set debug to 3, you'll see the SQL that is executed, which will
help you understand what's going on.

On Dec 4, 3:19 am, Zeugme [EMAIL PROTECTED] wrote:
 Hi Rob and thanks,

 I did already what you show but the issue was on client side, I found  
 the solution : name the HTML element with data[Person][Person][0] on a  
 contract creation form witch is really not intuitive !

 Now back the controller I have to delete a contract. I read 
 herehttp://bakery.cakephp.org/articles/view/add-delete-habtm-behavior
 a very interesting article but I still don't understand the difference  
 between add and delete.

 Add :  if I put the right ID in data[Person][Person][0] it work fine
 Delete look like an update, so the idea is to put in ata[Person]
 [Person][0], ata[Person][Person][1], ... the ID you want to keep.
 = this ends up with an update : fine but then, adding should remove  
 the previously associated element, how it can work ???

 On 4 Dec 2008, at 03:15, Rob wrote:



  You need to structure the data so that it has the right data and keys
  for each record that will be added.

  Your $this-data['Contract']['Person'] would be an array of the data
  required for the person.

  Since it appears you are doing this in your contracts controller, you
  would need to get the ID for the person, then save the contract just
  as you've shown.

  I do something similar in my code: I have a signup sheet that uses
  the currently logged in user's ID to add a row to the signup slots:

                 $this-User-create();
                 if ( $this-User-save($this-data) ){
                     $this-data['User']['user_id'] = $this-User-
  getLastInsertId();
                     $this-data['UserSlot']['user_id'] = $this-data
  ['User']['user_id'];

                     $this-User-UserSlot-create();

                     if ($this-User-UserSlot-save($this-data
  ['UserSlot'])) {
                         $user_slot_id = $this-User-UserSlot-
  getLastInsertId();

  Basically I create the user, get the user ID, set the UserSlot's
  user_id, and save the UserSlot ...

  So as long as you set the data for the HATBM id's correctly, the rows
  will be created.

  On Dec 3, 9:57 am, Zeugme [EMAIL PROTECTED] wrote:
  Hi,

  How to add an item to a HABTM relation ?

  I have a Person and I have to add a Contract to that person.
  Here are the data on a view :

  data[Contract][type] : 12
  data[Contract][name] : contract-AAaa
  data[Contract][Person] : 26 // Should I put the person id here ?? or
  something else ??

  In fact, that contract added to person 26 could be later on added to
  another Person, that's why its HABTM.
  Currently, I'm trying to add a new contract to an existing person, so
  the question, but later I'm not sure how to just link an existing
  contract to an existing person ...

  Here is the code on the controller to receive that data :
       function add() {
           if (!empty($this-data)) {
                          $this- Contract-create();
                          if ($this- Contract-save($this-data)) {
                              $this-set('returncode', 'Contract '.
  $this- Contract-id.'
  saved');
                          } else {
                                  $this-set('returncode', 'ERROR :  
  Contract '.$this-
   data['Contract']['name'].' not saved');
                          }
           }
       }

  Thanks !


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