upgradeSchema is not working in magento 2 - php

I want to add new column in my existing table. I created an upgradeSchema.php file. And changed version in module.xml file. But after executing upgrade command, nothing happened. Here is my code
<?php
namespace Bridge\Tradeuser\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {#inheritdoc}
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) {
$installer = $setup;
$installer->startSetup();
if (version_compare($context->getVersion(), '3.0.1', '<')) {
$installer->getConnection()
->addColumn(
$installer->getTable('batchcode_entity'),
'status',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 50,
'nullable' => false,
'default' => 'Active',
'comment' => 'Batchcode status'
]
);
}
$installer->endSetup();
}
}
?>
Then I run the following commands:
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento setup:di:compile
bin/magento cache:flush

Hey Try using this method just change the version number and columns names to match it, I tried right now and it worked
<?php
namespace SimplifiedMagento\Database\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Db\Ddl\Table;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* Upgrades DB schema for a module
*
* #param SchemaSetupInterface $setup
* #param ModuleContextInterface $context
* #return void
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if(version_compare($context->getVersion(),'0.0.2','<'))
{
//if version of your project is lower than 0.0.2 then implement this
$setup->getConnection()->addColumn(
$setup->getTable('affiliate_member'),
'phonenumber',
['nullable' => false,'type' => Table::TYPE_TEXT,'comment' => 'Phone number column']
);
}
$setup->endSetup();
}
}
But make sure your module.xml file is having similar version which you wrote in if condition:
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SimplifiedMagento_Database" setup_version="0.0.2">
</module>
</config>

Related

Get arguments from Laravel Artisan command

Hello I am fairly new to Laravel and have ran into an issue with logging complete artisan commands. Here is the listener I registered for logging my commands:
protected $listen = [
CommandStarting::class => [CommandLogging::class],
];
Here is the code for the listener:
<?php
namespace App\Listeners;
use Illuminate\Console\Events\CommandStarting;
class CommandLogging
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param CommandStarting $event
* #return void
*/
public function handle(CommandStarting $event): void
{
$log->info([
'commandName' => $event->command,
'input' => $event->input->getArguments(),
'output' => $event->output,
]);
}
}
My issue is when I run a command such as php artisan make:event FakeEventTest the only thing I get is the command name. Is there anyway to get the arguments as well such as FakeEventTest in the example command.

TYPO3 The default controller for extension cannot be determined

I am creating an extension to list job offers, and when I try to see the view, I have this error message :
The default controller for extension "Offerjob" and plugin "JobList" can not be determined. Please check for
TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin() in your ext_localconf.php.
I checked the namespaces of all my files, I tried some solutions and I can't resolve my issue, someone have an idea ?
let check my files :
ext/offerjob/Classes/Controller/OfferjobController.php
namespace Vendor\Offerjob\Controller;
use Vendor\Offerjob\Domain\Repository\JobRepository;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
/**
* Class OfferjobController
*
* #package Vendor\Offerjob\Controller
*/
class OfferjobController extends ActionController
{
/**
* #var JobRepository
*/
private $jobRepository;
/**
* Inject the job repository
*
* #param \Vendor\Offerjob\Domain\Repository\JobRepository $jobRepository
*/
public function injectJobRepository(JobRepository $jobRepository)
{
$this->jobRepository = $jobRepository;
}
/**
* List Action
*
* #return void
*/
public function listAction()
{
$jobs = $this->jobRepository->findAll();
$this->view->assign('jobs', $jobs);
}
}
public/typo3conf/ext/offerjob/ext_localconf.php
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
'Vendor.Offerjob',
'JobList',
[
\Vendor\Offerjob\Controller\OfferjobController::class => 'list',
],
// non-cacheable actions
[
\Vendor\Offerjob\Controller\OfferjobController::class => '',
]
);
public/typo3conf/ext/offerjob/Configuration/TCA/Overrides/tt_content.php
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin(
'Vendor.Offerjob',
'JobList',
'Job list',
'EXT:offerjob/Resources/Public/Icons/Extension.svg'
);
Is all the naming of plugins, namespace right? Is there something I'm doing wrong?
I'm Using TYPO3 10.4.21 and I followed this tutorial to make my extension : https://docs.typo3.org/m/typo3/book-extbasefluid/10.4/en-us/4-FirstExtension/2-create-folder-structure-and-configuration-files.html
Remove the vendor part of the extension name as this is deprecated in v10 and will be removed in v11:
Vendor.Offerjob => Offerjob
Documentation: https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/10.1/Deprecation-88995-CallingRegisterPluginWithVendorName.html

