Codeigniter an migration not working with wamp - php

I am following a tutorial and I have check the source code with my code to make sure there are no errors but I am unable to get codeigniter to create update or retrieve any records from my wamp sever(ver 2.4) when I enter public_html/admin/migration in the task bar codeigniter returns a message saying "Migration worked!" which is the result that is expected when codeigniter has updated the records but no changes are made to the database this is the code used in the controller
<?php
class Migration extends Admin_Controller
{
public function __construct ()
{
parent::__construct();
}
public function index ()
{
$this->load->library('migration');
if (! $this->migration->current()) {
show_error($this->migration->error_string());
}
else {
echo 'Migration worked!';
}
}
}
I have set the autoload libraries as follows $autoload['libraries'] = array('database');
is there something I am missing
here is my migration library file called '001_create_users.php'
<?php
class Migration_Create_users extends CI_Migration {
public function up()
{
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'email' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'password' => array(
'type' => 'VARCHAR',
'constraint' => '128',
),
'name' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
));
$this->dbforge->create_table('users');
}
public function down()
{
$this->dbforge->drop_table('users');
}
}

Not sure if you are referring to the tutorial by Free Courses on youtube about cms buildout in codeigniter.
The way I fixed the issue (after a few hours) was debugging down to the environment setup.
Replace everything in your index function of the migration controller with var_dump($this->db) and see what it returns for your username/password/hostname/database, etc. If they are not what you expected per your database library setup, then your environment is not set properly in the index.php file.
I had to fix my case statement, had an extra / or \ can not remember which one, so the switch statement was forcing it to use the production environment configuration, which were not set.

add primary key before create table
$this->dbforge->add_key('id');

Related

What causes this "No migration could be found with the version number" error in Codeigniter 3?

