YII Cannot Find Table - mysql

i am working in Yii using WAMP-MYSQL database.i have created tables as-
CREATE TABLE IF NOT EXISTS `balaee_dev`.`country` (
`countryId` INT(11) NOT NULL AUTO_INCREMENT ,
`country` VARCHAR(45) NULL DEFAULT NULL ,
PRIMARY KEY (`countryId`) ,
UNIQUE INDEX `country_UNIQUE` (`country` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
When I test the project here - it's giving me this error:
"The table "country" for active record class "country" cannot be found in the database", though this table exists in database. I am getting this error for each and every controller. Here is my config file:
<?php
// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Console Application',
// application components
'components'=>array(
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=balaee_dev',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
),
);
And i have main.php as-
<?php
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Balaee Application',
// preloading 'log' component
'preload'=>array('log'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
'application.modules.KnowledgePortal.*',
'application.modules.QuestionBank.*',
//'application.modules.UserAuthentication.*',
'application.plugins.*',
'application.extensions.noaaWeather.*',
),
'modules'=>array(
//Question Bank Module
'QuestionBank'=>array(),
//Social Networking Module
'SocialNetworking'=>array(),
//Knowledge Portal Module
'KnowledgePortal'=>array(),
// uncomment the following to enable the Gii tool
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'root',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
//'ipFilters'=>array('204.93.172.30'),
),
),
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'mailer' => array(
'class' => 'application.extensions.mailer.EMailer',
'pathViews' => 'application.views.email',
'pathLayouts' => 'application.views.email.layouts'
),
'curl'=>array(
'class' => 'application.extensions.curl.Curl',
),
'params'=>array(
'noaaWeather.cachePath' => 'protected/extensions/noaaWeather/cache'
),
// uncomment the following to enable URLs in path-format
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
/*
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
*/
'db'=>array(
//'connectionString' => 'mysql:host=localhost;dbname=balaee_dev',
'connectionString' => 'mysql:host=204.93.172.30;dbname:shailani_balaee_dev',
//'emulatePrepare' => true,
'username' => 'shailani_bdev',
'password' => 'nTRXpfuH8wmjx5hV',
'charset' => 'utf8',
),
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=balaee_dev',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
// uncomment the following to show log messages on web pages
/*
array(
'class'=>'CWebLogRoute',
),
*/
),
),
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster#example.com',
),
);
So please can someone guide me what changes i need to make?

You say you're using MySQL. So the sqlite line in your configuration shall not appear.
Provided the tables have been actually created in your MySql database (I guess you can see them in MySQL Workbench or in PHPMyAdmin):
remove this line
re-run the model / CRUD / whatever generation.
If your connection string and params are OK, it should work.
If it doesn't, feel free to copy here the related errors logs : Yii, MySql and Apache's logs can contain useful information.

Related

cakephp + phpunit + gitlab continuous integration

I'm looking for help with my tests in cakephp + phpunit + gitlab-ci.
I defined my test/fixtures like this: public $import = ['model' => 'MyModel']; to avoid having to redefine the tables schema in the fixture, and have out of sync/wrong code.
I understand that what it does is to look for the live db and use that information to generate the tables in the test db, but what about if there is no live db?
I'm trying to automate my testing in gitlab / continuous integration environment, and there they provide you a mysql container with a single db to run your tests. If I have a single DB, i will not be able to have both a main connection and the testing db, right?
I was thinking on having a create-db-for-tests.sql and imprort that script BUT I understand that running a test with a fixture will remove the table, so I would need to create it again and again on every test.
I could not use the gitlab mysql service and install mysql in the testing container to have 2 databases, like in dev, but I would need to populate the other DB with some script too, and that's the same as defining the db structure in the fixtures.
How can I solve this problem?
Any help will be gladly appreciated.
Something came out from my mind after I read your question.
If you don't have live db, there might be two possibility which is
you may have staging db or your application don't have DB.
According to your second question related about If I have single DB, I would say No. Because, you could have two connections.
e.g
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'foge',
'prefix' => '',
'encoding' => 'utf8mb4',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'root',
'database' => 'test_foge',
'prefix' => '',
'encoding' => 'utf8mb4',
);
When creating a fixture you will mainly define two things: how the table is created (which fields are part of the table), and which records will be initially populated to the table.
I think, you can populate your script with this. Because this is mysql command. But, say honestly i haven't try it before.
mysql -u jenkins -pcakephp_jenkins -e 'DROP DATABASE IF EXISTS test_foge; CREATE DATABASE test_foge; CREATE TABLE users; CREATE Table posts';
And the last one is you can populate data before you run it and I guess you already knew it.
class ArticleFixture extends CakeTestFixture {
public $fields = array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'title' => array('type' => 'string', 'length' => 255, 'null' => false),
'body' => 'text',
'published' => array('type' => 'integer', 'default' => '0', 'null' => false),
'created' => 'datetime',
'updated' => 'datetime'
);
public function init() {
$this->records = array(
array(
'id' => 1,
'title' => 'First Article',
'body' => 'First Article Body',
'published' => '1',
'created' => date('Y-m-d H:i:s'),
'updated' => date('Y-m-d H:i:s'),
),
);
parent::init();
}
}
As my personal perspective, good idea to drop and re-create the database before each build as well. This insulates you from chained failures, where one broken build causes others to fail.

