Anatoliy
Well-known member
So, following the Building with XenForo 2 videos I created a Notes add-on. Now I would like to modify it to my needs. A simple modification - to replace a 'Title' field with a 'Fishing date' field. As I understand I need to modify my custom entity and also a setup file. So regarding a custom entity - I just replace
with
?
Here is the whole code just in case
And regarding a setup file. What should I do? Should I add installStep2 that alters a table deleting the 'title' column and add installStep3 that alters a table adding the 'fishing_date' column?
Please advise.
Code:
'title' => ['type' => self::STR, 'maxLength' => 255, 'required' => true, 'censor' => true],
Code:
'fishing_date' => ['type' => self::UINT, 'default' => \XF::$time],
Here is the whole code just in case
PHP:
<?php
namespace AV\Notes\Entity;
use XF\Mvc\Entity\Entity;
use XF\MVC\Entity\Structure;
class Note extends Entity
{
protected function _preSave()
{
if ($this->isUpdate()) {
$this->edit_date = \XF::$time;
}
}
protected function verifyTitle(&$value)
{
if (strlen($value) < 3) {
$this->error('Please anter a proper title', 'title');
return false;
}
$value = utf8_ucwords($value);
return true;
}
public function getFirstWord()
{
return explode($this->content, ' ')[0];
}
public static function getStructure(Structure $structure): Structure
{
$structure->table = 'av_notes_note';
$structure->shortName = 'AV\Notes:Note';
$structure->contentType = 'av_notes_note';
$structure->primaryKey = 'note_id';
$structure->columns = [
'note_id' => ['type' => self::UINT, 'autoIncrement' => true],
'user_id' => ['type' => self::UINT, 'default' => \XF::visitor()->user_id],
'title' => ['type' => self::STR, 'maxLength' => 255, 'required' => true, 'censor' => true],
'content' => ['type' => self::STR, 'required' => true],
'post_date' => ['type' => self::UINT, 'default' => \XF::$time],
'edit_date' => ['type' => self::UINT, 'default' => 0],
];
$structure->relations = [
'User' => [
'entity' => 'XF:User',
'type' => self::TO_ONE,
'conditions' => 'user_id',
'primary' => true,
],
];
$structure->defaultWith = ['User'];
$structure->getters = [
'firstfWord' => true
];
$structure->behaviors = [];
return $structure;
}
}
And regarding a setup file. What should I do? Should I add installStep2 that alters a table deleting the 'title' column and add installStep3 that alters a table adding the 'fishing_date' column?
PHP:
<?php
namespace AV\Notes;
use XF\AddOn\AbstractSetup;
use XF\AddOn\StepRunnerInstallTrait;
use XF\AddOn\StepRunnerUninstallTrait;
use XF\AddOn\StepRunnerUpgradeTrait;
class Setup extends AbstractSetup
{
use StepRunnerInstallTrait;
use StepRunnerUpgradeTrait;
use StepRunnerUninstallTrait;
public function installStep1()
{
$this->schemaManager()->createTable('av_notes_note', function (\XF\Db\Schema\Create $table) {
$table->addColumn('note_id', 'int')->autoIncrement();
$table->addColumn('user_id', 'int')->setDefault(0);
$table->addColumn('title', 'varchar', 255);
$table->addColumn('content', 'text');
$table->addColumn('post_date', 'int')->setDefault(0);
$table->addColumn('edit_date', 'int')->setDefault(0);
});
}
public function uninstallStep1()
{
$this->schemaManager()->dropTable('av_notes_note');
}
}
Please advise.