Laravel vs Symfony | Model Attributes - php

I'm actually discovering Laravel (I've used symfony for years) and I face a situation where I don't know if I just couldn't find the right information or it is just the way to do in Laravel.
When creating a model in Symfony you put attributes in your model class. Actually these attributes represent the different columns of your table for the ORM.
Now in Laravel I see all people don't put theses attributes in the model class but in the migrations files. So a new developer who should contribute in a new project will have to look on database or migrations files. Which in my opinion is not the rule of an ORM : "let dev think class and not table"
Could somebody, please, put a light on this point?
Thanks

If you use PHPStorm (but it can work with other IDEs as well) there is an interesting composer package that can generate model documentation for you, in order to have autocompletion for attributes ad methods based on your migrations:
https://github.com/barryvdh/laravel-ide-helper
its three main methods:
php artisan ide-helper:generate creates phpDoc for Facades
php artisan ide-helper:model creates phpDoc for your model (you should relaunch it after every new migration)
php artisan ide-helper:meta creates phpstorm meta file
so, if you launch php artisan ide-helper:model it will add something similar to this
/**
* App\User
*
* #property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
* #property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[] $roles
* #method static \Illuminate\Database\Eloquent\Builder|\App\User permission($permissions)
* #method static \Illuminate\Database\Eloquent\Builder|\App\User role($roles, $guard = null)
* #property int $id
* #property string $name
* #property string $email
* #property \Illuminate\Support\Carbon|null $email_verified_at
* #property string $password
* #property string|null $remember_token
* #property \Illuminate\Support\Carbon|null $created_at
* #property \Illuminate\Support\Carbon|null $updated_at
* #method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreatedAt($value)
* #method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmail($value)
* #method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmailVerifiedAt($value)
* #method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
* #method static \Illuminate\Database\Eloquent\Builder|\App\User whereName($value)
* #method static \Illuminate\Database\Eloquent\Builder|\App\User wherePassword($value)
* #method static \Illuminate\Database\Eloquent\Builder|\App\User whereRememberToken($value)
* #method static \Illuminate\Database\Eloquent\Builder|\App\User whereUpdatedAt($value)
* [...]
*/
So you have a phpDoc
and your IDE should be able to autocomplete your code like this:

Related

CakePHP: How to update the Models PHPDocs?

I have a huge project with 100+ tables in my database. Over the years, we add some new columns in some tables, but we forgot to reflect this on Models phpDocs. Those phpdocs were initially generate by cake bake model in the beginning of our project.
Example:
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
/**
* Pessoa Entity
*
* #property int $id
* #property string $numero_documento_principal
* #property string $nome
* #property string $tipo_pessoa
* #property string $cidade_natural
* #property string $nacionalidade
* #property string $estado_natural
* #property \Cake\I18n\Time $data_obito
* #property string $sexo
* #property string $nome_genitor
* #property string $nome_genitora
* #property \Cake\I18n\Time $data_nascimento
*
* #property \App\Model\Entity\EnderecoPessoa[] $endereco_pessoa
* #property \App\Model\Entity\Parte[] $parte
*/
class Pessoa extends Entity {}
Those docs are very important nowadays thanks auto-complete features used in VSCode and similars IDEs.
The problem is: if we try to generate by bake again, all custom code will be replaced by the original bake generated one.
Is there a away to avoid this and update only the phpDocs part?
Put your code in a version control system and revert the changes that you do not need after baking, or use dereuromark/cakephp-ide-helper, which can update annotations.
Alternatively check if the upcoming (partially) non-destructive baking functionality works for your needs.

How could this Laravel Model code was generated?

