简体   繁体   English

如果在 if 语句中,则不回显组件

[英]Not echo a component if inside an if statement

So I have the below method inside my class and one thing that I was wondering if how can I make it not echo out an component if its detected that it's null.所以我在我的 class 中有以下方法,我想知道如果它检测到它是 null,我是否可以让它不回显一个组件。

public function sync_profiles(object $record): Profile
{
    $profile = $this->get_profile($record->id);
    var_dump($profile);

    $default = [
        'post_status' => 'publish',
        'post_author' => 0,
        'octopus_id' => $record->id,
    ];

    $update = [
        'post_title' => $record->first_name . ' ' . $record->last_name,
        'peoplesoft_id' => $record->employee_id,
        'first_name' => $record->first_name,
        'last_name' => $record->last_name,
        'email_address' => $record->email,
    ];

    if ($profile === null) {
        $profile = new Profile(array_merge($default, $update));
        echo '<pre>Profile ' . $record->id . ' has been created</pre>';
    }
    $profile->load($update);
    $profile->save();
    echo '<pre>Profile ' . $record->id . ' has been updated</pre>';

    return $profile;
}

So the above code code outputs the following messages:所以上面的代码代码输出以下消息:

us.php:247:null (This is the var_dump): us.php:247:null(这是 var_dump):
Profile ID: 7899 has been created配置文件 ID:7899 已创建
Profile ID: 7899 has been updated个人资料 ID:7899 已更新

If it finds that $profile is null, it'll merge the data arrays and echo the message.. If $profile is null, echo only the created message and if it'a not null, echo only the updated message. If it finds that $profile is null, it'll merge the data arrays and echo the message.. If $profile is null, echo only the created message and if it'a not null, echo only the updated message.

Thank you for your comment.感谢您的评论。

I would use an if-statement and return the profile after the update.我会使用 if 语句并在更新后返回配置文件。 In this case you stop the function from further execution.在这种情况下,您将停止 function 的进一步执行。 In this case it will not execute the creation anymore.在这种情况下,它将不再执行创建。 You can also use your existing if-statement and add an else to it.您还可以使用现有的 if 语句并向其添加 else。 It would have the same effect.它会产生同样的效果。

public function sync_profiles(object $record): Profile
{
    $update = [
        'post_title' => $record->first_name . ' ' . $record->last_name,
        'peoplesoft_id' => $record->employee_id,
        'first_name' => $record->first_name,
        'last_name' => $record->last_name,
        'email_address' => $record->email,
    ];

    $profile = $this->get_profile($record->id);
    if (!is_null($profile)) {
      $profile->load($update);
      $profile->save();

      echo '<pre>Profile ' . $record->id . ' has been updated</pre>';

      return $profile;
    }

    $profile = new Profile(array_merge([
        'post_status' => 'publish',
        'post_author' => 0,
        'octopus_id' => $record->id,
    ], $update));
    $profile->load($update);
    $profile->save();

    echo '<pre>Profile ' . $record->id . ' has been updated</pre>';

    return $profile;
}

In case you want my personal opinion then I would go with 2 new methods.如果您需要我的个人意见,那么我将使用 2 种新方法 go。 One for a new profile sync and another one for an updated profile sync to keep the logic separated.一个用于新的配置文件同步,另一个用于更新的配置文件同步,以保持逻辑分离。 It could look like this:它可能看起来像这样:

public function sync_profiles(object $record): Profile 
{
    $profile = $this->get_profile($record->id);

    return is_null($profile) 
        ? $this->sync_profiles_new($record)
        : $this->sync_profiles_existing($record, $profile);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM