【问题标题】:phalcon 2.0.13 set data with magic setter to related modelphalcon 2.0.13 使用魔术设置器将数据设置为相关模型
【发布时间】:2016-12-15 08:32:03
【问题描述】:

我对 phalcon 模型魔法 getter 和 setter 有疑问。 我想像本教程一样更新: https://docs.phalconphp.com/en/latest/reference/models.html#storing-related-records

但问题是我的项目是多模块和分离的模型文件夹。

所以我必须为 hasOne 和 belongsTo 使用别名

$this->hasOne('user_id', '\Models\UserProfile', 'user_id', array('alias' => 'UserProfile'));

 $this->belongsTo('user_id', '\Models\CoreUser', 'user_id', array('alias' => 'CoreUser'));

我想做的就是这样。

$CoreUser = new CoreUser();
$user = $CoreUser->findFirst(array(
        //...condition here to find the row i want to update
     ));

$user->assign($newUserData);

$user->setUserProfile($newProfileData); 

$user->update();

但上面这段代码只保存用户数据,根本不保存个人资料数据。 (有个人资料数据 -- 已确认)

那么你知道错误是什么吗?如果你知道,请帮助我或给我小费。

【问题讨论】:

  • $user 上是否存在方法setUserProfile()?否则只需使用$user->userProfile = $newProfileData
  • 您能否用$newProfileData 的内容更新您的答案?
  • $user->userProfile = $newProfileData 或 $user->UserProfile = $newProfileData 不起作用..为什么会这样?它应该像在 vokuro 示例中一样工作:(
  • $newProfileData = array('first_name' => 'John', 'last_name' => 'PHP');

标签: php model-view-controller model phalcon


【解决方案1】:

我现在明白了.. 当分配像 $user->UserProfile = $newUserProfile; $newUserProfile 应该是一个模型对象。

所以我的新代码是

$CoreUser = new CoreUser();

$user = $CoreUser->findFirst(array(
    //...condition here to find the row i want to update
 ));
$profile = $user->UserProfile; //$profile is now model object which related to $user

//assign new array data 
$profile->assign($newProfileData);
$user->assign($newUserData);
/*
* can also assign one by one like
* $user->first_name = $newProfileData['first_name'];
* but cannot be like $profile = $newProfileData or $user->UserProfile = $newProfile
* since it's gonna override it the model with array
*/
$user->UserProfile = $profile;   

$user->update(); // it's working now

也感谢@Timothy 的提示.. :)

【讨论】:

  • 顺便说一句,不需要初始化$CoreUser。您可以在 CoreUser 类上静态运行查询。像这样:$user = CoreUser::findFirst(['your-conditions']); 这将为您节省一行代码:)
  • 顺便说一句,$user->UserProfile = $profile;不应用“/Models/Base::update()”方法? /Models/Base 用户配置文件扩展的类,我创建为所有模型的基本模型..
  • 如果我正确地解释了你的代码,那么$user->UserProfilecreate 一个新的配置文件而不是update-ing 一个。因此,它不会调用您的/Models/Base::update(),而是调用::create()。这是你想问的吗?
  • 是的。应该调用 create() 或 update().. 但 create() 方法也没有被调用.. 因为我在基本模型中组合了一些默认列和默认值。但在 db 中,默认列上只有 null .. :( .. 看起来这些魔法方法不适合我
  • 我添加了一个答案,希望可以解决问题!
【解决方案2】:

而不是做

$profile = $user->UserProfile;

你应该实例化一个新的UserProfile对象

// find your existing user and assign updated data
$user = CoreUser::findFirst(array('your-conditions'));
$user->assign($newUserData);

// instantiate a new profile and assign its data
$profile = new UserProfile();
$profile->assign($newProfileData);

// assign profile object to your user
$user->UserProfile = $profile;

// update and create your two objects
$user->save();

请注意,这将始终创建一个 UserProfile。如果您想使用相同的代码来更新和创建UserProfile,您可以执行以下操作:

// ...

// instantiate a (new) profile and assign its data
$profile = UserProfile::findFirstByUserId($user->getUserId());

if (!$profile) {
    $profile = new UserProfile();
}

$profile->assign($newProfileData);

// ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    相关资源
    最近更新 更多