My title is a little bit ambiguous because my issue is kinda weird to describe.
I got two tables : Articles | Products; There's a many to many relationship between them and as such my pivot table is article_product.
An user can select products when submitting an article and up until then it was working perfectly. But unfortunately I have to run weekly a LOAD DATA INFILE task which is going to update all the products records on the product table.
The function LOAD DATA INFILE REPLACE does exactly what I want. It deletes all of my records on my product table and create new ones. Each product has an id which come from the imported file through the LOAD DATA INFILE command and as such there are two rows on my product table : id (autoincrement) and id_imported (unsignedInteger| index).
id_imported is working as the referenced key on my pivot table in a way that even if I delete all of my records and then create new ones meaning the autoincrement keys would be totally different I would still be able to keep the relationship without problems.
And database wise it's working great but when taking a look at my user interface it doesnt. Take a look :
ArticleController :
$products = Product::lists('designation_1', 'id_imported');
View :
<div class="form-group">
{!! Form::label('product_list', 'Articles: ') !!}
{!! Form::select('product_list[]', $products, null, ['id'=>'product_list', 'class' => 'form-control', 'multiple']) !!}
</div>
Article.php :
public function getProductListAttribute() {
return $this->products->lists('id_imported');
}
This bunch of code would retrieve all the products from the product table into select options. After creating an article with one or many product if an user had to go back to modify it, the linked products would be already selected but it's not. None are selected and more importantly it does work only if I've imported my product table ONCE. Meaning if I import data from my .csv file with the load data infile command once everything is working perfectly but if I do it twice or more then it's breaking everything.
It would be kinda normal if my pivot table wasn't working perfectly. But each time I choose a product the pivot table is updated perfectly! Even if I update my product table many times the relationship is still intact because I've referenced the id_imported key on my product table and not the auto increment key. But yet the system is unable to retrieve a basic relationship such has :
$article->products->lists('id_imported');
I've been struggling on it for hours. It literally makes non sense as my database is working the way it should but it seems laravel is missing something. Do I need to update something? Thanks in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire