OctoberCMS Rainlab.Builder- Error on "hasOne" relation to same model - laravel

I'm developing an October CMS Plugin for managing animals (with rainlab.builder).
Animals have a couple of fields and relations. Every animal have a father and a mother animal. But when I try to safe my animal the following error appears:
Event Log:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'id' cannot be null in dir/website/vendor/laravel/framework/src/
Illuminate/Database/Connection.php:413
Plugin animal form:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id'
cannot be null (SQL: update `prefix_animals_animal` set `id` = where
`prefix_animals_animal`.`id` = 1 and `prefix_animals_animal`.`id` is
not null)" on line 666 of dir/website/vendor/laravel/framework/
src/Illuminate/Database/Connection.php
The error only appers when I use the relations (child -> father, child -> mother).
Code
I have implemented the following hasOne - relations to my animal model:
/* Relation */
public $hasOne = [
'father' => [
'Namespace\Animals\Models\Animal',
'key' => 'id',
'otherKey' => 'id'
],
'mother' => [
'Namespace\Animals\Models\Animal',
'key' => 'id',
'otherKey' => 'id'
]
];
This are my fields from field.yaml:
father:
label: Father
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
emptyOption: 'No father'
span: left
type: relation
mother:
label: Mother
span: right
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
emptyOption: 'No mother'
type: relation
I would be very happy if someone have a solution for these kind of relations.
Cheerio!

Don't use id as the primary key for the relation as this is not good practice.
Actually this creates the problem you are seeing also. Currently your model is using the id for primary Id, mother Id and father Id. You can't do that.
Add mother_id and father_id to the Animal model and change the relation definition to this:
public $hasOne = [
'father' => [
'Namespace\Animals\Models\Animal',
'key' => 'father_id',
'otherKey' => 'id'
],
'mother' => [
'Namespace\Animals\Models\Animal',
'key' => 'mother_id',
'otherKey' => 'id'
]
];
PS. In your case you dont have to define key and otherKey as the default value for otherKey is "id" and the otherKey value is created from the relation name with the "_id" suffix.

Related

Having multiple columns (foreign keys) of the same n-n relation in Backpackforlaravel

I'm struggling with the following situation:
My entities Session & Registration are related with a many-to-many (n-n) relationship. In Registration, I have two foreign keys, player_id and hotel_id.
In the Session CrudController, I want to display the related name(s) of the Player(s) and Hotel(s).
For Player, it worked like this:
CRUD::addColumn([
// any type of relationship
'name' => 'registrations', // name of relationship method in the model
'type' => 'relationship',
'label' => 'Spieler', // Table column heading
// OPTIONAL
'entity' => 'registrations', // the method that defines the relationship in your Model
'attribute' => 'player.full_name', // foreign key attribute that is shown to user
'model' => App\Models\Registration::class, // foreign key model
]);
So I've continued to add the Hotel like this:
CRUD::addColumn([
// any type of relationship
'name' => 'registrations', // name of relationship method in the model
'type' => 'relationship',
'label' => 'Hotel', // Table column heading
// OPTIONAL
'entity' => 'registrations', // the method that defines the relationship in your Model
'attribute' => 'hotel.name', // foreign key attribute that is shown to user
'model' => App\Models\Registration::class, // foreign key model
]);
But unfortunately it doesn't work - I don't get an error message, it just keeps showing only the Player column. I think the reason for this is the same value in 'name' - because after commenting the 'name' value in Player, the Hotel information was visible. But I have no idea how to avoid it.
Currently I'm not sure if it's a bug or if I'm doing anything wrong. Would appreciate any kind of support - thanks in advance!
thank you #tabacitu, that works!
I've added
'key' => 'hotel'
and it showed up in the table.

Adding Columns with nested relationships in backpack for laravel

