6.5.1. A Tool for Model-making
To create a model for our customer entity, Laravel offers the artisan
command that makes it relatively easy. This is the command for creating a model template:
php artisan make:model Customer
We want to change the model so that it looks like this:
namespace App;
use Firebird\Eloquent\Model;
class Customer extends Model
{
/**
* Table associated with the model
*
* @var string
*/
protected $table = 'CUSTOMER';
/**
* Primary key of the model
*
* @var string
*/
protected $primaryKey = 'CUSTOMER_ID';
/**
* Our model does not have a timestamp
*
* @var bool
*/
public $timestamps = false;
/**
* The name of the sequence for generating the primary key
*
* @var string
*/
protected $sequence = 'GEN_CUSTOMER_ID';
}
Notice that we use the modified Firebird\Eloquent\Model
model from the sim1984/laravel-firebird
package as the basis. It allows us to use the sequence specified in the $sequence
attribute to generate values for the primary key ID.
We create a model for products — Product
— in the same way.
namespace App;
use Firebird\Eloquent\Model;
class Product extends Model
{
/**
* Table associated with the model
*
* @var string
*/
protected $table = 'PRODUCT';
/**
* Primary key of the model
*
* @var string
*/
protected $primaryKey = 'PRODUCT_ID';
/**
* Our model does not have a timestamp
*
* @var bool
*/
public $timestamps = false;
/**
* The name of the sequence for generating the primary key
*
* @var string
*/
protected $sequence = 'GEN_PRODUCT_ID';
}
Now, a model for the invoice header:
namespace App;
use Firebird\Eloquent\Model;
class Invoice extends Model {
/**
* Table associated with the model
*
* @var string
*/
protected $table = 'INVOICE';
/**
* Primary key of the model
*
* @var string
*/
protected $primaryKey = 'INVOICE_ID';
/**
* Our model does not have a timestamp
*
* @var bool
*/
public $timestamps = false;
/**
* The name of the sequence for generating the primary key
*
* @var string
*/
protected $sequence = 'GEN_INVOICE_ID';
/**
* Customer
*
* @return \App\Customer
*/
public function customer() {
return $this->belongsTo('App\Customer', 'CUSTOMER_ID');
}
/**
* Invoice lines
* @return \App\InvoiceLine[]
*/
public function lines() {
return $this->hasMany('App\InvoiceLine', 'INVOICE_ID');
}
/**
* Payed
*/
public function pay() {
$connection = $this->getConnection();
$attributes = $this->attributes;
$connection->executeProcedure('SP_PAY_FOR_INOVICE',
[$attributes['INVOICE_ID']]);
}
}
You’ll observe some additional functions in this model. The customer
function returns the customer that relates to the invoice header via the CUSTOMER_ID
field. The belongsTo
method is used for establishing this relation. The name of the model class and the name of the relation field are passed to this method.
The function lines
returns items from the invoice that are represented by a collection of InvoiceLine
models, described later. To establish the one-to-many relation in the lines
function, the name of the class model and the relation field are passed to the hasMany
method.
You can find more details about specifying relations between entities in the Relationships section of the Laravel documentation.
The pay
function performs payment of an invoice by calling the stored procedure SP_PAY_FOR_INVOICE
, passing the identifier of the invoice header. The value of any field (model attribute) can be obtained from the attribute attribute
. The executeProcedure
method calls the stored procedure.
This method is available only when the |