假设有一个User模型和一个Profile模型,它们通过user_id字段进行关联,同时使用了CakeDC Users插件。现在我们想要保存一个新的Profile记录,并将其与一个现有的User关联起来。以下是实现此目标的步骤。
$userId = $this->Auth->user('id');
$profile = new Profile(); $profile->user_id = $userId;
$profile->first_name = 'John'; $profile->last_name = 'Doe'; $profile->email = 'john@example.com'; $this->Profiles->save($profile);
现在,我们已经成功地创建了一个新的Profile记录,它已经与当前已登录用户的User记录进行了关联。
注意:在使用CakeDC Users插件时,您需要使用Auth组件来获取已登录用户的ID,并使用它来填写相关字段。此外,您还需要确保在Profile模型中定义user_id字段的关联。
上一篇:保存棒球投球局数的小数
下一篇:保存包含逗号的字符串的CSV文件