you know that using gii tool we can generate models and view controllers, so, the question is :
Is it possible to generate a model without using the gii tool, I mean, just doing it through the php code like calling some method giving required parameters to it and so on?
If you want to create a model called company, create a file named Company.php in models folder and add the following
namespace app\models;
use yii\db\ActiveRecord;
class Company extends ActiveRecord {
// add your functions and properties here
}
N.B. : This is for the latest version of Yii, ie Yii 2.0
Related
Background:
I am having my first encounter with any MVC framework, as I am developing a little application with PHP web framework Laravel v6 and MongoDB (with jenssegers moloquent) as database engine. I am following this tutorial series to learn Laravel 6.
Normally when I used to develop such applications with simple php, I would create one file called readFromDb.php and in it I would find/read/select data from all DB tables (collections in Mongodb). Then I would include in on top of every PHP file in which I would need to do some processing on any data from DB.
For example, if I have the following collections
allPaintingsCollection
paintingHistoriesCollection
paintingCategoriesCollection
artGalleriesCollection
paintingArtistsCollection
supervisorArtistsCollection
smPlatformsCollection
nonSmPlatformsCollection
targetSchoolsCollection
I would select all records/documents from them into associative arrays in the readFromDb.php, and then include readFromDb.php on top of every page where I would need to display or do processing on data from DB.
Question:
Now, in Laravel, should I create such a script called readFromDb.php and include it on top of every function in every single controller? In that case, where should I put this readFromDb.php file, and how do I include it in controllers?
Or should I write code to read from relevant DB collection/table in each function in every controller, before using that data from DB?
I am using class and call it as repository to handle all my request through database
First i create an file UserRepository
<?php
namespace App\Repositories;
use App\Model\User;
class UserRepository
{
function getUsers(){
Return User::get();
}
}
Then in Controller, you just need to call the repository in the parent of contruct
<?php
namespace App\Http\Controllers;
use App\Repositories\UserRepository;
class UserController extends Controller
{
protected $user;
function __construct(){
$this->user = new UserRepository();
}
function index(){
$users = $this->user->getUser(); // getUser() is the function inside the repository class
return view('user',compact('users'));
}
}
That's how I do code in laravel, because i dont want to call Laravel Model and do all the store, reads inside Controller and cause messy code
Laravel has Eloquent ORM what presents Object Relation Mapping interface.
In Introduction:
The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.
Basically, same as your readFromDb.php file but it handles with OOP so codebase is very understandable and expandable and strictly related with your code.
When querying with Eloquent you use Models. If your query result has single row you get single Model, if your result has multiple rows you get Eloquent\Collection result set contain your models.
Finally, you mustn't write ORM in Laravel because you already have.
If you think high level abstract accessors; you should explore design patterns. After that you can think to Repository Pattern, Decorator Pattern etc.
I'm creating an app using laravel. Tables and columns will be created on the frontend applying the schema codes inside the controller. But how can I do to create models without programming each one manually? Any idea to code it?
Controller example (is working fine):
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class teste extends Controller
{
public function CreateTable(Request $tableName)
{
Schema::create($tableName, function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
//return view
}
}
You can use this to create models from your controller.
Artisan::call('make:model ' + $modelPath + '/' + $request->modelName)
Now there are a lot of options to create migrations, controller, resource routes etc. along with the model. You can check them out if you want.
The command will create the model. You can create the model from the table name of course. But it will not follow the naming conventions. Like table names are in plural and model name are in singular form with the first letter in upper case.
If you had used artisan to create the table migration along with model Laravel would've handled some complex namings. Like Category model with categories table, or Software model with software table. That's why I recommend sending the model name separately from your view.
The real problem arises when you plan to build a whole model programmatically along with the fields. Laravel does not allow you to do that. You would have to build a custom command to handle this.
Also, even if you make the fields programmatically, you'd still have to go back to the model to define the relations. You can build your custom command to handle this too.
However, you can go through this question to have some idea. This guy asked for a similar thing like you.
Also, there are some packages that read your table and backtrack it to build your model. Here's an example. Another example here.
Note: if you plan on building your custom command and make it open-source please let me know.
In Laravel 4 I want to use a model that represents an Event coming from my database. Thus, in app/models I have my Event model that extends Eloquent.
However, Laravel 4 already has an Event class which is used to manage events within the application lifecycle.
What I want to know is, how can I properly namespace my Event model and access it in a way that will not clash with the existing Event class.
You just need to apply a namespace to it as you normally would. So, for example.
<?php namespace Models;
use Eloquent;
class Event extends Eloquent {
}
You should then correctly setup your composer.json so that it loads your models. You could use classmap or psr-0 for this, depending on whether or not you're following PSR-0 with your directory structure.
I'm pretty sure the models directory is already mapped.
Edit
As mentioned in the comments you must run composer dump-autoload.
As much i have research about using namespaces, because i want to use those inside the PHPDocumentar, its not possible to add twice the namespaces, as laravel already adds those for you, if we understand the basic problem, why we use namespaces
1 - many libraries uses same class name, so we use namespaces to differentiate that.
2 - if you want to use namespaces then you should be out of the scope of the app, means is the vendor folder for you to this kind of stuff.
Enjoy
I'm new to Kohana and I'm trying to build an application using the ORM module. I created my own user module containing stuff like login, account creating etc. The problem however is that I can't seem to create a user model in my own user module extending the ORM one. If I understand kohana correctly I should name my user model: Model_User. The problem is, the ORM model I'm trying to extend is also called Model_User.
The reason I'm trying to create my own model is so I can add some extra methods and checks without modifying with the ORM user model.
So my question is:
How do I create a user model in my own module that extends the ORM user model?
You can extend pretty much everything in Kohana via the transparent class extensions: http://kohanaframework.org/3.2/guide/kohana/extension
In this case, the default ORM Model_User class is an empty (transparent) class that extends Model_Auth_User.
So if you want to add new methods to this model, just create a new Model_User model class that extends Model_Auth_User, in your module.
Due to Kohana's cascading file system, the model stored in your module will be used. http://kohanaframework.org/3.2/guide/kohana/files
[edit] Important to note, the order in which you enable the modules is important, see http://kohanaframework.org/3.2/guide/kohana/modules
I have to models that are practically the same, just diferent table in db and a new field,
So is there a way in zend framework so I can inhert from the first class? and just change these two things?
The same for the controlles and view.
You can extends those classes like any other php class by using the "extend" call.
class Application_Model_Foo extends Application_Model_Bar
the same goes for controllers, but not views, as those are not classes, just php files, but You could use render method inside one view to render another view.