Codeigniter / Migration Not Working - php

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!

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).

Codeigniter 3 migrations: add the Uncategorized posts category at the first migrations run

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
I use migration files (001_create_authors.php up to 005_create_comments.php) to automatically create the necessary database tables.
In addition to creating the categories table, I need to insert the default "Uncategorized" category in it, as posts must belong in a category.
The migration's current code:
class Migration_Create_Categories extends CI_Migration
{
public function up()
{
$this->dbforge->add_field(array(
'id'=>array(
'type'=>'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'author_id'=>array(
'type'=>'INT',
'constraint' => 11,
'unsigned' => TRUE,
),
'name'=>array(
'type'=>'VARCHAR',
'constraint' => 255,
),
'created_at'=>array(
'type'=>'TIMESTAMP',
)
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('categories');
}
public function down()
{
$this->dbforge->drop_table('categories');
}
}
What must I add to he above code in order to insert the default "Uncategorized" category in the categories table, at the first migrations run?
$this->dbforge->create_table('categories');
//after this line
$data = array(
//'id' => leave this as it will auto created
'author_id' => 1,
'name' => 'Uncategorized'
//'created_at' => leave it as this will auto created
);
$this->db->insert('categories', $data);

No migration could be found with the version number: 1

I tried to followed the codeigniter tutorail on youtube here about creating migration in codeigniter. However, I got error
No migration could be found with the version number: 1
I already set $config['migration_version'] = 1; in Application/Config/migration.php and my migration file for creating users table
Application/migrations/001_Create_User.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Create_Users extends CI_Migration {
/*
up function is for creating and alert table
*/
public function up()
{
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'email' => array(
'type' => 'VARCHAR',
'constraint' => '128',
),
'password' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
));
$this->dbforge->add_key('id',TRUE);
$this->dbforge->create_table('users');
}
/*
down function for rollback table
*/
public function down()
{
$this->dbforge->drop_table('users');
}
}
?>
When I check my database, I saw migration table version is always 0.
Please help me, thanks
In config/migration.php
/*
|--------------------------------------------------------------------------
| Migration Type
|--------------------------------------------------------------------------
|
| Migration file names may be based on a sequential identifier or on
| a timestamp. Options are:
|
| 'sequential' = Default migration naming (001_add_blog.php)
| 'timestamp' = Timestamp migration naming (20121031104401_add_blog.php)
| Use timestamp format YYYYMMDDHHIISS.
|
| If this configuration value is missing the Migration library defaults
| to 'sequential' for backward compatibility.
|
*/
$config['migration_type'] = 'sequential';

Override class of CGridColumn

i am just trying to override the class of CGridColumn but somehow its not overriding.
my code is below
<?php
Yii::import('zii.widgets.grid.CGridColumn');
class CGridColumnCustom extends CGridColumn
{
// new variable that will bind near header
public $headerFilter;
public function renderHeaderCell()
{
$this->headerHtmlOptions['id']=$this->id;
echo CHtml::openTag('th',$this->headerHtmlOptions);
$this->renderHeaderCellContent();
$this->renderFilterHeaderCellContent();
echo "</th>";
}
#------------------------------------------------------------------------------------
# custom function that will concat with renderHeaderCellContent at renderHeaderCell
#------------------------------------------------------------------------------------
protected function renderFilterHeaderCellContent()
{
echo trim($this->headerFilter)!=='' ? $this->headerFilter : $this->grid->blankDisplay;
}
}
?>
and i added this file(CGridColumnCustom.php) in components folder and i also imported this file in CustomerModule.php file like below
$this->setImport(array(
'customer.components.*',
));
but when i am trying to implement my custom function like below in view file
'columns'=>array(
array(
'name' => 'Name',
'value' => '$data->Name',
'type' => 'raw',
'headerFilter'=> '<span class="name-filter-head" onclick="alert(\'test\');"> CustomFilter</span>',
),
then it will give below error
Property "CDataColumn.headerFilter" is not defined.
But if i directly added those changes on Core file at CGridColumn.php at gii.widgets.grid then its working fine but i don't want to change in core file.
Try to do it following way:
First thing (actually not sure if it's relevant, but name your class CGridCustomColumn with word 'Column' on the end.
Second, view should be like this then:
'columns'=>array(
array(
'header' => 'CGridCustom',
'class' => 'CGridCustomColumn'
'value' => '$data->Name'
'type' => 'raw'
),

Codeigniter an migration not working with wamp

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');

Categories