Home > Software design >  Undefined array key "project" - work with api, cteating console command
Undefined array key "project" - work with api, cteating console command

Time:08-25

I have code where I use function for get data from api getTimeEntriesInfo

    public function getTimeEntriesInfo(int $user_id): array
    {
        $users = self::getAllData('time_entry', ['user_id'=>$user_id]);
        return $users['time_entries'];
    }

Then I create console command artisan to save all data from api:

<?php

class LinkNew extends Command
{

    public function handle()
    {
        $redmineService = new RedmineAPIService();
        $users = $redmineService->getUsersInfo();

        foreach ($users as $user) {
            $data = $redmineService->getTimeEntriesInfo($user['id']);
            //dd($data);

            $project = $redmineService->getTimeEntriesInfo($user['project']);
            $activity = $redmineService->getTimeEntriesInfo($user['activity']);
            $issue = $redmineService->getTimeEntriesInfo($user['issue']);
            $comments = $redmineService->getTimeEntriesInfo($user['comments']);

            if ($this->confirm('Is this information correct?')) {
                $link = new TimeEntry();

                $link->project = $project;
                $link->activity = $activity;
                $link->issue = $issue;
                $link->comments = $comments;
                //dd($link->getAttributes());
                $link->save();

                $this->info("Saved.");
            }
        }
        return 0;
    }
}

But I have mistake Undefined array key "project". Why?

dd($data)

  248 => array:10 [
    "id" => 5379
    "project" => array:2 [ …2]
    "issue" => array:1 [ …1]
    "user" => array:2 [ …2]
    "activity" => array:2 [ …2]
    "hours" => 1.5
    "comments" => "Some comment"
    "spent_on" => "2022-05-27"
    "created_on" => "2022-05-27T09:31:18Z"
    "updated_on" => "2022-05-27T09:31:18Z"
  ]
  249 => array:10 [
    "id" => 5369
    "project" => array:2 [ …2]
    "issue" => array:1 [ …1]
    "user" => array:2 [ …2]
    "activity" => array:2 [ …2]
    "hours" => 1.0
    "comments" => "Some comment"
    "spent_on" => "2022-05-27"
    "created_on" => "2022-05-27T05:00:19Z"
    "updated_on" => "2022-05-27T05:00:19Z"
  ]

upd

    public function handle()
    {
        $redmineService = new RedmineAPIService();
        $users = $redmineService->getUsersInfo();
        foreach ($users as $user) {
            $data = $redmineService->getTimeEntriesInfo($user['id']);


            //isset($user);
            foreach ($users as $user) {
                //$data = $redmineService->getTimeEntriesInfo($user['id']);
                //dd($data);

                $project = $data['project'];
                $activity = $redmineService->getTimeEntriesInfo($data['activity']);
                $issue = $redmineService->getTimeEntriesInfo($data['issue']);
                $comments = $redmineService->getTimeEntriesInfo($user['comments']);

                if ($this->confirm('Is this information correct?')) {
                    $link = new TimeEntry();

                    $link->project = $project;
                    $link->activity = $activity;
                    $link->issue = $issue;
                    $link->comments = $comments;
                    //dd($link->getAttributes());
                    $link->save();

                    $this->info("Saved.");
                }
            }
        }

        return 0;
    }

CodePudding user response:

I resolved it like:

public function handle()
    {
        $redmineService = new RedmineAPIService();

        $timeEntries = $redmineService->getTimeEntriesInfo();
        foreach ($timeEntries as $timeEntry) {
            $link = new TimeEntry();

            $link->redmine_id = $timeEntry['id'];
            $link->project = $timeEntry['project']['name'];
            $link->issue = $timeEntry['issue']['id'];
            $link->user = $timeEntry['user']['name'];
            $link->activity = $timeEntry['activity']['name'];
            $link->hours = $timeEntry['hours'];
            $link->comments = $timeEntry['comments'];

            $link->save();
        }
        return 0;
    }
}

  • Related