This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi, I'm facing what's seems to be an October issue but I'm not sure since it's my first model scope in it.
[EDIT]Even if it's not solved, some search in the october source code says to me it's an actual octobercms issue. It seems to be related to those lines in Relation formwidget
[EDIT²]For those who came here with the same problem, I've made a PR, which could be refused, in this case: I followed an advice from @mjauvin and created a custom FormWidget. You can look at the source code here
I have a HouseModel model, which has a Many To Many relationship with Description.
Then I have a Land model which has a Many To Many relationship with HouseModel, and I want to display in this form a list of HouseModel which have at least one Description, so I created a scope with a join on pivot table to filter them:
public function scopeWithDescription($query)
{
return $query->join('descriptions_house_models', 'house_models.id', '=', 'descriptions_house_models.house_model_id');
} // The tables names are without namespace/plugin name to be more readable
In my Land model, I declared the relationship with the scope parameter:
public $belongsToMany = [
'compatible_house_models' => [
HouseModel::class,
'table' => 'house_models_lands',
'scope' => 'withDescription'
]
] // The table name is without namespace/plugin name to be more readable
When I use Tinker, the scope works perfectly and returns me only two rows:
>>> HouseModel::withDescription()->get();
=> October\Rain\Database\Collection {#3137
all: [
RomainMazB\RealEstate\Models\HouseModel {#3139},
RomainMazB\RealEstate\Models\HouseModel {#3143},
],
}
but in the Land create form, all the HouseModel are displayed!
I said to myself "you are misusing the scope and it's never be called", so I simplified the scope with a basic id > 3
public function scopeWithDescription($query)
{
return $query->where('id', '>', 3);
}
and this works perfectly! So the scope is called, but the join does not work!
I also tried to use the fields.yaml scope parameter and disabled the scope into the hasMany declaration, same result:
compatible_house_models:
label: 'Modèles constructible'
type: relation
scope: withDescription
I'm using the debug bar plugin and the sql query is without any scope in both case (field.yaml and relation with scope):
select * from `romainmazb_realestate_house_models` order by `name` asc
When used with the basic scope id > 3, the query is correct:
select * from `romainmazb_realestate_house_models` where `id` > 3 order by `name` asc
Is that a october issue? or am I missing something?
Last updated
1-1 of 1