Add the new field to the existing table
- Currently, we have 3 tables which are ’employ’, ‘education’ and ‘job’.
- Now we want to add an option to select the job when we add an ’employ’, and for that, we need to add a new field for job in the ’employ’ table.
- To add the job field in the employ table, it is required to update the files like SQL, TCA, and model.
Update the SQL file
Add the below line of code into the table tx_employee_domain_model_employ of the SQL file. File path is employeeext_tables.sql
job int(11) DEFAULT 0 NOT NULL
Below is the screenshot of the tx_employee_domain_model_employ table after adding the job field.
Update the TCA file
1. A column of the job is required to be added to the TCA file of the employ table. The file path is employeeConfigurationTCAtx_employee_domain_model_employ.php
2. Add the code below as a column in order to add the job column to the TCA file of the employ table.
"job" => [
'label' => 'Job',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'foreign_table' => 'tx_employee_domain_model_job',
],
],
Below is the screenshot
3. Then add the job into the ‘types’ as you can see in the below screenshot
Save the file, toggle the extension (Deactivate once and then activate again), or go to Maintainance > Flush TYPO3 and PHP cache and click on the button “Flush cache” and then go to the List > select folder where you have stored all the employees > go to the Employ, select any record and you will see the option to select the job as you can see in the screenshot below
Update the model file
1. Now add the job to the model file of employ. The file path is employeeClassesDomainModelEmploy.php.
2. You can replace the model code with the below code to add the job.
<?php
declare(strict_types=1);
namespace CompanyEmployeeDomainModel;
use DateTime;
use TYPO3CMSExtbaseDomainObjectAbstractEntity;
use TYPO3CMSExtbaseAnnotationORMLazy;
use TYPO3CMSExtbaseDomainModelFileReference;
use TYPO3CMSExtbasePersistenceGenericLazyLoadingProxy;
use TYPO3CMSExtbasePersistenceObjectStorage;
use CompanyEmployeeDomainModelEducation;
/**
* Employ Model
*/
class Employ extends AbstractEntity
{
/**
* firstName
*
* @var string
*/
protected $firstName = '';
/**
* lastName
*
* @var string
*/
protected $lastName = '';
/**
* gender
*
* @var string
*/
protected $gender = '';
/**
* birthDate
*
* @var DateTime
*/
protected $birthDate;
/**
* joiningDate
*
* @var DateTime
*/
public $joiningDate;
/**
* @Lazy
* @var TYPO3CMSExtbasePersistenceObjectStorage<TYPO3CMSExtbaseDomainModelFileReference>
*/
protected ObjectStorage $image;
/**
* bio
*
* @var string
*/
protected $bio = '';
/**
* experiance
*
* @var string
*/
protected $experiance = '';
/**
* salary
*
* @var string
*/
protected $salary = '';
/**
* integer
*
* @var integer
*/
protected $languages = '';
/**
* country
*
* @var string
*/
protected $country = '';
/**
* job
*
* @var integer
*/
protected $job = '';
/**
* @Lazy
* @var ObjectStorage<Education>
*/
protected $educations;
public function __construct()
{
$this->initializeObject();
}
public function initializeObject(): void
{
$this->educations = $this->educations ?? new ObjectStorage();
$this->image = $this->image ?? new ObjectStorage();
}
/**
* Returns the firstName
*
* @return string
*/
public function getFirstName(): string
{
return $this->firstName;
}
/**
* Sets the firstName
*
* @param string $firstName
* @return void
*/
public function setFirstName(string $firstName): void
{
$this->firstName = $firstName;
}
/**
* Returns the lastName
*
* @return string
*/
public function getLastName(): string
{
return $this->lastName;
}
/**
* Sets the lastName
*
* @param string $lastName
* @return void
*/
public function setLastName(string $lastName): void
{
$this->lastName = $lastName;
}
/**
* Returns the gender
*
* @return string
*/
public function getGender(): string
{
return $this->gender;
}
/**
* Sets the gender
*
* @param string $gender
* @return void
*/
public function setGender(string $gender): void
{
$this->gender = $gender;
}
/**
* Get birthDate
*
* @return DateTime|null
*/
public function getBirthDate(): ?DateTime
{
return $this->birthDate;
}
/**
* Set birthDate
*
* @param DateTime $birthDate
*/
public function setBirthDate(DateTime $birthDate): void
{
$this->birthDate = $birthDate;
}
/**
* Get year of birthDate
*
* @return int
*/
public function getYearOfBirthDate(): int
{
if ($this->getBirthDate()) {
return (int) $this->getBirthDate()->format('Y');
}
return 0;
}
/**
* Get month of birthDate
*
* @return int
*/
public function getMonthOfBirthDate(): int
{
if ($this->getBirthDate()) {
return (int) $this->getBirthDate()->format('m');
}
return 0;
}
/**
* Get day of birthDate
*
* @return int
*/
public function getDayOfBirthDate(): int
{
if ($this->birthDate) {
return (int) $this->birthDate->format('d');
}
return 0;
}
/**
* Set joiningDate
*
* @param DateTime $joiningDate
*/
public function setJoiningDate(DateTime $joiningDate): void
{
$this->joiningDate = $joiningDate;
}
/**
* Get year of joiningDate
*
* @return int
*/
public function getYearOfJoiningDate(): int
{
if ($this->getJoiningDate()) {
return (int) $this->getJoiningDate()->format('Y');
}
return 0;
}
/**
* Get month of joiningDate
*
* @return int
*/
public function getMonthOfJoiningDate(): int
{
if ($this->getJoiningDate()) {
return (int) $this->getJoiningDate()->format('m');
}
return 0;
}
/**
* Get day of joiningDate
*
* @return int
*/
public function getDayOfJoiningDate(): int
{
if ($this->joiningDate) {
return (int) $this->joiningDate->format('d');
}
return 0;
}
/**
* @return TYPO3CMSExtbasePersistenceObjectStorage<TYPO3CMSExtbaseDomainModelFileReference>
*/
public function getImage(): ObjectStorage
{
return $this->image;
}
/**
* @param TYPO3CMSExtbasePersistenceObjectStorage<TYPO3CMSExtbaseDomainModelFileReference> $image
*/
public function setImage(ObjectStorage $image): self
{
$this->image = $image;
return $this;
}
/**
* Returns the bio
*
* @return string
*/
public function getBio(): string
{
return $this->bio;
}
/**
* Sets the bio
*
* @param string $bio
* @return void
*/
public function setBio(string $bio): void
{
$this->bio = $bio;
}
/**
* Returns the experiance
*
* @return string
*/
public function getExperiance(): string
{
return $this->experiance;
}
/**
* Sets the experiance
*
* @param string $experiance
* @return void
*/
public function setExperiance(string $experiance): void
{
$this->experiance = $experiance;
}
/**
* Returns the salary
*
* @return string
*/
public function getSalary(): string
{
return $this->salary;
}
/**
* Sets the salary
*
* @param string $salary
* @return void
*/
public function setSalary(string $salary): void
{
$this->salary = $salary;
}
/**
* Returns the languages
*
* @return integer
*/
public function getLanguages(): int
{
return $this->languages;
}
/**
* Sets the languages
*
* @param integer $languages
* @return void
*/
public function setLanguages(int $languages): void
{
$this->languages = $languages;
}
/**
* Returns the country
*
* @return string
*/
public function getCountry(): string
{
return $this->country;
}
/**
* Sets the country
*
* @param string $country
* @return void
*/
public function setCountry(string $country): void
{
$this->country = $country;
}
public function addEducations(Education $educations): void
{
$this->educations = $this->getEducations();
$this->educations->attach($educations);
}
/**
* @return ObjectStorage<Education>
*/
public function getEducations(): ObjectStorage
{
if ($this->educations instanceof LazyLoadingProxy) {
$this->educations->_loadRealInstance();
}
if ($this->educations instanceof ObjectStorage) {
return $this->educations;
}
return $this->educations = new ObjectStorage();
}
public function removeEducations(Education $educations): void
{
$this->educations = $this->getEducations();
$this->educations->detach($educations);
}
/**
* @param ObjectStorage<Education> $educations
*/
public function setEducations(ObjectStorage $educations): void
{
$this->educations = $educations;
}
/**
* Returns the job
*
* @return integer
*/
public function getjob(): int
{
return $this->job;
}
/**
* Sets the job
*
* @param integer $job
* @return void
*/
public function setjob(int $job): void
{
$this->job = $job;
}
}