table prefix in model generator
Closed this issue · 21 comments
after install via composer and setup everything looks fine so far.
need to use table prefix in my db (provider),
just didn't find any config docs in giiant and generating with "tbl_*" gives me
models with "TblTableName" where "TableName" is suggested
There was a discussion in the yii2 repo about this, it may be a default setting from an older version (either Connection or in the app config). Can't you set a tbl_
as a prefix in the CRUD generator?
this worked fine in yii-gii and GiixModel Generator.
I didn't find any setting for 'Table Prefix' in yii2-gii or yii2-giant
also searched in app config (maybe there is a undocumented entry to 'db' ?)
problem starts with relations --- have to edit relating classnames by hand when i use
ModellClassNames different to TableNames. What happens if there are used more
then one prefix ... (auth_ , usr_, time_) for different Modules/Components ?
Do you have the latest Yii 2 version (dev-master)?
There was an issue here: yiisoft/yii2#2922, yiisoft/yii2#2911
What changes when you check the Use Table Prefix
option?
TablePrefix in 'db' works as expected ...
had to change crud controller template
function findModel
if (($model = ::find()) !== null) {
if (($model = ::findOne()) !== null) {
since findModel is supposed to find one record by primary key.
first tests look fine so far
thxs for fast response
Please send a PR if this is a general fix.
I think there were several changes also to yii2-gii
in the past weeks, we'll have to review this (@dmstr/developers)
There's a checkbox ... use table prefix
.
I am not 100% sure, but I think Yii is always using the connection prefix from the database.
While giiant allows you to specify a table prefix, just for CRUD generation.
In giiant this setting is used to generate model class names like so: prefix_model_name
=> app\models\ModelName
.
I'll clarify this and update the docs.
@schmunk42 thank you for your answer.
I am in the process of migrating from Cake Php to YII. I want to know where to put the prefix, in what file.
I know there is a checkbox, just want to know how to supply the actual prefix.
Do you need a prefix for your database connection?
http://www.yiiframework.com/doc-2.0/yii-db-connection.html#$tablePrefix-detail
This applies to all tables from this connection.
You put that setting in the application config config/main.php
components > db
wow, that is great documentation!
And the prefix is indeed for the connection, all the table names have the same prefix, I want to use GII and this will make my life easier.
Thanks and keep up the good work!
Edit in db.php :
'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname= ******_', 'username' => '**_**_', 'password' => '**_****_', 'charset' => 'utf8', 'tablePrefix' => '**_**_' ]; Then generate CRUD. After that generate Model with option "Use Table Prefix" (Check) Voila tout. PS In models you can see like public static function tableName() { return '{{%tablename_without_prefix}}'; }I used Giiant for CRUD generation, it didnt create any model :( I get error like: Class 'app\modules\crud\models\Faq' not found
Did you use the Web UI or the CLI batch command?
Does the table in the used db connection exists?!
Could you give some more information about your configuration?
I used the CLI command "yii batch". I updated console->config->main.php with 'controllerMap' as under:
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'controllerMap' => [
'batch' => [
'class' => 'schmunk42\giiant\commands\BatchController',
'overwrite' => true,
'modelNamespace' => 'backend\\crud\\models',
'crudTidyOutput' => true,
]
],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
'params' => $params,
];
The class backend\crud\models\base\Faq.php has the following function:
public static function find()
{
return new \app\models\query\FaqQuery(get_called_class());
}
I am getting this error: Class 'app\models\query\FaqQuery' not found
Double check your namespaces.
app/modules/crud
and
backend/crud
do not match.
Von meinem iPhone gesendet
Am 03.04.2016 um 16:38 schrieb ik-j notifications@github.com:
I used the CLI command. I updated console->config->main.php with 'controllerMap' as under:
return [
'id' => 'app-console',
'basePath' => dirname(DIR),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'controllerMap' => [
'batch' => [
'class' => 'schmunk42\giiant\commands\BatchController',
'overwrite' => true,
'modelNamespace' => 'backend\crud\models',
'crudTidyOutput' => true,
]
],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
'params' => $params,
];—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
Where shud I check ? Please guide that I have placed the batch config in the right place i.e. in console->config->main.php ?
Actually FaqQuery does exist in \app\models\query\ namespace. But still it says, class not found.
Schmunk I love your module giiant-yii2. I have been using it in all of my projects. Great piece :)
Thank you 🙇
Sorry I mixed up the namespace paths...
Could you try setting the modelQueryNamespace
property like described here: https://github.com/schmunk42/yii2-giiant/blob/master/docs/20-batches.md
or look at the example and way we do it here:
'https://github.com/phundament/app/blob/master/src/config/giiant.php'
with application config
'https://github.com/phundament/app/blob/master/src/config/main.php'
I am using Yii2 advanced app. So "CLI configuration" in advanced app would mean console->config->main.php ?
exactly ;-)
just merge your giiant.php configuration file into your console->config->main.php.
cheers chris