I have 3 models created in backpack for laravel CRUD:
Driver, Municipality and Province.
Driver has a 'municipality_id' and the relationship function:
public function municipality()
{
return $this->belongsTo('App\Models\Municipality');
}
To get the province of the driver i need to use a relationship function inside Municipality model:
public function province()
{
return $this->belongsTo('App\Models\Province');
}
The driver model does not contain the province function so i have to to something like
$objdriver->municipality->province->name;
To get the province name.
Now the problem is that i have to add 2 columns in the table created by artisan that display the municipality name and the province name.
For the municipality it's easy and i did it like this:
$this->crud->addColumn('municipality_id', [
// 1-n relationship
'label' => "Comune", // Table column heading
'type' => "select",
'name' => 'municipality_id', // the column that contains the ID of that connected entity;
'entity' => 'municipality', // the method that defines the relationship in your Model
'attribute' => "name", // foreign key attribute that is shown to user
'model' => "App\Models\Municipality", // foreign key model
]);
And it works Fine, but how do i add a column for the Provinces? Since the method is not in the driver model i can't use the same approach.
I already tried using a custom function to fetch what i need from the province table, something like:
$this->crud->addColumn('province_id', [
// run a function on the CRUD model and show its return value
'name' => "province_id",
'label' => "Provincia", // Table column heading
'type' => "model_function",
'function_name' => 'GetProvinceName', // the method in your Model
])
where GetProvinceName is:
public function GetProvinceName()
{
return $this->municipality->province->name;
}
but that just gives out an error.
You could try the following code.
$this->crud->addColumn([
'name' => 'municipality.province.name', // the relationships are handled automatically
'label' => 'Provincia', // the grid's column heading
'type' => 'text'
]);

OctoberCMS plugin record update server internal error

Internal Server error on update record with hasOne relation field in builder tool of octoberCMS
In my database parent_id is foreign key and i have use same name for label too for the relation, so i have change label from parent_id to parent, what i understand it cause infinite recursion of query because of same name of label and foreign key "parent_id" which cause server error 500,
it have resolve my issue
public $belongsTo = [ "parent_id" =>
[
"Stuff\Profession\Models\Profession_list",
"table" => "stuff_profession_list", "key"=>"parent_id" ]
];
to
public $belongsTo = [ "parent" => [ "Stuff\Profession\Models\Profession_list", "table" => "stuff_profession_list", "key"=>"parent_id" ] ];

Laravel 5.4 Duplicate entry for key 'PRIMARY' on sync()

I have Playlist and Track Many To Many relationship with additional order field:
Schema::create('playlist_track', function (Blueprint $table) {
$table->integer('playlist_id')->unsigned();
$table->integer('track_id')->unsigned();
$table->integer('order')->unsigned();
$table->primary(['playlist_id', 'order']);
});
User can delete tracks from playlists and change an order. So this sync method is what I need:
foreach ( $tracks as $key => $track ){
$_tracks[ $track ] = [ 'order' => $key ];
}
$playlist->tracks()->sync( $_tracks );
But I get an error when trying to change an ordering of tracks:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '3-0' for key 'PRIMARY' (SQL: update `playlist_track` set `order` = 0 where `playlist_id` = 3 and `track_id` = 1)
That's because i'm using composite primary key I guess... But I don't know how to make it works
UPD
This is dd( $_tracks )
array:2 [
1 => array:1 [
"order" => 0
]
3 => array:1 [
"order" => 1
]
]
Since it's a pivot table, you can't define foreign key as primary, so remove this:
$table->primary(['playlist_id', 'order'])
Pivot table can have non unique playlist_id values, but with ->primary() you're creating a constraint.
Use an auto-increment, integer column as a primary key:
$table->increments('id');

How to add multi select value in database in October CMS Backend?

Not inserted multi select value in database.
My fields.yaml Code is :
related_recipe:
label: 'Related Recipe'
span: auto
nameFrom: recipe_title
descriptionFrom: description
attributes: {multiple:'multiple'}
type: relation
My model code is :
public $belongsTo = [
'related_recipe' => [
'Qdata\Taeq\Models\Recipe',
'conditions' => 'status = 1'
],
];
Currently only one selected value inserted in database.need add multiple value in database. Can any one have the solution of it ?
You should use a $belongsToMany relation in this case.
$belongsTo means your model is link with a single entity of your Recipe model.
To do so with relation you need to go with "belongsToMany" relation. for example:
In your Model
'related_recipes' => [
'Qdata\Taeq\Models\Recipe',
'table' => 'pivot_table_name',
'key' => 'foreign_key_of_pivot_table',
'otherKey' => 'other_key',
],
In related another Model
'makers' => [
'Qdata\Taeq\Models\AnotherModel',
'table' => 'pivot_table_name',
'key' => 'foreign_key_of_pivot_table',
'otherKey' => 'other_key',
],
this will save your multiselect dropdown data in the related pivot table in your database.

Resources