I am working on a Social Network application with Codeigniter 3, Ion-Auth and Bootstrap 4. You can see the Github repo HERE.
I have enabled migrations, then created the migration file 002_add_messages_table.php:
class Migration_Create_Messages_Table extends CI_Migration {
public function up()
{
$this->dbforge->add_field(array(
'id'=>array(
'type'=>'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'from'=>array(
'type'=>'INT',
'constraint' => 11,
'null' => FALSE
),
'to'=>array(
'type'=>'INT',
'constraint' => 11,
'null' => FALSE
),
'message'=>array(
'type'=>'TEXT',
),
'status'=>array(
'type'=>'TINYINT',
'constraint' => 1,
),
'created_at'=>array(
'type'=>'TIMESTAMP',
'default' => NULL
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('messages');
}
public function down() {
$this->dbforge->drop_table('messages');
}
}
I also have a Migrate.php controller with this content:
<?php defined("BASEPATH") or exit("No direct script access allowed");
class Migrate extends CI_Controller{
public function index($version){
$this->load->library("migration");
if(!$this->migration->version($version)){
show_error($this->migration->error_string());
} else {
echo "Tables created";
}
}
}
When I access http://myapp.com/index.php/migrate/index/002, instead of creating a messages table, the application throws the message:
No migration could be found with the version number: 002.
What am I doing wrong?
*** UPDATED ***
First off, edit application/config/migration.php and set
$config['migration_type'] = 'sequential'; // was 'timestamp' in your repo
Secondly, edit your migration file and either change class name to
class Migration_Add_Messages_Table extends CI_Migration {
or rename the file as 002_create_messages_table
(class name and file name need to be similar)
THEN
If the following query
SELECT version FROM migrations
is returning anything greater than or equal to 2, what you can do is decrease the version value in your migrations table, and re-run the migration (if it's not on automatic).

Adding a new custom Customer attribute when updating a module

I'm having troubles at creating new customer attribute when upgrading one of my modules.
I've created the UpgradeData.php file under /app/code/vendor/modulename/Setup/UpgradeData.php with the current code:
namespace Ucs\CustomerAttribute\Setup;
use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
class UpgradeData implements UpgradeDataInterface{
private $customerSetupFactory;
public function __construct(
CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {#inheritdoc}
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context){
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.0.6') < 0) {
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'nome_azienda', [
'type' => 'varchar',
'label' => 'Nome azienda',
'input' => 'text',
'source' => '',
'required' => false,
'visible' => true,
'position' => 333,
'system' => false,
'backend' => ''
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'nome_azienda')
->addData(['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]]);
$attribute->save();
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'codice_univoco', [
'type' => 'varchar',
'label' => 'Codice Univoco',
'input' => 'text',
'source' => '',
'required' => false,
'visible' => true,
'position' => 333,
'system' => false,
'backend' => ''
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'codice_univoco')
->addData(['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]]);
$attribute->save();
}
}
}
in short, it needs to create 2 new text (varchar) attributes. My module.xml has
setup_version="1.0.5" schema_version="1.0.5" so it should enter the version_compare condition and create the attribute, but, after running php bin/magento setup:upgrade it doesn't work. If i check in the setup_module table, the setup_version and schema_version change correctly with the version in the module.xml. It looks like for some reason the UpgradeData.php does not get executed at all.
In Ucs\CustomerAttribute\etc\module.xml
change version to 1.0.6
then replace
if (version_compare($context->getVersion(), '1.0.6') < 0) {
with
if (version_compare($context->getVersion(), '1.0.6', '<')) {
Edit: Just to be sure.. by
I've created the UpgradeData.php file under
/app/code/vendor/modulename/Setup/UpgradeData.php
You mean app/code/Ucs/CustomerAttribute/Setup/UpgradeData.php ?
Edit2:
I assumed Your agency is called Ucs. That's why I've asked about it, beacuse that's what suggest Your module namespace.
This is not recomended practice but for purpose of installator verification, change namespace to:
namespace vendor\modulename\Setup;
What I recomend is:
Create a new module or find app/code/[YOURCOMPANYNAME]/Customer - try to corespond Magento native modules. This way You can easier manage code, design and Magento doesn't need to load separated module for each functionallity.
In UpgradeData.php try to call separate function for each version.
Like:
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$this->addCustomerMyAttribute();
}
$setup->endSetup();
and then below
private function addCustomerMyAttribute($setup){
// Your code goes here
}
If it's first version of Customer module in app/code/[YOURCOMPANYNAME] remember to create InstallData.php insted of UpgradeData.php (in that case no need to check version).
After bin/magento setup:upgrade check eav_attribute table for new attribute.
If it's there remember to bin/magento indexer:reindex so it goes to flat table. If it's not there. Put ```die(var_dump('I'm running'); at the beginning of upgrade function.

Can't register custom DBAL types

I'm trying to register a bunch of custom DBAL types. When I run the migrations:diff I get the exception:
Fatal error: Class 'App\Persistence\Models\Types\Money' not found in D:\development\projects\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\Type.php on line 174
I've tried to do this either by registering it after all Doctrine settings and using a event subscriber:
class DoctrineCustomTypesEventSubscriber implements Subscriber {
public function getSubscribedEvents() {
return [Events::postConnect];
}
public function postConnect(ConnectionEventArgs $args) {
Type::addType('money', "App\Persistence\Models\Types\Money");
Type::addType('geopoint', "App\Persistence\Models\Types\Point");
Type::addType('geoarea', "App\Persistence\Models\Types\Area");
}
}
$doctrineCustomTypesSubscriber = new App\Persistence\DoctrineCustomTypesEventSubscriber();
$app['db.event_manager']->addEventSubscriber($doctrineCustomTypesSubscriber);
$app->register(new Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider, array(
'orm.proxies_dir' => $app['APP_ROOT_DIR'].'/app/persistence/proxies',
'orm.em.options' => array(
'mappings' => array(
array(
'type' => 'annotation',
'namespace' => 'App\Persistence\Models',
'path' => $app['APP_ROOT_DIR'].'/app/persistence/models',
'use_simple_annotation_reader' => false,
),
),
),
));
Update
Placing the registration before all orm settings doesn't work either:
use Doctrine\DBAL\Types\Type;
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array('url' => $app['APP_DB_CONN_URL']),
));
Type::addType('money', "App\Persistence\Models\Types\Money");
Type::addType('geopoint', "App\Persistence\Models\Types\Point");
Type::addType('geoarea', "App\Persistence\Models\Types\Area");
What am I doing wrong here?
Also can you tell me where do I put these registrations:
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping(...)?
The type classes should exist and be registered in autoloader, so that they can be instantiated by FQCN associated with type.

Alias "custom.controllers.ExampleController.php" is invalid

This is the error:
Alias "custom.controllers.ExampleController.php" is invalid. Make sure
it points to an existing PHP file and the file is readable.
My code is given below
main.php=>
return
array(
'controllerMap' => array(
'product' => array(
'class' => 'custom.controllers.Product.php',
),
),
'import' => array(
'custom.mycompany.*',
),
'components' =>
array(
'widgetHandler' => array(
//Load a component
'class' => 'custom.mycompany.mywidget.mywidget',
),
)
);
Product.php=>
<?php
class Product extends Controller
{
public function actionIndex()
{
echo "this is the default index function";
}
public function actionTest()
{
echo "This is the test function";
}
}
I am using lightspeed cms.
the notation for Yii2 / php class is not dot based but slash based
'class' => 'custom\controllers\Product.php',
(And in your code there is not the ExampleController..)
see p https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
and https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md
https://github.com/yiisoft/yii2/blob/master/docs/internals/core-code-style.md
for PHP an Yii2 coding standard suggestion

Codeigniter / Migration Not Working

My migration is not working. I receive "Migration Work" in my browser but no blog table created which is what I expect.
This checks the revision number in migration.php and then looks for a file that
begins with that number (eg. 001_) in migrations folder.
<?php
// this is controller/Migration.php
class Migration extends CI_Controller {
function index() {
$this->load->library('migration');
if ( ! $this->migration->current()) {
show_error($this->migration->error_string());
} else {
echo "Migration Worked";
}
}
}
This is 002_install_blog.php It creates the blog table.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Install_blog extends CI_Migration {
public function up()
{
// Drop table 'blog' if it exists
$this->dbforge->drop_table('blog', TRUE);
// Table structure for table 'blog'
$this->dbforge->add_field(array(
'id' => array(
'type' => 'MEDIUMINT',
'constraint' => '8',
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'slug' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'body' => array(
'type' => 'TEXT',
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('blog');
}
public function down()
{
$this->dbforge->drop_table('blog', TRUE);
}
}
This is migration.php. It holds the migration version. It is set to 2 in order to use 002_install_blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['migration_enabled'] = TRUE;
$config['migration_type'] = 'sequential';
$config['migration_table'] = 'migrations';
$config['migration_auto_latest'] = TRUE;
$config['migration_version'] = '2';
$config['migration_path'] = APPPATH.'migrations/';
I figured this out. I have a table called 'migrations'. I incorrectly dropped my table inside of mysql and did not by running the migration file. Then, I ran the migration file, however, the version number in the 'migrations' table was still set to 2. I set the version number to 1 manually and then ran the migration file. Looked at the version number in the migrations table after running it and sure enough the version was 2.
Moral of the story. No need to drop tables manually. It should be all handled in the migration file!

Categories