要将相关数据中的新记录保存为多对1和多对多,如下所示?我无法保存我的数据。 我试图在没有身份证的情况下保存给监护人但是这是一个新记录并且我无法正常工作时出错
控制器
if ($this->request->is(['patch', 'post', 'put'])) {
$mydata=$this->request->data;
$guardian =$this->Guardians->newEntity();
$guardian->guardian_first_name = $mydata['Guardians']['guardian_first_name'];
$guardian->guardian_last_name = $mydata['Guardians']['guardian_last_name'];
$guardian->guardian_mobile = $mydata['Guardians']['guardian_mobile'];
$guardian->guardian_email = $mydata['Guardians']['guardian_email'];
$student = $this->Students->newEntity( $mydata,['associated' => ['AvailabilityForStudents','Subjects','Guardians']] );
$student->guardians = [ $guardian];
$val= $this->Students->save($student,[
'validate' => false ]);
public function initialize(array $config)
{
parent::initialize($config);
$this->table('students');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'LEFT'
]);
$this->belongsTo('Guardians', [
'foreignKey' => 'guardian_id',
'joinType' => 'LEFT'
]);
$this->hasMany('AvailabilityForStudents', [
'foreignKey' => 'student_id'
]);
$this->belongsToMany('Subjects', [
'foreignKey' => 'student_id',
'targetForeignKey' => 'subject_id',
'joinTable' => 'students_subjects'
]);
}
http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-with-associations
update-returned data
[
'first_name' => 'a1',
'last_name' => 'a2',
'student_mobile' => '',
'school' => '9',
'class_year' => '9',
'tutor_gender_preference' => 'No Preference',
'doing_assessment' => '0',
'tutoring_typest_id' => '1',
'address_street' => '1 gghh',
'address_suburb' => 'seagg',
'address_postcode' => '3111',
'address_state' => 'Vic',
'address_lat' => '',
'address_long' => '',
'school_address_street' => '',
'school_address_suburb' => '',
'school_address_postcode' => '',
'school_address_lat' => '',
'school_address_long' => '',
'Guardians' => [
'guardian_first_name' => 'aa',
'guardian_last_name' => 'aa',
'guardian_phone' => '03534535',
'guardian_mobile' => '0000000000',
'guardian_email' => 'a@ww.com'
],
'subjects' => [
'_ids' => [
(int) 0 => '1'
]
],
'AvailabilityForStudents' => [
'Monday' => [
'weekday' => 'Monday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Tuesday' => [
'weekday' => 'Tuesday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Wednesday' => [
'weekday' => 'Wednesday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Thursday' => [
'weekday' => 'Thursday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Friday' => [
'weekday' => 'Friday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Saturday' => [
'weekday' => 'Saturday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Sunday' => [
'weekday' => 'Sunday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
]
]
]
patched data
object(App\Model\Entity\Student) {
'first_name' => 'a1',
'last_name' => 'a2',
'student_mobile' => '',
'school' => '9',
'class_year' => (int) 9,
'tutor_gender_preference' => 'No Preference',
'doing_assessment' => (int) 0,
'tutoring_typest_id' => (int) 1,
'address_street' => '1 gghh',
'address_suburb' => 'seagg',
'address_postcode' => '3111',
'address_state' => 'Vic',
'address_lat' => '',
'address_long' => '',
'school_address_street' => '',
'school_address_suburb' => '',
'school_address_postcode' => '',
'school_address_lat' => '',
'school_address_long' => '',
'Guardians' => [
'guardian_first_name' => 'aa',
'guardian_last_name' => 'aa',
'guardian_phone' => '03534535',
'guardian_mobile' => '0000000000',
'guardian_email' => 'a@ww.com'
],
'subjects' => [
(int) 0 => object(App\Model\Entity\Subject) {
'id' => (int) 1,
'name' => 'English: Prep- Grade 3',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Subjects'
}
],
'AvailabilityForStudents' => [
'Monday' => [
'weekday' => 'Monday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Tuesday' => [
'weekday' => 'Tuesday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Wednesday' => [
'weekday' => 'Wednesday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Thursday' => [
'weekday' => 'Thursday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Friday' => [
'weekday' => 'Friday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Saturday' => [
'weekday' => 'Saturday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
],
'Sunday' => [
'weekday' => 'Sunday',
'not_available' => '0',
'start_time' => '9:00',
'end_time' => '10:00'
]
],
'student_enq' => false,
'user_id' => (int) 7,
'student_inactive' => false,
'acknowledge' => (int) 0,
'is_email_notify' => (int) 0,
'days_joined' => (int) 0,
'satisfaction_rating' => (int) 0,
'student_unallocated' => false,
'admin_satisfaction_rating' => (int) 0,
'another_assessment' => (int) 0,
'numeracy_active' => (int) 0,
'is_waiting_assessment' => (int) 0,
'group_status' => (int) 0,
'address_billing' => '4',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'first_name' => true,
'last_name' => true,
'student_mobile' => true,
'school' => true,
'class_year' => true,
'tutor_gender_preference' => true,
'doing_assessment' => true,
'tutoring_typest_id' => true,
'address_street' => true,
'address_suburb' => true,
'address_postcode' => true,
'address_state' => true,
'address_lat' => true,
'address_long' => true,
'school_address_street' => true,
'school_address_suburb' => true,
'school_address_postcode' => true,
'school_address_lat' => true,
'school_address_long' => true,
'Guardians' => true,
'subjects' => true,
'AvailabilityForStudents' => true,
'student_enq' => true,
'user_id' => true,
'student_inactive' => true,
'acknowledge' => true,
'is_email_notify' => true,
'days_joined' => true,
'satisfaction_rating' => true,
'student_unallocated' => true,
'admin_satisfaction_rating' => true,
'another_assessment' => true,
'numeracy_active' => true,
'is_waiting_assessment' => true,
'group_status' => true,
'address_billing' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Students'
}
答案 0 :(得分:1)
我也遇到了类似的问题,毕竟R& D,我尝试了这个代码并且它有效。希望你也会受益。代码在这里:
假设您有一个与“RolesController”相关联的控制器“UsersController”和“id”。然后在UsersController下的操作中添加以下代码行:
$this->loadModel('Roles');
$roles = $this->Roles->find('list');
$roles = $roles->toArray();
$this->set(compact('roles'));
现在在模板文件夹中的操作视图文件中进行以下更改:
echo $this->Form->select('role_id', $roles);
在这里,我们已将该变量分配给表单字段。
通过这些更改,您应该能够完成任务。如果仍有任何问题,请随时在评论中提问。
保持编码!玩得开心! :)
答案 1 :(得分:-1)
我建议拆分实体创建。
首先,使用$student = $this->Students->newEntity();
然后将数据修补到实体,如:
$student = $this->Student->patchEntity($mydata,['associated' => ['AvailabilityForStudents','Subjects','Guardians']]);
也许patchEntity()方法以更好的方式理解关联。 希望这能解决你的问题!