0

I am developing my application with cakephp 2.4.6. i know there are lots of posts in the stackoverflow related to saveAssociated issue.I have implemented several HABTM relationship in my database and now i am struggling a lot because of its unexpected behaviour. I have

                Model  Details

               TEmployeeProfile HABTM TAddress
               TEmPloyeeProfile  HABTM TUserGroup

my table names are

          employee_profiles , t_addresses,t_addresses_employee_profiles,
           t_user_groups,employee_profiles_t_user_groups

And my $this->request->data contain

             array(
                 'EmployeeProfile' => array(
                         'first_name' => 'Emppppp',
                          'last_name' => 'kjljj',
                          't_login_id' => '222'
                                        ),
                  'TUserGroup' => array(
                                 (int) 0 => '9',
                                (int) 1 => '13'
                                   ),
              'TAddress' => array(
                               (int) 0 => array(
                                'number_street' => 'emmmm',
                                'area' => '545454',
                                'state' => '2',
                                'city' => '3',
                                                ),
                               (int) 1 => array(
                                   'number_street' => 'empppppp',
                                   'area' => 'nkjk',
                                   'state' => '2',
                                   'city' => '3',
                                  )
                                    )  
                                      )       

NOW When i try to save using

              $this->EmployeeProfile->saveAssociated($this->request->data)

    it will save only EmployeeProfile and TUserGroup Details

Ans When i try t save using

              $this->EmployeeProfile->saveALl($this->request->data)
         it will save only EmployeeProfile Details..

Whats Wrong With me.. i am stucking everywhere... because every whhere i am using this like relationship . if i can solve this then only i can do the remaining sections. please help me... i have Posted my whole code here COMPLETE CODE

SibinF
  • 385
  • 1
  • 7
  • 25
  • please help me iam stucking a lott – SibinF Apr 30 '14 at 10:55
  • what is the relation between TEmployeeProfile and EmployeeProfile? Is it $hasOne? – Fazal Rasel Apr 30 '14 at 15:58
  • @FazalRasel if you know the answer pls reply – SibinF May 01 '14 at 04:49
  • there might be minor error...Please, update your question with tables names, model, controller and view... Or you can pastebin all those and provide a link here.. – Fazal Rasel May 01 '14 at 04:56
  • and most probably its a model naming issue.. – Fazal Rasel May 01 '14 at 04:59
  • @FazalRasel i have updated my question as you suggested..please give me any solution..iam stucking. – SibinF May 01 '14 at 10:07
  • Well I would like to help you but i need to see all your code and that's why I asked for all your code including `controller`, `view` not just the `model` code. Your question will be very big and messy if you place everything here.. Paste to pastebin.com‎ and give a link on comments. – Fazal Rasel May 01 '14 at 20:04
  • @FazalRasel http://pastebin.com/s0niDZs6 please look at this . i have posted my code – SibinF May 02 '14 at 10:46

1 Answers1

1

As I understand your question that you need to rethink about the model association. Thus a TEmployeeProfile can take many TAddress then you can link model as-

TEmployeeProfile.php model:

<?php
class TEmployeeProfile extends AppModel{
    public $useTable = 't_employee_profile';

    public $belongsTo = array(
        'CommunicationTAddress' => array(
            'className' => 'TAddress',
            'foreignKey' => 'communication_taddress_id'
        ),
        'HomeTAddress' => array(
            'className' => 'TAddress',
            'foreignKey' => 'home_taddress_id'
        )
    );
    public  $hasAndBelongsToMany = array('TUserGroup');
} 

Table schema for TEmployeeProfile:

CREATE TABLE t_employee_profile
(
    id INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
    first_name VARCHAR(50) NOT NULL,
    communication_taddress_id INT UNSIGNED NOT NULL, // this column is mandatory 
    home_taddress_id INT UNSIGNED NOT NULL, // this column is mandatory
    //and your other columns
);

TEmployeeProfilesController.php controller:

    public function add(){
        if($this->request->data){
            if($this->TEmployeeProfile->saveAll($this->request->data)){
              $this->Session->setFlash('data saved');
                //redirect to another place
            }else{
                //what you wants to do if save fails
            }

        }
        $TUserGroup = $this->TEmployeeProfile->TUserGroup->find('list');
        $this->set('tUserGroups', $TUserGroup);
    }
} 

add.ctp view

<?php
echo $this->Form->create();

// t_employee_profile
echo $this->Form->input('first_name');

//t_address as CommunicationTAddress
echo $this->Form->input('CommunicationTAddress.street');
echo $this->Form->input('CommunicationTAddress.city');


//t_address as HomeTAddress
echo $this->Form->input('HomeTAddress.street');
echo $this->Form->input('HomeTAddress.city');

//t_user_group
echo $this->Form->input('TUserGroup');

echo $this->Form->end('save');
Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
  • idea is good, thanks man. But i told you , the address table is a generic table, it will use everybody . ok anyway i will implement this. – SibinF May 06 '14 at 10:37