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" ] ];
Related
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.
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');
I have a very simply model which I want to paginate. The pagination works but the sort links do not have any effect:
My Controller:
public $paginate = [
'limit' => 10,
'order' => [
'Properties.id' => 'asc'
],
'sortWhitelist' => [
'Properties.id',
'Properties.name',
'Properties.active'
],
];
My query:
$properties = $this->Properties->find('all')->where($options)->contain($contains)->order(['Properties.id']);
$this->set('properties', $this->paginate($properties));
My view displays 10 items per page and the links to pages/next/previous work fine. When I click the sort link:
$this->Paginator->sort('id', 'ID')
The url called is:
properties/index/3?sort=id&direction=desc
The page re-loads but the order of the data does not change.
The whitelist expects the exact field name that you are using in sort. It will not automatically prepend the primary table's name, so your whitelist should look like this:
public $paginate = [
'limit' => 10,
'order' => [
'Properties.id' => 'asc'
],
'sortWhitelist' => [
'id',
'name',
'active'
],
];
It will see "id" in the query and check that the exact field name "id" exists in the whitelist.
Also, it appears you are including an order() on your query. To allow the paginator to set the order, you should remove this clause.
Can I add a part of the fields of an table to the index of algolia?
For example:
There is a table articles,it's fileds like this:
id title content author status created_at updated_at deleted_at
I only want to add the 4 fileds id title content author to the index of algolia,
I read the docs of Scout in Laravel 5.3,it seems not to be mentioned.
You need to implement the toSearchableArray method on your model.
public function toSearchableArray()
{
return [
'objectID' => $this->id,
'title' => $this->title,
'content' => $this->content,
'author' => $this->author,
]
}
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.