2016-01-06 10 views
5

में हैसनी एसोसिएशन डेटा सहेजना मेरे पास दो टेबल हैं। मेरी प्राथमिक तालिका छात्र है। और मेरी माध्यमिक तालिका परीक्षा है। मैं का उपयोग कर दोनों टेबलों को सहेजने की कोशिश कर रहा हूं कई और संबंधित ToMany एसोसिएशन। लेकिन यह छात्र तालिका में डेटा सहेज रहा है, परीक्षा में नहीं। क्या कोई इस समस्या के समाधान में मेरी मदद कर सकता है।केकपीएचपी 3.x

छात्र मॉडल:

class StudentsTable extends Table { 
    public function initialize(array $config) { 
    $this->addBehavior('Timestamp'); 
    parent::initialize($config); 
    $this->table('students'); 
    $this->primaryKey(['id']); 
    $this->hasMany('Exams', [ 
     'className' => 'Exams', 
     'foreignKey' => 'student_id', 
     'dependent'=>'true', 
     'cascadeCallbacks'=>'true']); 
    } 
} 

परीक्षा मॉडल:

class ExamsTable extends Table { 
    public function initialize(array $config) { 
    parent::initialize($config); 
    $this->table('exams'); 
    $this->primaryKey(['id']); 
    $this->belongsToMany('Students',[ 
     'className'=>'Students', 
     'foreignKey' => 'subject_id', 
     'dependent'=>'true', 
     'cascadeCallbacks'=>'true']); 
    } 
} 

मेरे school.ctp:

echo $this->Form->create(); 
echo $this->Form->input('name'); 
echo $this->Form->input('exams.subject', array(
    'required'=>false, 
    'multiple' => 'checkbox', 
    'options' => array(
    0 => 'Tamil', 
    1 => 'English', 
    2 => 'Maths'))); 
echo $this->Form->button(__('Save')); 
echo $this->Form->end(); 

मेरी नियंत्रक में:

public function school() { 
    $this->loadModel('Students'); 
    $this->loadModel('Exams'); 
    $student = $this->Students->newEntity(); 
    if ($this->request->is('post')) { 
    $this->request->data['exams']['subject'] = 
     implode(',',$this->request->data['exams']['subject']); 
    $student = $this->Students->patchEntity(
     $student, $this->request->data, ['associated' => ['Exams']] 
    ); 
    if ($this->Students->save($student)) { 
     $this->Flash->success(__('The user has been saved.')); 
    } else { 
     $this->Flash->error(__('Unable to add the user.')); 
    } 
    } 
} 
+0

इकाई को पैच करने के बाद ** $ छात्र-> getErrors() ** का आउटपुट क्या है। – styks

उत्तर

0

Patching BelongsToMany Associations

आपको यह सुनिश्चित करना होगा कि आप परीक्षाएं सेट कर सकें। सेट accessibleFields आप संबंधित डेटा

$student = $this->Students->patchEntity(
    $student, $this->request->data, [ 
     'associated' => ['Exams'], 
     'accessibleFields' => ['exams' => true] 
    ] 
); 

तुम भी इकाई में $ _accessible संपत्ति के साथ ऐसा कर सकते हैं पैच करने के लिए अनुमति देने के लिए।