CI: generate migration on model change - php

Does a utility exist in Codeigniter to automatically generate a migration script when a model or schema changes? If so, where is it?

Unfortunately, no such utility exists. Though it would definitely be a fantastic one. You could write one to monitor changes in db structure, though that might be a monster project. Alternatively, A library could be written to provide that feature.

Related

Is it possible to use a model in it's own factory in a Laravel project?

My team is building a project using laravel for the server code. Early on we were going to use models to manage some of the images used in the project, but we didn't have the images from the client yet. So we built out the models and used a laravel factory and faker to create fake images for testing.
A month later (and many test cases later) we have the actual images from the client. I added the images to the project, created structural database seeders to populate the data needed for the database, and wrote out unit tests for the model to confirm it works.
The issue is that now some of the tests are failing because the factory we're using for the image model still uses factory and faker. Anywhere where we need to look for a specific file using the model we get fails from the fake data provide by faker.
I thought "well that's fine, I can just switch out the fake data in the factory for randomized data from the actual model". The problem I'm running into now is that when I try to use the actual model within the factory function, the model class only gets provided as a factory:
I know there's a good reason for this happening behind the scenes, I'm just wondering if there's a way of getting around it. If it's possible to use the actual model in the factory it would prevent me from having to rewrite a lot of test cases to swap out the factory for the actual model. It also seems like this would be a really convenient way of being able to do feature testing for items that you know will exist but don't have the actual assets for yet.
Is there a way around this or should I plan on buckling down and refactoring my tests?
A factory exists to generate a model on demand using fake data — not necessarily from faker but data that doesn't represent an entity that already exists. You are attempting to create factories that depend on the database having been populated with real data but if you have a database with real data that you need to test against then you should use that data directly. Your approach would give tests a dependency on real data which would now need to be distributed to all developers and build services.
Populating your database with a fixed set of data for testing should be done using Database Seeders. At the start of the development you should create a database seeder (which can use factories) to populate your database with fake data, then once you obtain the real data you can add an additional database seeder (or populate the database directly from outside of Laravel if necessary). This approach means that your application can be tested without any concern for whether the data is real or not, and you'd continue to be able to use factories as they're intended to be used.
The unfortunate conclusion here is that if you wish for your tests to be resilient you're going to need to refactor.
If you absolutely must use factories with real data then you can create your own faker provider that has fallbacks for if the data does not exist.
I believe if you declare the class object as the Faker class is being passed as a parameter within the factory declaration, you should be able to use it within the factory itself. I might be wrong though, I believe it happens that way because the factory itself is a function call, so any parameters used need to be declared before hand within its function() call.
If I recall correctly, if you do this:
$factory->define(App\models\AreaOfAffectMap::class, function(AOA $AOA) {...
It should work.

Laravel 5.3 Writing a DB routine/subroutine in a migration file?

In a previous question, I asked about how to write a trigger and gave a decent example of one way to write it in a laravel 5.X migration file. (note: hard/raw coded)
Referenced Question:
https://www.stackoverflow.com/questions/39177303/laravel5-x-database-triggers-and-possible-best-practices
However, as the title states, does anyone know how to write a database routine/subroutine in a laravel migration file??
Or
Does anyone know of a clean way to implement using laravel's methods to create a migration file that will make subroutines or routines within a database?

How to add fields to a model via eloquent in Laravel?

My question is if it is possible to add all the fields directly to a new model via Eloquent.
I guess it would be something like
php artisan make:model MyModel --fields=?
However, I can't find anything related with that. Anyway, I have to generate a lot of model and any trick would be really appreciated.
Thanks in advance
If you mean table's column by fields then:
Firstly you don't need to define fields in modal. I mean in Laravel no need to define fields while creating model. Besides, model automatically work with your database table's columns as its property.
So, now you may want to define columns while creating migration, not while creating model. There is library to serve this demand named as Generator(https://github.com/laracasts/Laravel-5-Generators-Extended) maintained by Laracasts.
Using this generator you can generate migration file to create table in DB specifying their column names and their type also. Here is a example from their Github repo, how you can do this:
php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"
You can checkout their documentation for more information. Best of luck.
It's not possible with make:model or make:migrations commands, but you can create your own console command and add this feature by yourself.
Also, take a look at source code of make:model and make:migration commands to get some ideas on how to do that.
it looks like only built in options are --migration and -m to include a migration with the model generation. L5.3 Docs
There does look like there is a package for L5.0, which looks like it would work in 5.*+. It is put out by Laracasts:
https://github.com/laracasts/Laravel-5-Generators-Extended
It also looks like you can make a custom solution as well:
https://laracasts.com/discuss/channels/tips/l5-artisan-command-makemodel
Hope that helps!
No options while creating a model,
This is my terminal output (laravel 5.3) while i check,
You don't need to mention fields while creating model.
Ex:- based on the rules you should keep the names as like below,
model name as User
table name as users
then the model automatically handle everything, you don't need to mention the table/fields name.
I was looking for the same thing myself, as I used to work like that in previous frameworks, but could not find it, or at least not as I wanted it, so I did my thing. You can check it out if you like:
https://github.com/Triun/laravel-model-base
It will read your database, and create the laravel eloquent models for you.
It is meant to be very flexible, so the configuration may be a little complex, and I guess that I didnt' catch up with the documentation, but you can ask me if you don't know how to make it do what you want.
Basically it has 4 customization levels:
By out of the box modificators, configurable by the config files.
Including interfaces and traits to your auto-generated models.
Create your own modificators. Classes where you receive the model skeleton before it is saved, so you can add or remove properties, methods, etc.
Generate the model base, but edit yourself the final model.
And, of course, suggestions and contributions are more than welcome.

Should I continue to rely on code generation to generate my models and CRUD?

As I delve a little deeper into Yii I'm now wondering if relying on Gii and Giix to generate my models and "admin" CRUD may be a crutch rather than a time-saving tool. Many times in the beginning stages of small projects it helps me get going more quickly, allowing me to focus on database design. However, whenever I make a change to my table structure or relations, I find myself having to rely on GiiX to re-generate the model. Before I do so, I always copy the parts of the model that I have written so that I can paste it into the updated model later. This seems like a tedious thing to do and I'm now wondering if it is saving me any actual time. I have a few questions:
For Yii users specifically, once you've been doing Yii for a while do you even bother with Gii or GiiX? Did you quit using it because it was no longer useful, or because it was a crutch? Did you work on writing your own code generation and scaffolding tools?
For all coders, do you feel code generation tools should be avoided when learning a new language or framework?
My hope is that there is an effective way to use Gii and other code generation tools even after updating table structure multiple times and writing in my own code, sans the copying and pasting and keeping track of what's what.
Please let me know your thoughts!
Gii is useful to generate the initial boilerplate code and directory structure.
As the project go ahead, I use the diffs provided by Gii to add the relevant new code snippets in my model class files. Say you modify a table. Go to Gii and try to generate the model. You will get notified that the model class file exists. Also, you will see the link that gives you the diff in a pop-up.
I don't know if it's possible with Yii but with another framework that I use we extend the model classes and put our custom code into those extended classes. In the app we only reference the extended class, not the base (generated) model classes.
Since we don't put any custom code into the base model classes they can be re-generated without worrying about overwriting any custom code.
However, whenever I make a change to my table structure or relations,
I find myself having to rely on GiiX to re-generate the model.
You really do not need that. Yii design makes all of your table fields available as attributes in your model. This way, if you add a new fieldX to your TableA, you can imediatelly use $modelA->fieldX. You do not need make any upgrade in your model. Yii 'knows' you have changed the table.
See:
"Although we never explicitly declare the title property in the Post
class, we can still access it in the above code. This is because title
is a column in the tbl_post table, and CActiveRecord makes it
accessible as a property with the help of the PHP __get() magic
method. An exception will be thrown if we attempt to access a
non-existing column in the same way."
Source: http://www.yiiframework.com/doc/guide/1.1/en/database.ar
For Yii users specifically, once you've been doing Yii for a while do you even bother with Gii or GiiX? Did you quit using it because it was no longer useful, or because it was a crutch? Did you work on writing your own code generation and scaffolding tools?
I use Gii in all of my projects for the most of models or CRUD generation. It is very useful. I can customize the generated code the way i want. I even have done some customizations to 'skeleton' of Gii generator so that the code generated to be in my language, not english, and with some methods/attributes I need more.
For all coders, do you feel code generation tools should be avoided when learning a new language or framework?
No, IMO. The generated code is one more way to learn.

Better use of models and migration in symfony

Hey.
I'm having a hard time migrating changes I've done i my config/doctrine/schema.yml file.
I added the column age to the user table. Then I did a php symfony doctrine:generate-migrations-diff followed by php symfony doctrine:migrate .
Looking in my database, the column age is now added, without deleting any data.
But, my /lib/model/doctrine/base/BaseUser.class.php is not changed, there is no age field or functions for age . So I also did the command php symfony doctrine:build-model . Finally the model is updated/migrated too.
So I wonder, is this the only way? Seems like a lot of work, and I'm afraid to miss something each time doing it.
Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).
Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?
This might be a bad question, but why is the model layer inside the /lib/doctrine path? As far as I have learned, you keep modules inside apps, where you create your view and controller. Why should the model be outside. Like this I can make models without attached controller and view?
Thanks.
Why should the model be outside
Because models can be used everywhere in your project, in example, in different applications and modules.
Could I go right into phpmyadmin, add changes in the database there and just do a php symfony doctrine:build-schema , and like that skip the migration part (two commands).
Of course you can, but migrations are a good approach to track your schema when deploying to production or working in team.
Here how I use doctrine migrations (simple use-case):
Add a column age to my User model in schema.yml
./symfony doctrine:generate-migrations-diff. Migration class(-es) have been generated.
./symfony doctrine:migrate. Column age successfully added to table.
./symfony doctrine:build --all-classes. Build forms/filters/models
That's it. The main idea is that doctrine:generate-migrations-diff class:
Gathers information about all your models' structure (php-representation of schema.yml)
Compares your schema.yml and info from (1)
Generates migration classes based on difference
Also when the comes to use of models, am I right that /lib/model/doctrine/User.class.php is where I can make functions and such for my User "data class"? Like, making a function isFemale . If not, where would that kind of function be?
Yes, you can add such method to User model because it's about users.

Categories