...or I really need to create a class for each Table? then.. everytime i changes table structure i need to update the code..
You could use an ORM (Object-relational mapper) such as Eloquent, which is included in Laravel, and then just create an model (class) for each table in your database. Eloquent automatically maps each field into a PHP object. If you haven't ever used an ORM, I highly recommend you check out Laravel... it's what made me stick with PHP and I do almost all my projects using Laravel. Best of luck!
Adding to BakerStreet Response.
Eloquent fits your needs as the ORM itself will fetch all the columns you specify if you leave it as default. By default the drivers that it works with are: mysql, postgreSQL, and Sqlite.
Eloquent itself can be downloaded without Laravel being involved.
Please refer to Jeffrey Way's Laracast for instruction:
https://laracasts.com/lessons/how-to-use-eloquent-outside-of-laravel
...thanks all, ended up writting my own magic class:
DBIntrd - Simple PHP framework for SQLite3 databases
Tired of spending time writting a bunch of code to create PHP classes & methods for SQLite tables?
DBIntrd is magic way to dinamically instance objects and persists data at SQLite3 tables..
Related
I'm getting started with PHP and I wanted to know if there is a way that you can extract data from the DB in a model.
If I have a Users table in my DB, I'm looking for something like Users.all in rails that will use the model to extract all the entires in the Database.
Does PHP offer that type of functionality?
The closest to Rails is Laravel. Laravel's database layer Eloquent was already mentioned in the comment. Eloquent: Getting Started
If you go for an ORM instead of Eloquent's active record pattern Doctrine is the most popular project. Doctrine Website
Pure php doesn't provide an ORM out of the box ( I suppose you can install a package or something), but if you are using Laravel (since the tag in your question), you need to create a model (supposing you have a migration for users_table)
php artisan make:model user
then simply 'use' the model in any controller
use App\User
and get all users :
$users = User::all();
For a project at work I'm creating an API in Laravel. I wanted to use MongoDB as database driver which is new to me. So I want to use Eloquent with MySQL in the beginning and when I'm confident enough switch to MongoDB.
I was reading some tutorials about the repository pattern in Laravel and saw that some returned an eloquent model, like this one. It seems to me that when returning a model on for example create($data), you're limited to database drivers which are suitable with Eloquent. I was wondering if it is a good practice to return an eloquent model. Because if I want to use MySQL for now and in the future MongoDB (without the Laravel MongoDB package), I need to rewrite some code in the controllers because the use the eloquent models instead of an array (for example). Maybe a 'wrapper' between the model and your code is a possibility? Any good recommendations, tips or thoughts on this?
If you're wondering why I want to use MySQL for now and later switch to MongoDB, the reason is there is a time limit on the project. I'm still learning Mongo. So to fulfil the needs of my employer, I'm not using MongoDB until finishing the first version / prototype.
If you use eloquent and its methods, you should be good. Just do not use the DB::raw() method as it may break your query if it does not match the DB engine you are currently in.
I wrote a repository pattern article if you want to look at it (with tdd).
But if you really want future proof, you can mix Doctrine w Laravel since Doctrine is a Data Mapper Pattern. Link
With laravel-doctrine package, you persist the data in your db engine that you prefer.
Is something like laravel-mongodb good?
Extending Eloquent to use MongoDB instead of MySQL.
My question is more theoritical, as I am not quite sure if it is a better way to create a model in Laravel for each table on database, if yes, what would be the benefition of it?
I am using Laravel 4 Eloquent for the ORM.
Thanks
The basic answer is yes, you should have a model for each table.
But the long answer is "it depends". As for what "depends" is, it is something that comes with experience and also your design criteria. There is no 100% right answer that can be used everytime.
As a principle if you plan on accessing data from tables using Eloquent, then you generally need one eloquent model per table, so you can access the table using Eloquent functions.
As a principle you dont need a model if you never use Eloquent to access the data. i.e. perhaps you have a table that you only use the query builder on.
What I'm trying to figure out is how to add new fields to a table, using Symfony2 with Doctrine2.
I used this to initially create the Entity:
php app/console doctrine:generate:entity --entity="MyMainBundle:ImagesTable" --fields="title:string(100) file:string(100)"
And I used this to create/update the tables on the database:
php app/console doctrine:schema:update --force
Now if I wanted to add new fields to the ImagesTable entity, is there an easy way to do it using the console, or do I have to manually edit the entity. I am just using 1 entity as an example right now, but in reality, there are many entities I'd be changing; so, there has to be an easier way to do it.
I've been manually editing them to create relationships, so if there is an easier way to do that as well, that'd be great.
I remember this being a lot easier with Symfony1.4 - all I had to do was create the database/tables using phpMyAdmin, and Symfony was able to generate the models with no issues.
I really hope I'm missing something here, because this won't work if I have to manually edit every entity for every change.
Doctrine generator commands are intended to help the developer to quickly prototype an idea. They generally don't produce production ready code, and the code needs to be checked to see if it contains what you want.
You can still create your model in phpmyadmin and use Doctrine reverse engineering tools, but it also doesn't produce production ready code, only intended to use in prototyping.
Creating database/tables beforehand doesn't really work well with Doctrine2, as the underlying relation between tables may not be the same as the relation between objects of your model. The whole point of ORM is to think in classes and letting Doctrine do the rest of the work for you.
Doctrine is not intended to write your entities for you, it gives you tools to build your data model, which you use to code your model in Php.
If you don't like to code your entities by hand (which is what all developers using doctrine does), you may want to have a look at RedbeanPHP, a zero-config ORM framework for PHP. It creates the database tables, columns, indexes on the fly depending on the data model you use.
Is it somehow possible to automatically generate a YAML schema file or models from an existing MySQL database?
I need to create models for Doctrine but writing the model classes manually seems extremely boring to me. I already have MySQL database with tables and all relations so it would help me if there is some way to generate Doctrine models from it.
If you are using doctrine 2:
http://www.doctrine-project.org/docs/orm/2.0/en/reference/tools.html#reverse-engineering
Yes, it is possible ;-)
For Doctrine 1.2, take a look at the Command Line Interface : amongst other utilities, you have the possibity to generate the YAML files from an existing database.
And, for Doctrine 2.0, you'll want to take a look at Reverse Engineering