How can I use an existing database with the advanced template of Yii2?

I installed the yii2 advanced template, and I'm now at migration point where I should create a new database and migrate the app to it.
but I already have a database full of data and I want to use it with yii2 without modifying anything
Open the file
common/config/main.php
and add the parameters for your database connection
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
],
],
];

Magento 2 separate read/write database connections

What is the proper way to define separate read and write connections to separate MySQL databases in app/etc/env.php for Magento 2?
So the key here is to think of the Magento 1.X read write connections as master/slave connections in 2.X. I believe this is an enterprise only feature so community editions users might be out of luck. Below is an excerpt of my app/etc/env.php. We are using haproxy to balance read/write connections to a Percona cluster so unless you have the same setup you will need to configure your hosts to the appropriate IP's.
...
'db' =>
array (
'connection' =>
array (
'default' =>
array (
// HaProxy Write (master) connection
'host' => '127.0.0.1:3308',
'port' => '3308',
'dbname' => 'magento_db',
'username' => 'username',
'password' => 'password',
'active' => '1',
),
),
'slave_connection' =>
array (
'default' =>
array (
// HaProxy Read (slave) connection
'host' => '127.0.0.1:3307',
'port' => '3307',
'dbname' => 'magento_db',
'username' => 'username',
'password' => 'password',
'active' => '1',
),
),
'table_prefix' => '',
),
...

How to replace environment-specific config, not cascade

I want to have a local development environment configuration for my database.php. I've created a new config director in my app and copied the database.php file into it. I'm using MongoDB and on production, I'm using a replica set with multiple servers, but locally, I'm just running a single server, so my connection info for production has more options:
Production:
...
'connections' => array(
'mongodb' => array(
'driver' => 'mongodb',
'host' => array('mongoDBA', 'mongoDBB'),
'port' => 27017,
'username' => 'myUserName',
'password' => 'myPassword',
'database' => 'theDatabase',
'options' => array('replicaSet' => 'myReplicaSet')
)
),
Local:
...
'connections' => array(
'mongodb' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'database' => 'theDatabase'
)
),
The problem is, when my local environment config loads, it merges the "connections" array. I want a way to completely replace the "mongodb" connection, so it's either one or the other, not both.
How can I accomplish this?
Create different set of configuration files:
app/config/local/database.php
app/config/production/database.php
And delete the file (or what you don't want of it):
app/config/database.php
And the you have to set your environment as
local
development
EDIT
This is how I do set my environment flawlessly, so I don't have to deal with hostnames and still don't get my local environment conflict with staging and production.
Create a .environment file in the root of your application and define your environment and add your sensitive information to it:
<?php
return array(
'APPLICATION_ENV' => 'development', /// this is where you will set your environment
'DB_HOST' => 'localhost',
'DB_DATABASE_NAME' => 'laraveldatabase',
'DB_DATABASE_USER' => 'laraveluser',
'DB_DATABASE_PASSWORD' => '!Bassw0rT',
);
Add it to your .gitignore file, so you don't risk having your passwords sent to Github or any other of your servers.
Right before $app->detectEnvironment, in the file bootstrap/start.php, load your .environment file to PHP environment:
foreach(require __DIR__.'/../.environment' as $key => $value)
{
putenv(sprintf('%s=%s', $key, $value));
}
And then you just have to use it:
$env = $app->detectEnvironment(function () {
return getenv('APPLICATION_ENV'); // your environment name is in that file!
});
And it will work everywhere, so you don't need to have separate dirs for development and production anymore:
<?php
return array(
'connections' => array(
'postgresql' => array(
'driver' => 'pgsql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_DATABASE_NAME'),
'username' => getenv('DB_DATABASE_USER'),
'password' => getenv('DB_DATABASE_PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
),
);
Note that I don't set a fallback:
return getenv('APPLICATION_ENV') ?: 'local';
Because, if I don't set the file, I want it to fail on every server I deploy my app to.

Can i use mysql with yii framework?

I have xampp installed which includes mysql. Can i use this mysql with yii framework?
If yes how can i do it with phpmyadmin. Pls help. Am a newbie..
I have seen many people using sqlite with yii in many tutorials..
ok what you need to do is open /protected/config/main.php, and look for the line like this :
),
'db'=>array( // as you can see this db is currently in use
'connectionString' => 'sqlite:protected/data/blog.db',
'tablePrefix' => 'tbl_',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
*/
just comment the first connection to sqlite data base and uncomment the second part that uses mysql database :
),
/*'db'=>array(
'connectionString' => 'sqlite:protected/data/blog.db',
'tablePrefix' => 'tbl_',
),*/
// uncomment the following to use a MySQL database
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
),
that's all!