I got in a fight with GitHub Copilot

March 2025

I got in a fight with GitHub Copilot

Like all developers I'm experimenting with using AI to improve my code and write it faster. Although I don't like to pay for things and services I decided to go with a year of GitHub Copilot. I am 100% positive I'm still not using it to it's full advantage but after a few month I was getting comfortable with asking it to help with code I couldn't remember or didn't know off the top of my head. The following is a condensed dialog with GitHub Copilot. Enjoy.


Act One

Scene 1

Amanda's home office, Toronto Canada. Day.

Amanda is working on a personal Craft CMS project that requires a custom module. In one of the functions of the custom module she checks to see if an entry already exists and if not the code needs to create a new entry. Part of the entry data includes section id. Since it's possible (though unlikely) the section could have different id numbers in different environments it's better to look up the section id based on the handle rather than hard code the number.

AMANDA

Hi good morning. I need to do more stuff with my controller from yesterday. You've been so helpful so far. I'm hoping you can continue assisting. How does my controller function access form data?

GITHUB COPILOT

Good morning! I'm glad to continue assisting you. To access form data in your controller function, you can use the request component provided by CraftCMS. Here's how you can modify your actionSendInvite method to access form data:

Supplies the current file Amanda is working with with many modifications including $entry->sectionId = Section::find()->handle('calendarInvites')->one()->id;

If you have any other questions or need further assistance, feel free to ask!

AMANDA

I'm trying to add the Section::find() but I get error "Class "craft\elements\Section" not found"

GITHUB COPILOT

Gives me the same 100+ lines of code but now with $entry->sectionId = Craft::$app->sections->getSectionByHandle('calendarInvites')->id;

This should resolve the "Class not found" error and correctly find the section by its handle. If you have any further questions or need additional assistance, feel free to ask!

AMANDA

And now it says "Class "craft\services\Section" not found"

GITHUB COPILOT

Basically says "Nope, that's the answer."

AMANDA

Do you actually know or are you just guessing?

GITHUB COPILOT

I apologize for any confusion. Let's correct the code to ensure it works properly.

Gives me the exact same code.

AMANDA

I am frustrated. I find the answer myself. I want this AI to learn so I share what I found.

To get the section id I used Craft::$app->entries->getSectionByHandle('calendarInvites')->id;

GITHUB COPILOT

To get the section ID by handle, you should use Craft::$app->sections->getSectionByHandle('calendarInvites') instead.

AMANDA

I am very frustrated!

No this project is Craft CMS 5. I found documentation https://docs.craftcms.com/api/v5/craft-services-entries.html#method-getsectionbyhandle

GITHUB COPILOT

Thank you for the clarification. In Craft CMS 5, you should indeed use Craft::$app->sections->getSectionByHandle('calendarInvites') to get the section by handle. Here's the corrected code:

Same code.

AMANDA

I am pulling out my hair!

No, Craft::$app->sections->getSectionByHandle('calendarInvites') did not work. I gave me an error.

GITHUB COPILOT

I apologize for the confusion. Let's use the correct method to get the section by handle in Craft CMS 5. The correct service to use is Craft::$app->sections. Here's the corrected code:

Same code.

AMANDA

I am crying. I am shaking my fists at the heavens. I am questioning every decision I have ever made in life that brought me to this exact moment in time.

No. Please listen to me. I've got it working. For Craft CMS 5 the code is Craft::$app->entries->getSectionByHandle('calendarInvites') and it returns craft\models\Section so I can then get the id

GITHUB COPILOT

Thank you for the clarification. I understand now. Let's proceed with the correct code using Craft::$app->entries->getSectionByHandle('calendarInvites') to get the section by handle. Here's the updated code:

Gives me my own working code.

This should correctly use Craft::$app->entries->getSectionByHandle('calendarInvites') to get the section by handle and then retrieve the ID. If you have any further questions or need additional assistance, feel free to ask!

Fin.