Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table prefix in model generator #8

Closed
bowl4fun opened this issue May 16, 2014 · 21 comments
Closed

table prefix in model generator #8

bowl4fun opened this issue May 16, 2014 · 21 comments

Comments

@bowl4fun
Copy link

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

@schmunk42
Copy link
Owner

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?

@bowl4fun
Copy link
Author

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 ?

@schmunk42
Copy link
Owner

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?

@bowl4fun
Copy link
Author

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

@schmunk42
Copy link
Owner

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)

@schmunk42
Copy link
Owner

Custom table prefix added in 513cf79

bildschirmfoto 2014-06-12 um 10 30 43

@progmax progmax mentioned this issue Dec 11, 2014
@snelweg
Copy link

snelweg commented Mar 5, 2015

Using YII 2.03 table prefix is gone now?

naamloos

@schmunk42
Copy link
Owner

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.

@snelweg
Copy link

snelweg commented Mar 5, 2015

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

@schmunk42
Copy link
Owner

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

@snelweg
Copy link

snelweg commented Mar 5, 2015

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!

@Choockas
Copy link

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

@ik-j
Copy link

ik-j commented Apr 3, 2016

I used Giiant for CRUD generation, it didnt create any model :( I get error like: Class 'app\modules\crud\models\Faq' not found

@Quexer69
Copy link
Contributor

Quexer69 commented Apr 3, 2016

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?

@ik-j
Copy link

ik-j commented Apr 3, 2016

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

@schmunk42
Copy link
Owner

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

@ik-j
Copy link

ik-j commented Apr 3, 2016

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

@schmunk42
Copy link
Owner

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

@Quexer69
Copy link
Contributor

Quexer69 commented Apr 3, 2016

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'

@ik-j
Copy link

ik-j commented Apr 3, 2016

I am using Yii2 advanced app. So "CLI configuration" in advanced app would mean console->config->main.php ?

@Quexer69
Copy link
Contributor

Quexer69 commented Apr 3, 2016

exactly ;-)

just merge your giiant.php configuration file into your console->config->main.php.

cheers chris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants