Gii not detecting mysql tables in model creator - yii

From config/main.php :
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdb',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
When I use the gii model generator (http://localhost/yii/testdelete/index.php?r=gii/model) and try to put "post" or "user" in the table name and click "Preview" it says:
Table 'post' does not exist.
I want to be able to create models using those tables.
Exporting from phpMyAdmin:
-- Host: 127.0.0.1
-- Database: `testdb`
CREATE TABLE IF NOT EXISTS post (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
created_on int(11) unsigned NOT NULL,
title varchar(255) COLLATE utf8_unicode_ci NOT NULL,
content text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS user (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
username varchar(200) COLLATE utf8_unicode_ci NOT NULL,
password char(40) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

If you type "s" into Table Name field auto complete should pop up with both tables.
check that
you have saved the config file (close it and open again to verify its content)
you have one db section in your config file (maybe you have sqlite one overwriting it)
check that you are on same mysql instance. phpMyAdmin host 127.0.0.1 doesnt meant its the same instance, its the instance phpMyAdmin runs on (check the url). Login from command line (or use wonderful desktop client http://www.heidisql.com/) to poke around in localhost database

Related

A Symbol Name was Expect by 'id"

I'm trying to make a table for a login system for a simple website project but I'm having this error that I wasn't having the other day. I'm getting the errors at 'id' and unsigned on the 2nd line. The error is "A Symbol Name is Expected Near 'id'. I also get an error by unsigned on the same line that says "unrecognizable statement type". I am using XAMPP.
CREATE TABLE 'users' (
'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
'username' varchar(75) NOT NULL,
'password' varchar(255) NOT NULL,
'email' varchar(100) NOT NULL,
'phone' varchar(15) NOT NULL,
'address' varchar(255) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE KEY 'email' ('email')
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;
You're using single quotes for identifiers (column and table names). Single quotes are for strings.
Identifiers are either unquoted, or use the MySQL/MariaDB specific `. The SQL standard uses ". Consider turning on ANSI mode for compatibility.
Also consider using the serial shorthand for bigint unsigned not null auto_increment unique.
create table users (
id serial primary key,
...
)

Yii accessRules() authorization method issues - getting 403 error

Instead of using the raw username / password generated by Gii, I'm trying to implement a database to store those username/passwords as well as a role field.
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(128) NOT NULL,
`password` varchar(128) NOT NULL,
`role` int(1) NOT NULL DEFAULT '0',
`create_date` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `superuser` (`role`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
I'm struggling to understand this:
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
I'm getting:
403 unauthorised
If I use # it works. So, I'm clearly missing a connection here, between the user array contents, and the database username.
How can I properly connect the 'admin' to the database username field?
Can I have a push please?

Yii create table from application

In my yii aplication we need to create dynamic table.
so i hav used like below:
Yii::app()->db->createCommand("CREATE TABLE {$tokenid}( `column1` INT(11) NOT NULL AUTO_INCREMENT, `column2` VARCHAR(255) NOT NULL, PRIMARY KEY (`column1`) ); ");
But it's not working. Any suggestion
You should use createTable() for this. An example from the docs:
// CREATE TABLE `tbl_user` (
// `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
// `username` varchar(255) NOT NULL,
// `location` point
// ) ENGINE=InnoDB
createTable('tbl_user', array(
'id' => 'pk',
'username' => 'string NOT NULL',
'location' => 'point',
), 'ENGINE=InnoDB')
You need to execute the command,
Yii::app()->db->createCommand("CREATE TABLE {$tokenid}( column1 INT(11) NOT NULL AUTO_INCREMENT, column2 VARCHAR(255) NOT NULL, PRIMARY KEY (column1) ); ")->execute();
or look at the create table function http://www.yiiframework.com/doc/api/1.1/CDbCommand#createTable-detail

Gii CRUD generator and related tables

I am using Yii framework and I have got a problem with CRUD generator.
I have got two tables called Users and news with the following structures:
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`keyword` varchar(1000) COLLATE utf8_persian_ci DEFAULT NULL,
`user_id` tinyint(3) unsigned NOT NULL,
`title` varchar(100) COLLATE utf8_persian_ci DEFAULT NULL,
`body` varchar(1000) COLLATE utf8_persian_ci DEFAULT NULL,
`publishedat` date DEFAULT NULL,
`state` tinyint(1) unsigned DEFAULT NULL,
`archive` tinyint(1) unsigned DEFAULT NULL,
`last_modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `news_FKIndex1` (`keyword`(255)),
KEY `news_FKIndex2` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(128) NOT NULL,
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`lastvisit_at` timestamp NULL DEFAULT NULL,
`is_disabled` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `status` (`is_disabled`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
when I generate a CRUD using Gii for my news table I cannot see the fields for users table. Instead of user_id I want to see the username in the table created by CRUD generator. How can I make a change in the code to get the result as above?
First, user_id needs to be a foreign key field not just a key field.
Second, gii will not generate the field as you require by default. For such functionality an extension such as Giix might help. However, since a relation exists you could always use relationName.username to display the username in a grid view or a list view.

Optimize a mysql like query

I added the jquery autocomplete plugin to my places textfield to help users better select a location. What I didn't realize before building is that the query would be very slow.
select * from `geoplanet_places` where name LIKE "%San Diego%" AND (place_type = "County" OR place_type = "Town")
The query above took 1.18 seconds. Then I tried adding indexes for name and place_type but that only slowed it down (1.93s).
Is there a way to optimize this query or is there another technique to speed up the query.
This geoplanet_places table has 437,715 rows (mysql)
CREATE TABLE `geoplanet_places` (
`id` int(11) NOT NULL auto_increment,
`woeid` bigint(20) default NULL,
`parent_woeid` bigint(20) default NULL,
`country_code` varchar(255) collate utf8_unicode_ci default NULL,
`name` varchar(255) collate utf8_unicode_ci default NULL,
`language` varchar(255) collate utf8_unicode_ci default NULL,
`place_type` varchar(255) collate utf8_unicode_ci default NULL,
`ancestry` varchar(255) collate utf8_unicode_ci default NULL,
`activity_count` int(11) default '0',
`activity_count_updated_at` datetime default NULL,
`bounding_box` blob,
`slug` varchar(255) collate utf8_unicode_ci default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_geoplanet_places_on_woeid` (`woeid`),
KEY `index_geoplanet_places_on_ancestry` (`ancestry`),
KEY `index_geoplanet_places_on_parent_woeid` (`parent_woeid`),
KEY `index_geoplanet_places_on_slug` (`slug`),
KEY `index_geoplanet_places_on_name` (`name`),
KEY `index_geoplanet_places_on_place_type` (`place_type`)
) ENGINE=InnoDB AUTO_INCREMENT=5652569 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
EXPLAIN
id 1
select_type SIMPLE
table geoplanet_places
type ALL
possible_keys index_geoplanet_places_on_place_type
key NULL
key_len NULL
ref NULL
rows 441273
Extra Using where
You can switch the storage engine of the table to MyISAM to take advantage of full text indexing.
The name index wont help you unless you change the like to LIKE 'San Diego%' which can do a prefix search on the index
Get rid of the leading '%' in your where-like clause, so it becomes: where name like "San Diego%". For auto complete, this seems a reasonable limitation (assumes that the user starts typing correct characters) that should speed up the query significantly, as MySql will be able to use an existing index (index_geoplanet_places_on_name).