Laravel many-to-many sync Issue - database

I am currently working on a many-to-many, creating does not have any issue, but updating with pivot cause an issue sometimes.
here is some of my code
$request->value = array (
0 =>
array (
'consumable_id' => '3',
'quantity' => '2',
),
1 =>
array (
'consumable_id' => '21',
'quantity' => '1',
),
2 =>
array (
'consumable_id' => '20',
'quantity' => '1',
),
3 =>
array (
'consumable_id' => '19',
'quantity' => '1',
),
)
$mcs->consumables()->sync($request->value);
After this code, when i check my database, somehow it duplicate one of the consumable_id.
i create a log on my update controller. updating 2 times
[2021-04-29 10:45:49] local.INFO: MCS ID : 2021042910
[2021-04-29 10:45:49] local.INFO: Current Data...
[2021-04-29 10:45:49] local.INFO: Consumbale ID : 21
[2021-04-29 10:45:49] local.INFO: Quantity : 1
[2021-04-29 10:45:49] local.INFO: Consumbale ID : 20
[2021-04-29 10:45:49] local.INFO: Quantity : 1
[2021-04-29 10:45:49] local.INFO: Consumbale ID : 19
[2021-04-29 10:45:49] local.INFO: Quantity : 1
[2021-04-29 10:45:49] local.INFO: Consumbale ID : 3
[2021-04-29 10:45:49] local.INFO: Quantity : 1
[2021-04-29 10:45:49] local.INFO: Request :
[2021-04-29 10:45:49] local.INFO: array (
0 =>
array (
'consumable_id' => '21',
'quantity' => '2',
),
1 =>
array (
'consumable_id' => '20',
'quantity' => '2',
),
2 =>
array (
'consumable_id' => '19',
'quantity' => '2',
),
3 =>
array (
'consumable_id' => '3',
'quantity' => '4',
),
)
[2021-04-29 10:45:49] local.INFO: Update Sync...
[2021-04-29 10:45:49] local.INFO: Consumbale ID : 3
[2021-04-29 10:45:49] local.INFO: Quantity : 4
[2021-04-29 10:45:49] local.INFO: Consumbale ID : 21
[2021-04-29 10:45:49] local.INFO: Quantity : 2
[2021-04-29 10:45:49] local.INFO: Consumbale ID : 20
[2021-04-29 10:45:49] local.INFO: Quantity : 2
[2021-04-29 10:45:49] local.INFO: Consumbale ID : 19
[2021-04-29 10:45:49] local.INFO: Quantity : 2
[2021-04-29 10:47:16] local.INFO: MCS ID : 2021042910
[2021-04-29 10:47:16] local.INFO: Current Data...
[2021-04-29 10:47:16] local.INFO: Consumbale ID : 3
[2021-04-29 10:47:16] local.INFO: Quantity : 4
[2021-04-29 10:47:16] local.INFO: Consumbale ID : 21
[2021-04-29 10:47:16] local.INFO: Quantity : 2
[2021-04-29 10:47:16] local.INFO: Consumbale ID : 20
[2021-04-29 10:47:16] local.INFO: Quantity : 2
[2021-04-29 10:47:16] local.INFO: Consumbale ID : 19
[2021-04-29 10:47:16] local.INFO: Quantity : 2
[2021-04-29 10:47:16] local.INFO: Request :
[2021-04-29 10:47:16] local.INFO: array (
0 =>
array (
'consumable_id' => '3',
'quantity' => '2',
),
1 =>
array (
'consumable_id' => '21',
'quantity' => '1',
),
2 =>
array (
'consumable_id' => '20',
'quantity' => '1',
),
3 =>
array (
'consumable_id' => '19',
'quantity' => '1',
),
)
[2021-04-29 10:47:16] local.INFO: Update Sync...
[2021-04-29 10:47:16] local.INFO: Consumbale ID : 19
[2021-04-29 10:47:16] local.INFO: Quantity : 1
[2021-04-29 10:47:16] local.INFO: Consumbale ID : 19
[2021-04-29 10:47:16] local.INFO: Quantity : 1
[2021-04-29 10:47:16] local.INFO: Consumbale ID : 21
[2021-04-29 10:47:16] local.INFO: Quantity : 1
[2021-04-29 10:47:16] local.INFO: Consumbale ID : 20
[2021-04-29 10:47:16] local.INFO: Quantity : 1
Did i missed something?

