recordable_id as string
Bug Report
Q | A |
---|---|
PHP version | 8.0.1 |
Package version | 3.0.2 |
Framework | Lumen |
Framework version | 8.2.1 |
Actual Behaviour
I'm using Postgre. I have tables which have primary key as an int and tables which have varchar as primary key. To be able to record in Accountant I created "recordable_id" column as a string. It works to save the logs. Right now I'm trying to get ledgers for a model ($transferenciaEstoque->ledgers()->with('user')->get()) and getting an error because the model primary key is an int.
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer LINE 1: ...auditoria"."historico_alteracoes"."recordable_id" in (19) an... ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts. (SQL: select * from "auditoria"."historico_alteracoes" where "auditoria"."historico_alteracoes"."recordable_id" in (19) and "auditoria"."historico_alteracoes"."recordable_type" = App\Models\Estoque\TransferenciaEstoque limit 1)
Expected Behaviour
No error, auto cast according to model $keyType
Relevant information, logs and/or screenshots
Accountant config: `<?php
declare (strict_types = 1);
use App\Models\Auditoria\HistoricoAlteracao;
return [
'ledger' => [
/*
|--------------------------------------------------------------------------
| Ledger Implementation
|--------------------------------------------------------------------------
|
| Define the Ledger implementation.
|
*/
'implementation' => HistoricoAlteracao::class,
/*
|--------------------------------------------------------------------------
| Ledger Threshold
|--------------------------------------------------------------------------
|
| Specify a cutoff for the number of Ledger records a model can have.
| Zero means unlimited.
|
*/
'threshold' => 0,
/*
|--------------------------------------------------------------------------
| Ledger Driver
|--------------------------------------------------------------------------
|
| The default driver used to store Ledger records.
|
*/
'driver' => 'database',
/*
|--------------------------------------------------------------------------
| Ledger Date Serialisation Format
|--------------------------------------------------------------------------
|
| The default date serialisation format to be used by the model.
|
*/
'date_format' => null,
],
/*
|--------------------------------------------------------------------------
| Resolver Implementations
|--------------------------------------------------------------------------
|
| Define the Context, IP Address, URL, User Agent and User resolver
| implementations.
|
*/
'resolvers' => [
'context' => Altek\Accountant\Resolvers\ContextResolver::class,
'ip_address' => Altek\Accountant\Resolvers\IpAddressResolver::class,
'url' => Altek\Accountant\Resolvers\UrlResolver::class,
'user_agent' => Altek\Accountant\Resolvers\UserAgentResolver::class,
'user' => Altek\Accountant\Resolvers\UserResolver::class,
],
/*
|--------------------------------------------------------------------------
| Recording contexts
|--------------------------------------------------------------------------
|
| Define the contexts in which recording should take place.
|
*/
'contexts' => Altek\Accountant\Context::WEB,
/*
|--------------------------------------------------------------------------
| Notary Implementation
|--------------------------------------------------------------------------
|
| The default Notary implementation.
|
*/
'notary' => Altek\Accountant\Notary::class,
/*
|--------------------------------------------------------------------------
| Recordable Events
|--------------------------------------------------------------------------
|
| The events that trigger a new Ledger record.
|
*/
'events' => [
'created',
'updated',
'restored',
'deleted',
'forceDeleted',
'existingPivotUpdated',
'attached',
'detached',
'synced',
],
/*
|--------------------------------------------------------------------------
| User MorphTo relation prefix & default Guards
|--------------------------------------------------------------------------
|
| Define the morph prefix and which authentication guards the User resolver
| should use.
|
*/
'user' => [
'prefix' => 'user',
'guards' => [
'api',
],
],
]; `
Ledger:
<?php
namespace App\Models\Auditoria;
use Illuminate\Database\Eloquent\Model;
class HistoricoAlteracao extends Model implements \Altek\Accountant\Contracts\Ledger
{
use \Altek\Accountant\Ledger;
protected $table = 'auditoria.historico_alteracoes';
protected $casts = [
'properties' => 'json',
'modified' => 'json',
'pivot' => 'json',
'extra' => 'json',
];
}
Migration to create table:
Schema::create('auditoria.historico_alteracoes', static function (Blueprint $table): void {
$table->increments('id');
$table->string('user_type')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('recordable_id');
$table->string('recordable_type');
$table->unsignedTinyInteger('context');
$table->string('event');
$table->json('properties');
$table->json('modified');
$table->json('pivot');
$table->json('extra');
$table->text('url')->nullable();
$table->ipAddress('ip_address')->nullable();
$table->string('user_agent')->nullable();
$table->string('signature');
$table->timestamps();
$table->index([
'user_id',
'user_type',
'recordable_id',
'recordable_type',
]);
});
Is there a way to overcome this?