Kể từ khi một câu trả lời có thể chỉ có một người trả lời, mô hình Câu trả lời đã belongsTo mối quan hệ với mô hình sử dụng. Mô hình Câu trả lời đã có mối quan hệ belongsTo được xác định với mô hình Câu hỏi. Do đó, chúng ta sửa đổi các $ belongsTo biến thêm các mối quan hệ với mô hình sử dụng: | Simpo PDF Merge and Split Unregistered Version - http Quickwall User Authentication 6. Since an answer can have only one answerer the Answer model has belongsTo relationship with the User model. The Answer model already has belongsTo relationship defined with the Question model. Hence we modify the belongsTo variable to add the relationship with the User model php class Answer extends AppModel . var belongsTo array Question array className Question User array className User 7. Lastly since we do not have the answerer field in the Answer model any more let s remove the validation rule for it. The new validation array should look like this php class Answer extends AppModel . var validate array answer array rule array minLenght 1 required true allowEmpty false message Answer cannot be empty 248 Simpo PDF Merge and Split Unregistered Version - http Chapter 10 What Just Happened We started this section by removing any test data that we had in the database. This is because we are going to change the structure of the database and to make sure the old data does not bring any inconsistency. Thanks to God these are just test data. If this was a working app with real data this process for porting data from the old structure to the new would have taken a considerable amount of work. In step 2 we dropped the fields questioner and answerer from the questions and answers table respectively. Instead we add a new field to both the tables named user_id. Since we have a users table that stores user-specific data it will be redundant to store the name of users in the questions and answers table. Instead we will use user_id as a foreign key to the users table. In the remaining steps we defined model relationships between the User model Question model and the Answer model. Since a user can have many questions and a question can only belong to one user questioner the User model has hasMany relation to Question model. And the Question model has belongsTo