Doctrine fails to update schema. "Nothing to update" (Symfony 3.4.8)

I've recently created an Entity with php bin/console doctorine:generate:entity. After creation, I've tried to update my schema with php bin/console doctrine:schema:update --force but got an output of:
Nothing to update - your database is already in sync with the current entity metadata.
Things I've already tried:
php bin/console doctrine:schema:update --dump-sql
Same output
php bin/console doctrine:cache:clear-metadata
Ran, but nothing changed
Clearing cache multiple times
Nothing
php bin/console doctrine:mapping:info
output:
[OK] FOS\UserBundle\Model\Group
[OK] FOS\UserBundle\Model\User
Entity in question:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="WeeklyPaymentReport")
*/
class WeeklyPaymentReport
{
/**
* #var int
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var int
*
* #ORM\Column(type="integer", nullable=true)
*/
private $bbid;
/**
* #var String
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $request;
/**
* #var \DateTime
*
* #ORM\Column(name="dateOfBirth", type="date", nullable=true)
*/
private $date;
public function getId()
{
return $this->id;
}
public function setBbid($bbid)
{
$this->bbid = $bbid;
return $this;
}
public function getBbid()
{
return $this->bbid;
}
public function setRequest($request)
{
$this->request = $request;
return $this;
}
public function getRequest()
{
return $this->request;
}
public function setDate($date)
{
$this->date = $date;
return $this;
}
public function getDate()
{
return $this->date;
}
}
Doctrine also generated:
namespace AppBundle\Repository;
/**
* WeeklyPaymentReportRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class WeeklyPaymentReportRepository extends \Doctrine\ORM\EntityRepository
{
}
And also .orm.php for it:
use Doctrine\ORM\Mapping\ClassMetadataInfo;
$metadata->setInheritanceType(ClassMetadataInfo::INHERITANCE_TYPE_NONE);
$metadata->customRepositoryClassName = 'AppBundle\Repository\WeeklyPaymentReportRepository';
$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT);
$metadata->mapField(array(
'fieldName' => 'id',
'type' => 'integer',
'id' => true,
'columnName' => 'id',
));
$metadata->mapField(array(
'columnName' => 'bbid',
'fieldName' => 'bbid',
'type' => 'integer',
));
$metadata->mapField(array(
'columnName' => 'request',
'fieldName' => 'request',
'type' => 'string',
'length' => 255,
));
$metadata->mapField(array(
'columnName' => 'date',
'fieldName' => 'date',
'type' => 'datetime',
));
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
What else can I try?
Thank you for your help in advance!
Okay, so right after posting, I've found a post i've never been recommended before which solved the problem.
It's the last sentence, that solved my case.
"After deleting John.orm.php file if i run php bin/console doctrine:schema:update --force then it will generate tables."
Kinda weird solution, I hope I dont encounter weird anomalies in future developments because of it.

How to use monolog ElasticSearchHandler for logging in a Laravel application

Monolog contains elastic search handler and formatter, but it's implementation to laravel as a custom channel is not as straightforward as described on Laravel documentation web site.
Here's a brief step by step instruction how to do that.
Create a config file for your elastic search logging.
config/elastic_log.php
with the next content:
<?php
return [
'host' => env('ELASTIC_HOST'),
'index' => 'index_name',
'prefix' => 'index_prefix',
'type' => '_doc',
];
You can change your index name and prefix to any string values.
In your .env file put your elastic host address:
ELASTIC_HOST=your_elastic_host:port
Install elasticsearch/elasticsearch official package
composer require elasticsearch/elasticsearch
Create ElasticLogging service provider
php artisan make:provider ElasticLogProvider
With the following content:
<?php
namespace App\Providers;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\ElasticsearchFormatter;
use Monolog\Handler\ElasticsearchHandler;
class ElasticLogProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
$index = rtrim(config('elastic_log.prefix'), '_') . '_' . config('elastic_log.index');
$type = config('elastic_log.type');
$this->app->bind(Client::class, function ($app) {
return ClientBuilder::create()->setHosts([config('elastic_log.host')])->build();
});
$this->app->bind(ElasticsearchFormatter::class, function ($app) use ($index, $type) {
return new ElasticsearchFormatter($index, $type);
});
$this->app->bind(ElasticsearchHandler::class, function ($app) use ($index, $type) {
return new ElasticsearchHandler($app->make(Client::class), [
'index' => $index,
'type' => $type,
'ignore_error' => false,
]);
});
}
}
Add this provider to your app.php config file to the providers array:
App\Providers\ElasticLogProvider::class,
Create a command for elastic logging settings on the server. This command creates an index on the server if it doesn't exist yet.
Now for the preparation of your server, just run elastic:log_setup;
php artisan make:command ElasticLogSetup
With the following content:
<?php
namespace App\Console\Commands;
use Elasticsearch\Client;
use Illuminate\Console\Command;
class ElasticLogSetup extends Command
{
/**
* #var Client
*/
protected $client;
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'elastic:log_setup';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Setup elastic log index';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct(Client $client)
{
$this->client = $client;
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$index = rtrim(config('elastic_log.prefix'), '_') . '_' . config('elastic_log.index');
if (!$this->client->indices()->exists(['index' => $index])) {
$this->client->indices()->create([
'index' => $index,
]);
}
}
}
In the file config/logging.php add this element to 'channels' array and import the related classes:
use Monolog\Formatter\ElasticsearchFormatter;
use Monolog\Handler\ElasticsearchHandler;
'elastic' => [
'driver' => 'monolog',
'handler' => ElasticsearchHandler::class,
'level' => 'debug',
'formatter' => ElasticsearchFormatter::class,
];
Now you can use the channel 'elastic' or change it in your .env settings as a default channel:
LOG_CHANNEL=elastic
From now on, you can use standard laravel Log facade to send the information to your ElasticSearch server

