CakePHP edit multiple records at once - php

I have a HABTM relationship between two tables: items and locations, using the table items_locations to join them.
items_locations also stores a bit more information. Here's the schema
items_locations(id, location_id, item_id, quantity)
I'm trying to build a page which shows all the items in one location and lets the user, through a datagrid style interface, edit multiple fields at once:
Location: Factory XYZ
___________________________
|___Item____|___Quantity___|
| Widget | 3 |
| Sprocket | 1 |
| Doohickey | 15 |
----------------------------
To help with this, I have a controller called InventoryController which has:
var $uses = array('Item', 'Location'); // should I add 'ItemsLocation' ?
How do I build a multidimensional form to edit this data?
Edit:
I'm trying to get my data to look like how Deceze described it below but I'm having problems again...
// inventory_controller.php
function edit($locationId) {
$this->data = $this->Item->ItemsLocation->find(
'all',
array(
"conditions" => array("location_id" => $locationId)
)
);
when I do that, $this->data comes out like this:
Array (
[0] => Array (
[ItemsLocation] => Array (
[id] => 16
[location_id] => 1
[item_id] => 1
[quantity] => 5
)
)
[1] => Array (
[ItemsLocation] => Array (/* .. etc .. */)
)
)

If you're not going to edit data in the Item model, it probably makes most sense to work only on the join model. As such, your form to edit the quantity of each item would look like this:
echo $form->create('ItemsLocation');
// foreach Item at Location:
echo $form->input('ItemsLocation.0.id'); // automatically hidden
echo $form->input('ItemsLocation.0.quantity');
Increase the counter (.0., .1., ...) for each record. What you should be receiving in your controllers $this->data should look like this:
array(
'ItemsLocation' => array(
0 => array(
'id' => 1,
'quantity' => 42
),
1 => array(
...
You can then simply save this like any other model record: $this->Item->ItemsLocation->saveAll($this->data). Adding an Item to a Location is not much different, you just leave off the id and let the user select the item_id.
array(
'location_id' => 42, // prepopulated by hidden field
'item_id' => 1 // user selected
'quantity' => 242
)
If you want to edit the data of the Item model and save it with a corresponding ItemsLocation record at the same time, dive into the Saving Related Model Data (HABTM) chapter. Be careful of this:
By default when saving a HasAndBelongsToMany relationship, Cake will delete all rows on the join table before saving new ones. For example if you have a Club that has 10 Children associated. You then update the Club with 2 children. The Club will only have 2 Children, not 12.
And:
3.7.6.5 hasAndBelongsToMany (HABTM)
unique: If true (default value) cake will first delete existing relationship records in the foreign keys table before inserting new ones, when updating a record. So existing associations need to be passed again when updating.
Re: Comments/Edit
I don't know off the top of my head if the FormHelper is intelligent enough to autofill Model.0.field fields from a [0][Model][field] structured array. If not, you could easily manipulate the results yourself:
foreach ($this->data as &$data) {
$data = $data['ItemsLocation'];
}
$this->data = array('ItemsLocation' => $this->data);
That would give you the right structure, but it's not very nice admittedly. If anybody has a more Cakey way to do it, I'm all ears. :)

Related

Efficient solution to generating an array in PHP, which extracts unique data from one array, based on data from another

Writing in PHP, I have 2 arrays, each created from SQL queries.
The first query runs through a table that has multiple pieces of data that correspond to various quiz attempts. The table has a column for the user's Email, the activity ID (which represents a quiz attempt) and another 2 columns for data relating to the attempt (for example 'percentage achieved' or 'quiz ID'):
UserEmail ActID ActKey ActMeta
joB#gm.com 2354 Percentage 98
joB#gm.com 2354 Quiz ID 4
boM#hm.com 4567 Percentage 65
boM#hm.com 4567 Quiz ID 7
Once queried, this first array ($student_quiz_list) stores the selected data in the form of
[[UserEmail, ActID, ActKey, ActMeta], [UserEmail, ActID, ActKey, ActMeta], [UserEmail, ActID, ActKey, ActMeta]...]
where each pair of sub-arrays corresponds to a single quiz attempt.
The second table that is queried has two columns that relate to the quizzes themselves. The first column is the Quiz ID and the second is the Quiz name.
Quiz ID Quiz Name
4 Hardware
7 Logic
Once queried, this second array ($quiz_list) stores the selected data in the form of
[[ID, Name], [ID, Name]...]
What I need to do is create a 3rd array (from the 2 above) which holds the user's email and percentage score
[email, percentage], [email, percentage]...]
but with each sub-array corresponding to a unique actID (so basically the user's percentage in each quiz they attempted without duplicates) and (this is the challenging bit) only for quizzes with certain ID values, in this case, let's say quiz ID 4.
In PHP, what would be the most efficient solution to this? I continually create arrays with duplicates and cannot find a neat solution which provides the outcome desired.
Any help would be greatly received.
Try this code as the example and let me know.
$student_quiz_list=array(
array(
'UserEmail'=>'joB#gm.com','ActID'=>'2354','ActKey'=>'Percentage','ActMeta'=>'90',
),
array(
'UserEmail'=>'joB#gm.com','ActID'=>'2354','ActKey'=>'QuizID','ActMeta'=>'4',
),
array(
'UserEmail'=>'boM#hm.com','ActID'=>'4567','ActKey'=>'Percentage','ActMeta'=>'98',
),
array(
'UserEmail'=>'boM#hm.com','ActID'=>'4567','ActKey'=>'QuizID','ActMeta'=>'7',
),
);
$final_array=array();
foreach( $student_quiz_list as $row){
if($row['ActKey']=='Percentage'){
$final_array[]=array('UserEmail'=>$row['UserEmail'],
'ActMeta'=>$row['ActMeta']
) ;
}
}
echo"<pre>"; print_r($final_array); echo"</pre>";
As commenter #Nico Haase suggested, you can do most of the logic in SQL. You didn't respond to my comment, so I suppose a user can have multiple attempts per quiz ID:
SELECT
UserEmail,
ActMeta
FROM
your_table # replace with your table name
WHERE
ActKey = 'Percentage'
AND ActID IN (
# subselection with table alias
SELECT
t2.ActID
FROM
your_table t2 # replace with your table name
WHERE
t2.ActKey = 'Quiz ID'
AND t2.ActMeta = 2 # insert your desired quiz ID here
AND t2.ActID = ActID
)
(Query tested with MySQL/MariaDB)
For the case that you cannot change the SQL part, here is how you can process your data in PHP. But consider that a large dataset could exceed your server capabilities, so I would definitely recommend the solution above:
// Your sample data
$raw = [
['UserEmail' => 'joB#gm.com', 'ActID' => 2354, 'ActKey' => 'Percentage' , 'ActMeta' => 98],
['UserEmail' => 'joB#gm.com', 'ActID' => 2354, 'ActKey' => 'Quiz ID', 'ActMeta' => 4],
['UserEmail' => 'joB#gm.com', 'ActID' => 4567, 'ActKey' => 'Percentage' , 'ActMeta' => 65],
['UserEmail' => 'joB#gm.com', 'ActID' => 4567, 'ActKey' => 'Quiz ID', 'ActMeta' => 7],
];
// Extract the corresponding ActIDs for a QuizID
$quiz_id = 4;
$act_ids = array_column(
array_filter(
$raw,
function($item) use ($quiz_id) {
return $item['ActMeta'] == $quiz_id;
}
),
'ActID'
);
// Get the entries with ActKey 'Percentage' and an ActID present in the previously extracted set
$percentage_entries = array_filter(
$raw,
function($item) use ($act_ids) {
return $item['ActKey'] === 'Percentage' && in_array($item['ActID'], $act_ids);
}
);
// Map over the previous set to get the array into the final form
$final = array_map(
function($item) {
return [$item['UserEmail'], $item['ActMeta']];
},
$percentage_entries
);

How to show data from different related models in cakephp?

I'm fairly new to cakePHP and I'm trying to build a simple webapp.
I have a 3 Models: Property, Operation and Category.
My relationships are:
Category -> hasMany -> Property
Operation -> hasMAny -> Property
each property has a foreign key for the category (cat_id) and for the operation (op_id).
What I want to do is show each property (in the corresponding view) with the NAME of the category and operation (the field is 'name' in the respective tables), and not the ID's. How can I do this?
UPDATE:
An example of a desired output would be:
ID category operation description ....
1 House Sell a house ....
What I have now is
ID category operation description ....
1 2 3 a house ....
2 and 3 being the respective ID's of 'house'(category, cat_id) and 'sell'(operation, op_id)
This is the code of the Category model:
class Category extends AppModel{
public $hasMany = array(
'Property'=>array(
'foreignKey' => 'cat_id'
));
}
Thank you in advance.
properties(id, name, category_id, operation_id)
categories(id, name)
operations(id, name)
when you do a find on the property table with a recursive = 1 or 2 you will get all the related data automatically
Found the solution! Easier than I thought.
First, The use of 'hasMany' was not ideal. What I did was to use the relation "belongsTo" in the Property Model:
class Property extends AppModel{
var $name = 'Property';
public $belongsTo = array(
'Category' => array(
'foreignKey' => 'idCat'
),
'Operation' => array(
'foreignKey' => 'idOp'
)
);
}
Now the magic: Whenever I do a find() in the PropertiesController, What I've got is a two-dimensional array which contains the property data, along with the data of the tables it belongs to. :
Array
(
[Property] => Array
(
[id] => 1
)
[Category] => Array
(
[id] => 2
[name] => House
)
[Operation] => Array
(
[id] => 3
[name] => Sell
)
)
Now in the view, instead of, for instance, ['Property']['idCat'] I just use ['Category']['name'] and everything works.
Thanks for the help!
P.S.: sorry for any mis-understanding, not a native speaker.

Unable to Save BelongsTo Data Using Checkboxes in CakePHP

I have two models with the relationship Advisor belongsTo Room, Room hasMany Advisor. Advisor has a foreignKey constraint in the database (Advisor.room_id) which points to a specific room. The default value for this is the NULL value (representing an advisor without a room).
Suppose I had an Advisor, with room_id set to n. I now wish to unassign the the nth room from Advisor - using a select field, I can reset room_id to NULL, with the following request->data structure:
[Room] => Array
(
[name] => TestRoom2
[type] => single suite
[id] => 4
)
[Advisor] => Array
(
[0] => Array
(
[id] => 14
[room_id] =>
[name] => foo
)
)
However, when I attempt to do this through the use of checkboxes with the same generated $this->request->data, MySQL refuses to update the NULL value.
In addition, it seems that changing the value of room_id explicitly in request->data in the second case has no effect. However, if I were to change Advisor.0.name to hax, (by modifying request->data directly) the name field does save.
I save via calling $this->Room->saveAll($request->data, array('deep' => true)) - this is true both in the case of the select field and the checkbox.
I am generating the series of checkboxes by repeatedly calling the Form helper:
$count = 0;
// $key is the Advisor id, and $attributes is an array of the form
// array('name' => (string), 'disabled' => (bool))
foreach($options['advisorList'] as $key => $attributes) {
$form[] = $this->Form->hidden(sprintf('Advisor.%s.id', $count), array('value' => $key));
$form[] = $this->Form->input(sprintf('Advisor.%s.room_id', $count), array(
'type' => 'checkbox',
'label' => $attributes['name'],
'disabled' => $attributes['disabled']));
$count++;
}
Moreover, if room_id has already been set to NULL, there is no problem setting room_id - except, room_id will be set regardless if the checkbox is checked or not.
Any help would be greatly appreciated - thanks!
Solved! This has to do with funky cakePHP $model->saveAll() on hasMany relationships.
In order to correctly update this form, all advisors that may be changed in saveAll() must unset their relationship to their respective rooms and saved in the database
foreach($copyOfData['Advisor'] as $advisor) {
$advisor['room_id'] = null;
$this->Advisor->save(array('Advisor' => $advisor));
}
Only after this has been done, can we call $this->Advisor->saveAll($this->request->data, array('deep' => true)) and have saveAll() behave as expected.

Some small issue in getting the associated model data in cakephp?

I have 2 models product and image. Product has an Has-many relationship with Image. I want to fetch both products and images records based on product id.So I have written the query like this
$this->Product->find('all',array('conditions'=>array('product_id'=>230));
I am getting all product table entries but not image tables records. I checked with var_dump(), then images table entries are coming like this
array('Images =>
array
0 =>
array
...
1 =>
array
... );
What might be the problem? Any help would be appreciated.
Thanks in advance
Pushpa
$this->Product->find('first', array(
'conditions' => array('Product.id' => 230),
'recursive' => 1 // ensures we are retrieving related models
)
);

CakePHP and HABTM Model Limit Error

I've got a series of Post models that hasAndBelongsToMany Media models. In certain function calls inside of the Post model, I don't need to retrieve the entire list of Media models. However, when I use the following code:
$this->unbindModel( array('hasAndBelongsToMany' => array('Media')) );
// Rebind to get only the fields we need:
$this->bindModel(
array('hasAndBelongsToMany' => array(
'Media' => array(
'className' => 'Media',
'joinTable' => 'media_posts',
'foreignKey' => 'post_id',
'associationForeignKey' => 'media_id',
'limit' => 1,
'fields' => array('Media.type', 'Media.path', 'Media.title')
)
)
)
);
$this->find('all', $params);
This limit only works on one of the first retrieved Post model and all following Post models have no associated Media:
Array
(
[0] => Array
(
[Profile] => Array
(
)
[Media] => Array
(
[0] => Array
(
[type] => photo
[path] => ''
[title] => ''
)
)
)
[1] => Array
(
[Profile] => Array
(
)
[Media] => Array
(
)
)
)
Any suggestions would be great. Thanks!
why not use the containable behaviour
// you would probably want the next line in the app_model ot be able to use it with all models
$this->Post->actsAs = array('Containable')
$params['conditions'] = array(
);
$params['contain'] = array(
'Media' => array(
'fields' => array(
'type', 'path', 'title'
),
'limit' => 1
)
);
$this->Post->find('all', $params);
EDIT:
Just tried that and got this sql (Module <-> Tag):
SELECT `Module`.`id` FROM `modules` AS `Module` WHERE 1 = 1
and
SELECT `Tag`.`id`, `ModulesTag`.`module_id`, `ModulesTag`.`tag_id`
FROM `tags` AS `Tag`
JOIN `modules_tags` AS `ModulesTag`
ON (`ModulesTag`.`module_id` IN (1, 2, 3, 4) AND `ModulesTag`.`tag_id` = `Tag`.`id`)
WHERE `Tag`.`belongs_to` = 'Module'
ORDER BY `Tag`.`name` ASC
LIMIT 1
obviously that cannot return the wanted result, as you would have to do a query for each Module result (which then again would result in way too many queries).
As a conclusion I would return all Tags (in my example) as the overhead in too many result rows is better than the overhead of too many queries..
Cake fetches all the Habtm-related records in one batch query and then assembles them into the results array afterwards. Any additional conditions you specify in the association will be used as is in the query, so it'll look something like this:
SELECT … FROM Media WHERE Media.id in (1, 2, 3, …) LIMIT 1
So it'll only retrieve a single HABTM model.
There's no apparently easy solution for this. Maybe you could think about the original premise again and why the "first" (LIMIT 1) record is supposedly the right one, maybe you can find a different condition to query on.
Failing that, you could rebind your models so Media has a hasMany relationship to medias_posts, the pivot table. For hasMany and belongsTo queries, Cake automatically does JOIN queries. You could use a GROUP BY clause then, which would give you the desired result:
SELECT … FROM Media JOIN medias_posts … GROUP BY medias_posts.post_id
You might also want to experiment with passing the 'join' parameter with the query, to achieve that effect without extensive rebinding.
$this->Media->find('all', array('join' => array(…), …));
Try this:
$this->yourModel->hasAndBelongsToMany['Media'] = false; // or null
And then set your HABTM association manually
$this->yourModel->hasAndBelongsToMany['Media'] = array(........);
Or simply modify the association without nulling it:
$this->yourModel->HABTM['Media']['fields'] = array(....)
CakePHP has a very powerful tool for this containable behaviour

Categories