About CRUD generator in Yii - yii

i am starting to use Yii framework and the gii tool to create the crud functionality for several tables.
The issue is that i need normally to modify the generated code. For example, i have noticed that all the forms use only text inputs. For example, in the case of foreign keys i need to show a select where the user can select a register of the other table without having to insert the primary key.
I achive this modifying the involved views (_view.php, _form.php, ...). My problem starts when it's necessary to modify the database and regenerate the code again. For that case i would have to go table by table, updating the involved files one by one keeping my old changes.
Does it exist a way to deal better with this situation?

The GiiX extension generates dropdowns for relationships.
In case you use the Gii Model generation, GiiX generates better models, too.

You don't have to do that. You can generate a select with the foreign keys, for example.
<?php echo CHtml::dropDownList('ModelName[language_id]',$model->language_id,CHtml::listData(Language::model()->findAll(), 'id', 'name'), array('prompt'=>'Select Language')); ?>
The first parameter is the name of the select, second is the value (if you're updating, the value will be selected), third is the data, and the fourth is an array with html options. More info about CHtml::dropDownList and CHtml::listData.
In this, the language_id is the foreign key, and in the Language table you have an id and a name field. This will generate a select with all the available languages, like this : 1=> English, 2=> German etc.

If you're going to need the functionality of crud form generation a couple of times, with the option of show foreign keys as dropdowns, you can create your own gii template to do it for you. Take a look at the guide to know how to do it.

For this functionality you need to develop you gii tool which will inherit functionality of existing gii tool, means some modification in existing classes while creating CRUD. This is your specific requirement, gii tool provides only general functionality which suits to all users.

Related

Is it a good idea to manually create columns in existing AspNetUser table?

I'm using Identity 3 and I want to add some columns in AspNetUser table.
I've tried to do it using EF Core Code first and it's working well but I need to do it DB first.
I was wondering, if I will create the columns manually in database, then create the ApplicationUser class with corresponding properties, will it work?
Yup that should work, I've done it before.
However as time goes on I ended up having to add so many that it got messy.
So eventually I refactored those extra columns into their own related tables:
e.g: User_AdditionalDetails
This was a massive pain as I had live users and had to write scripts to migrate everyone's data etc.
This way you would only need to add a single FK for the related table with all this extra info.
It also neatens the code too, and gives the benefit of being able to load different sets of user properties only when they are needed.
If it's for an application scope property of the user like 'Region' which determines behaviour of core functionality of your app, then I'd say add it straight onto the main ApplicationUser class.

ASP.NET MVC is it okay to work with the ApplicationUser model for account management?

I know that each time a user registers in my ASP.NET MVC application the ApplicationUser class is used to create the new record and put it in the database.
I was wondering if it's okay to add properties to that class for example I want the model to have a column in the database for DateOfBirth. Then use that class(model) directly in my application when I have to do some business logic things, database queries and similar stuff. Or is it more correct to create a new table in the database called let's say ApplicationAccounts, that saves the general info about the account. Each ApplicationAccount will be associated with a ApplicationUser(1 to 1 relation) and be somewhat of a buffer in the communication with the real accounts. Does that make sense?
I would go with the second option : create your own table, link them up in a one to one relationship using the UserID as a unique foreign key and then go from there.
One note here, it is perfectly normal for the model you need for the views to be different from the database model, this is because your db model can hold a lot of data that your view doesn't actually need. You might want to consider having separate models and use something like Automapper for a quick translation from one to another.

Piranha CMS Custom Entities

I am new the Piranha. I want to make a new table in database like as site messages or newsletter form.
Can I make extension or what is the right way for Piranha?
Additional info: I am not using source code. I add piranha to my project from Nu-get.
just add entities/tables to your database using entity framework or whatever technology you like.
Remember that if you use an ORM you will can't have foreign key to tables in other contexts (Piranha) so you'll have to handle relationships with software triggers.
Regards
HÃ¥kan

Gii: generate CRUD application from existing database tables?

I have a mysql database with tables that have one to many relationships with foreign keys linking them.
How can I use gii to create CRUD form pages which will reflect the table relationships?
I suggest you to take a look at the following article which includes using yii's gii
Creating Your First Yii Application

Is there a way you can specify a custom ROLES table to use just like you can specify the USERS table in Websecurity.InitializeDatabase()?

So I know you can specify what custom table you would like to use for the Users table when utilizing the InitializeDatabase method on the WebMatrix.WebData.Websecurity class, but I wanted to find out if there is no way to specify the Roles table to use? There might not be any reason - except if you wanted more control over what data is stored with a role.
I think there is no way to specific roles table in Websecurity.InitializeDatabase()
but for example you can include Asp.net Simple Membership tables as part of your entity framework model and more control over it.
This article might assist you in resolving your issue.