Adding new form and entities to Symfony does not work

I've forked a Symfony 3 project and I'm trying to add a new Entity and the matching form type, but I can't make it work.
I've tried with the generator and manually, both won't work. A solution with the generator would be the best option
I've generated my entity School with php bin/console doctrine:generate:entity, but when I'm trying to generate the Form, I get the error Class GreenBundle\Entity\School does not exist.
I then tried to create the Form manually and got : Expected to find class "GreenBundle\Form\SchoolType" in file "/var/www/symfony/src/GreenBundle/Form/SchoolType.php" while importing services from resource "../../src/GreenBundle/*", but it was not found! Check the namespace prefix used with the resource.
My school entity generated by the command line is simple
<?php
namespace GreenBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* School
*
* #ORM\Table(name="school")
* #ORM\Entity(repositoryClass="GreenBundle\Repository\SchoolRepository")
*/
class School
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
(... Some other variables, the getters and setters )
}
The SchoolType I added is :
<?php
namespace GreenBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SchoolType extends AbstractType
{
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'GreenBundle\Entity\School',
));
}
/**
* #return string
*/
public function getName()
{
return 'green_mainbundle_school';
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom', TextType::class, array(
'label' => "Nom de l'école",
'required' => false,
'attr' => array(
'class' => 'form-control',
'placeholder' => 'ex: École Saint-Exupéry')
));
}
}
And the block In services.yml the error code is referring to is :
# this creates a service per class whose id is the fully-qualified class name
GreenBundle\:
resource: '../../src/GreenBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/GreenBundle/{Entity,Repository,Tests}'
Do you know what I'm doing wrong to get the server to run with the new SchoolType and be able to use it in a controller ?
Thanks
======== Edit
I do have Symfony 3
php bin/console --version
Symfony 3.4.14 (kernel: app, env: dev, debug: true)
The project architecture is classic I guess, I have the default folders of Symfony
In the src folder I have :
AppBundle (unused)
GreenBundle
Controller
DataFixtures
DBAL
DependencyInjection
Entity
Form
Repository (Empty, I don't think they created entities with the generator)
Service
Tests
Twig
GreenBundle.php
======== Edit 2
The namespace I use in GreenBundle.php is GreenBundle, you can see the file below
namespace GreenBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class GreenBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
In case anybody ever have the same kind of issue, I was running my project in a Docker container and it was a synch problem. The Symfony file themselves are correct. Docker detected that new file were added but didn't set the content properly. Rebuild and/or changing the way I use docker with this app fixes the issue

Categories