I have an old Laravel app laravel-5.4, I review it recently, and I wondered how did that code was generated for the model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* App\Defect
*
* #property int $id
* #property string $code
* #property string $title
* #property bool $defgroup_id
* #method static \Illuminate\Database\Query\Builder|\App\Defect whereCode($value)
* #method static \Illuminate\Database\Query\Builder|\App\Defect whereDefgroupId($value)
* #method static \Illuminate\Database\Query\Builder|\App\Defect whereId($value)
* #method static \Illuminate\Database\Query\Builder|\App\Defect whereTitle($value)
* #mixin \Eloquent
*/
class Defect extends Model
{//....
I'm pretty sure that I could not able to write all property comments there, but I could not remember how did they had written in the model?!
I tried php artisan make:model Defect but I almost get a plain model without any comments nor relations with about five lines of code.
Could any one able to remember me how could that code with comments was generated?
You can install this composer package:
https://github.com/barryvdh/laravel-ide-helper
Once you have that you can run this, which will generate the doc block similar to the one you listed in your example:
https://github.com/barryvdh/laravel-ide-helper#automatic-phpdocs-for-models
The package a bit more then generate the doc block you mentioned, it helps greatly with IDE code completion, I highly recommend it.

What does the #param keyword do in symfony routes

In annotations for a route what does #param mean?
I can not find any documentation on symfony website regarding this keyword and very confused as to what it's for and does.
I'm asking because I'm trying to figure out how to access parameters in my .yml files for use in the routes but I'd like to still use annotations instead of the YML method of routing.
Showing a code example is probably not the best for this but here is where #param shows up
/**
* League action
*
* #Route("/association/{assoc}/{league}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null})
* #Route("/association/{assoc}/{league}/{game}")
* #Template()
*
* #param $assoc
* #param $league
* #param $game
* #return array
*/
I took that from here (Symfony2 route in annotations with optional parameters)
Any help is appreciated, thanks!
#param and #return are phpDocumentor annotations, and do not actually affect routing.

How to declare columns on models?

I want my Code Completion (PhpStorm) to work with Eloquent models but if I declare fields corresponding to DB columns they no longer work:
class Limit extends Model
{
public $id;
public $type;
public $limit;
}
Is there some workaround?
(This is a slightly modified version of my own reply from this thread: Eloquent ORM Code Hinting in PhpStorm)
The laravel-ide-helper package can be used to help with this issue, by generating PHPDocs for your models.
You can generate a separate file for all PHPDocs with this command:
php artisan ide-helper:models
The generated metadata will look something like this for each class:
namespace App {
/**
* App\Post
*
* #property integer $id
* #property integer $author_id
* #property string $title
* #property string $text
* #property \Carbon\Carbon $created_at
* #property \Carbon\Carbon $updated_at
* #property-read \User $author
* #property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments
*/
class Post {}
}
This caused issues for me in PHPStorm however, where the software was complaining about multiple class definitions. Luckily an option is readily available for writing directly to the model files:
php artisan ide-helper:models -W
There are a few more options and settings available if you need to tweak the behavior, but this is the gist of it.

Setup Yii 2 Web Framework Coding Standard in NetBeans

I read this and this. But how to setup this in NetBeans IDE in Windows machine?
Yii2 requires PHP 5.4 minimum, so if your IDE supports PHP 5.4 or later then it will show standards for Yii2 too.
You can also check this yii pligin for netbeans http://plugins.netbeans.org/plugin/47246/php-yii-framework-netbeans-phpcc
in root of your project make file with name of autocompletion.php and add this to file.
/**
* Yii bootstrap file.
* Used for enhanced IDE code autocompletion.
* Note: To avoid "Multiple Implementations" PHPStorm warning and make autocomplete faster
* exclude or "Mark as Plain Text" vendor/yiisoft/yii2/Yii.php file
*/
class Yii extends \yii\BaseYii
{
/**
* #var BaseApplication|WebApplication|ConsoleApplication the application instance
*/
public static $app;
}
/**
* Class BaseApplication
* Used for properties that are identical for both WebApplication and ConsoleApplication
*
* #property trntv\filekit\Storage $fileStorage
* #property common\components\keyStorage\KeyStorage $keyStorage
* #property yii\web\UrlManager $urlManagerFrontend UrlManager for frontend application.
* #property yii\web\UrlManager $urlManagerBackend UrlManager for backend application.
* #property yii\web\UrlManager $urlManagerStorage UrlManager for storage application.
* #property trntv\glide\components\Glide $glide
* #property trntv\bus\CommandBus $commandBus
*/
abstract class BaseApplication extends yii\base\Application
{
}
/**
* Class WebApplication
* Include only Web application related components here
*
* #property User $user User component.
*/
class WebApplication extends yii\web\Application
{
}
/**
* Class ConsoleApplication
* Include only Console application related components here
*/
class ConsoleApplication extends yii\console\Application
{
}
/**
* User component
* Include only Web application related components here
*
* #property \common\models\User $identity User model.
* #method \common\models\User getIdentity() returns User model.
*/
class User extends \yii\web\User
{
}

Categories