I think i fixed my problem, the array index key was the culprit, i change my input field name.
name="input[consumable_id][quantity]"
The output
[2021-04-29 12:21:27] local.INFO: array (
21 =>
array (
'quantity' => '1',
),
20 =>
array (
'quantity' => '1',
),
19 =>
array (
'quantity' => '1',
),
3 =>
array (
'quantity' => '2',
),
)
Hope someone find this helpful.

Related

Adding new row using update_batch()

in my DATABASE table
parent_id | sibling_id | name | age
1 | 1 | john | 4
1 | 2 | rose | 7
in my AngularJS controller, i concatenated a data that looks like this:
$data = array(
'0' =>
( 'parent_id' => 1,
'sibling_id ' => 1,
'name ' => "john",
'age' => 4),
'1' =>
( 'parent_id' => 1,
'sibling_id ' => 2,
'name ' => "rose",
'age' => 7),
'2' =>
( 'parent_id' => 1,
'sibling_id ' => 3,
'name ' => "mae",
'age' => 5),
i added another array (mae details) to the data, and when i passed it to codeigniter model,
the batch_update() seems not working in terms of adding new row. Batch_update() seems only to work if you have an update to the original rows. Because my code only works for the original rows not whenever i added new row.
I think what i lack here is the logic only.

Cakephp : Find Query issues?

I have an issue with using the find query in cakephp.
The problem is as follows :
I have to two models associated with each other..say MODEL 1 hasmany MODEL2.
Now I want to run a find query through model 1 in such a way that it has to fetch different number of results from Model2 Table as follows :
array(
0 => ( Model1 : { table contents }
Model2 : { array( 0 =>
1 =>
2 =>
3 =>
)
}
)
1 => ( Model1 : { table contents }
Model2 : { array( 0 =>
1 =>
2 =>
3 =>
4 =>
)
}
)
)
and so on. In other words Result of Model2 should vary but Model1 has fixed number of entries. I wanted to know what would be the most efficient ways to obtain the above mentioned result.
Further adding -
Consider the following table - Student (id, class, marks)
I want the following result:
There are 100 rows in the table. I want to fetch a total of 11 rows.
Condition => class is 'Computer'
First row should print details of student having id = 15;
Remaining rows should have any 10 ids ie. id != 15.
In total I need a total of 11 rows.
One more constraint is that user having id = 1 can be anywhere in the table.
This is a relation between two model. Hope it helps you....
Model-1:
var $hasMany = array(
'ProductOptionValue' => array(
'className' => 'ProductOptionValue',
'foreignKey' => 'product_option_id',
'conditions' => array('ProductOptionValue.is_active' => 1)
)
);
Model-2:
var $belongsTo = array(
'ProductOption' => array(
'className' => 'ProductOption',
'foreignKey' => 'product_option_id',
'fields' => array('id', 'product_option_name')
)
);

Issue while looping an array in laravel view

Hi i have 2 arrays returned from a controller.
$order
array (size=2)
0 =>
array (size=2)
'orderid' => int 1
'ostatus' => string 'Placed' (length=6)
1 =>
array (size=2)
'orderid' => int 4
'ostatus' => string 'Placed' (length=6)
$orderdetails
array (size=2)
1 =>
array (size=3)
0 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro1-list.jpg' (length=33)
'brand_name' => string 'Puma' (length=4)
1 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro2-list.jpg' (length=33)
'brand_name' => string 'DressBerry' (length=10)
2 =>
array (size=3)
'oid' => int 1
'img6_path' => string 'images/product-list/pro3-list.jpg' (length=33)
'brand_name' => string 'United Colors of Benetton' (length=25)
4 =>
array (size=1)
0 =>
array (size=2)
'oid' => int 4
'img6_path' => string 'images/product-list/pro4-list.jpg' (length=33)
'brand_name' => string 'Puma' (length=4)
I am trying to print it in this order:
This is what i have been doing:
foreach($order as $ord)
//printing order id
foreach($orderdetail as $od)
foreach($od as $x)
//printing the products
endforeach()
endforeach()
endforeach()
But this is printing
orderid 1 with 3 products
orderid 1 with 1 product (That belonged to orderid 4)
orderid 4 with 3 products (That belonged to orderid 1)
orderid 4 with 1 product
Can anyone tell me how should i loop through the array so that after printing the first 3 products in order1 the loop breaks out and then print the 1 product in order 4 (Like in the picture above). Thanks
#foreach($order as $ord)
//printing order id
#foreach($orderdetail[$ord->orderid] as $od)
#foreach($od as $x)
//printing the products
#endforeach
#endforeach
#endforeach
where #foreach($orderdetail[$ord->orderid] as $od) is the big change... I would give this a try...

CakePHP FormHelper::input – set Index

I want to create a Form with CakePHP's FormHelper having a count fields for multiple items. My form looks like this:
| name | count |
|------------------------+-------|
| Item #1 | 2 |
| Item #2 | 0 |
| Item #3 | 15 |
| Item #4 | 1 |
What I need is a Form that submits data having the following structure:
array(
'Order' => array(
'0' => array(
'id' => 1
'count' => 2
),
'1' => array(
'id' => 2
'count' => 0
),
'2' => array(
'id' => 3
'count' => 15
),
'3' => array(
'id' => 4
'count' => 1
)
)
)
What I tried is this (within my Order view and ONLY the inputs, no table or layout!):
<?php
echo $this->Form->create('Order');
foreach ($item as $value) {
echo $this->Form->input('count', array('type' => 'number', 'min' => '0');
}
echo $this->Form->end('Submit');
?>
What I get is an array formed like this:
array(
'Order' => array(
'count' => '1'
)
)

CakePHP Retrieve results from multiple tables in one model

CakePHP version : 2.X
I have completely edited this post in order to reformulate my question to be clearer.
I'm creating a professional proposal system which contains 5 tables:
proposals.........> id - name - content - created
clients..............> id - name - content - created - proposal_id
products..........> id - name - content - created - client_id
specifications..> id - name - content - created - product_id
appendices.....> id - name - content - created - product_id
I've simplified the columns for the purpose of this example.
So the associations are the following:
proposals > HasMany > clients
clients > BelongsTo > proposals / clients > HasMany > products
products > BelongsTo > clients / products > HasMany > specifications
products > BelongsTo > clients / products > HasMany > appendices
specifications > BelongsTo > products BelongsTo > products
Then I created 5 models with the associations above.
It would look something like this:
proposal 1
....client 1
........product 1
............specification 1
............specification 2
............specification 3
............appendice 1
............appendice 2
........product 2
............specification 4
............specification 5
............appendice 3
............appendice 4
proposal 2
....client 2
........product 3
............specification 6
............specification 7
............specification 8
............appendice 5
............appendice 6
........product 4
............specification 9
............specification 10
............appendice 7
............appendice 8
And so on...
My question is how can I get an array that looks like this and retrieve me all these information in the view index.ctp of my ProposalController.php action?
Thank a lot in advance for your help!
I finally succeeded in building what i wanted using containable behaviour but i got a problem that i can't solve.
I'd like to retrieve a proposal id and display its attached clients, products, specifications and appendices.
To do so, i made the view action in my ProposalsController.php like this:
function admin_view($id){
$this->loadModel('Client');
// $d = $this->Proposal->find('all', array(
$d['proposals'] = $this->Proposal->find('all', array(
'conditions' => array('Proposal.id' => $id),
//'contain' => array() Tableau vide pour supprimer les liaisons
'contain' => array('Client' => array(
'fields' => array('id','name'),
'Product' => array(
'Specification', 'fields'=>array('id','name'),
'Appendice', 'fields'=>array('id','name')
)
))
));
$this->set($d);
// debug($d);
}
I'm retrieving the particular proposal via its id in my condition. Then I send ma query to my admin_view.ctp:
<h1>VIEW</h1>
<?php
// debug($proposals);
?>
<p>
Proposition : <strong><?php echo $proposals[0]['Proposal']['name']; ?></strong>
pour le client <strong><?php echo $proposals[0]['Client']['name']; ?></strong>
ayant le produit <strong><?php echo $proposals[0]['Client']['Product'][0]['name']; ?></strong>
avec la spec <strong><?php echo $proposals[0]['Client']['Product'][0]['Specification'][0]['name']; ?></strong>
et l'annexe <strong><?php echo $proposals[0]['Client']['Product'][0]['Appendice'][0]['name']; ?></strong>
</p>
It works but it seems to be quite a mess especially the way i have to use [0] that seems to me non adequate. Also, if a proposal hasn't any product nor specifications or appendices, it gives me an error for those because they don't exist of course.
How could i rearrange my code to simplify my view to get it to make more sense?
Also the following is my debug($d) uncommented from the view action of my controller :
array(
(int) 0 => array(
'Proposal' => array(
'id' => '1',
'name' => 'Proposal 1',
'created' => '2013-02-15 00:00:00',
'modified' => '2013-02-16 03:00:47',
'due' => '2013-02-28 00:00:00',
'content' => 'Terms and conditions of the proposal 1.'
),
'Client' => array(
'id' => '1',
'name' => 'Client 1',
'Product' => array(
(int) 0 => array(
'id' => '7',
'name' => 'produit 1',
'client_id' => '1',
'Specification' => array(
(int) 0 => array(
'id' => '8',
'name' => 'spec 1 produit 1',
'value' => 'value 1 produit 1',
'product_id' => '7'
),
(int) 1 => array(
'id' => '9',
'name' => 'spec 2 produit 1',
'value' => 'value 2 produit 1',
'product_id' => '7'
)
),
'Appendice' => array(
(int) 0 => array(
'id' => '12',
'name' => 'Annexe 1 produit 1',
'content' => 'content annexe 1 produit 1',
'product_id' => '7'
)
)
),
(int) 1 => array(
'id' => '8',
'name' => 'produit 2',
'client_id' => '1',
'Specification' => array(
(int) 0 => array(
'id' => '10',
'name' => 'spec 1 produit 2',
'value' => 'value 1 produit 2',
'product_id' => '8'
),
(int) 1 => array(
'id' => '11',
'name' => 'spec 2 produit 2',
'value' => 'value 2 produit 2',
'product_id' => '8'
),
(int) 2 => array(
'id' => '12',
'name' => 'spec 3 produit 2',
'value' => 'value 3 produit 2',
'product_id' => '8'
)
),
'Appendice' => array(
(int) 0 => array(
'id' => '13',
'name' => 'Annexe 1 produit 2',
'content' => 'content annexe 1 produit 2',
'product_id' => '8'
)
)
)
)
)
),
(int) 1 => array(
'Proposal' => array(
'id' => '1',
'name' => 'Proposal 1',
'created' => '2013-02-15 00:00:00',
'modified' => '2013-02-16 03:00:47',
'due' => '2013-02-28 00:00:00',
'content' => 'Terms and conditions of the proposal 1.'
),
'Client' => array(
'id' => '2',
'name' => 'Client 2',
'Product' => array()
)
)
)
I get what i need meaning the proposal with id 1 but below there's another array displaying the association of my 2 models Proposal and Client that I don't need because I already have it in the very first array(int) 0.
What am i doing wrong?
Thanks a lot for your help!

Resources