Welcome to Discussion Forum, where you can ask questions and receive answers from other members of the community.

How to associated table CakePHP: Model A is not associated with model B

1 like 0 dislike
I have tutor_students and users table. tutor_students contains id, tutor_id, student_id and created. users contain id, user_id, role_id, email, first_name, last_name. tutor_students table is connected table between tutor and student. so tutor can see their student list and can add new student too. role_id serves as authentication. role_id 2 for tutors and 3 for students.
 
I succeeded to display student list and add new student to tutor student list. but when i want to search student, there will be an error:
 
Model "TutorStudent" is not associated with model "User"
this is my controller:
 
public function admin_subadmin_add_tutor($tutor_id = null) 
{
    $user_list = '';        
    $this->loadModel('TutorStudent');   
    $this->set('title_for_layout','User');  
    $this->User->bindModel(array(
                    'belongsTo'=>array(
                        'Standard'=>array(
                            'className'=>'Standard'
                        ),
                    )
    ),false);
 
    $this->User->id     =   $tutor_id;
    /*form post and check conditions*/
    // if ($this->request->is('post') || $this->request->is('put')) 
    if(!empty($this->request->data) && isset($this->request->data['User']['status1']))
    {   
        // pr($this->request->data);die;
        $action = $this->request->data['User']['pageAction'];
        foreach ($this->request->data['User'] AS $value) {
            if ($value != 0) {
                $ids[] = $value;
            }
        }
 
        // pr($ids);die;
        if (!empty($this->request->data)) 
        {
            if (!isset($this->request->params['_Token']['key']) || ($this->request->params['_Token']['key'] != $this->request->params['_Token']['key'])) 
            {
                $blackHoleCallback = $this->Security->blackHoleCallback;
                $this->$blackHoleCallback();
            }
            foreach($ids as $id)
            {
                if(!empty($id))
                {
                    if($id != 1)
                    {
                        $shift = array();
                        $shift['TutorStudent']['tutor_id'] = $tutor_id;
                        $shift['TutorStudent']['student_id'] = $id;
                        $this->TutorStudent->save($shift);
                        $this->TutorStudent->id = false;
                    }
                }
            }
 
            $this->Session->setFlash("Tutor has been added successfully", 'admin_flash_good');
            $this->redirect(array('controller'=>'users', 'action'=>'subadmin_tutor',$tutor_id));
 
        }
    } 
 
    if (!isset($this->params['named']['page'])) 
    {
        $this->Session->delete('AdminSearch');
    }
    $email = '';
    $first_name = '';
    $role_id = '';
    $status = '';
    $filters    =   array('TutorStudent.tutor_id'=>$tutor_id);
    if (!empty($this->request->data)) 
    {
        $this->Session->delete('AdminSearch');
        if (isset($this->request->data['User']['first_name']) && $this->request->data['User']['first_name'] != '') 
        {
            $first_name = trim($this->request->data['User']['first_name']);
            $this->Session->write('AdminSearch.first_name', $first_name);               
        }
        if (isset($this->request->data['User']['email']) && $this->request->data['User']['email'] != '') 
        {
            $email = trim($this->request->data['User']['email']);
            $this->Session->write('AdminSearch.email', $email);             
        }
 
    }
 
 
    if ($this->Session->check('AdminSearch')) 
    {
        $keywords   =   $this->Session->read('AdminSearch');
        foreach($keywords as $key=>$values)
        {
            if($key == 'email')
            {
                $email = $values;
                $filters[] = array('User.'.$key.' LIKE'=>"%".$values."%");           &n
asked Mar 30, 2017 in Cake php by harsh (8,710 points)

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Related questions

0 like 0 dislike
0 answers 32 views
0 like 0 dislike
1 answer 36 views
36 views asked Mar 6, 2017 in Prestashop by harsh (8,710 points)
0 like 0 dislike
1 answer 68 views
0 like 0 dislike
0 answers 8,344 views
1 like 0 dislike
0 answers 278 views
278 views asked Mar 30, 2017 in Cake php by harsh (8,710 points)
...