Carl Booth Posted August 11, 2022 Posted August 11, 2022 (edited) Hi I can anyone help me with manipulating ProFields Table rows? It works ok using first() and last() as per the README, however I can't seem to manipulate specific items, given a selector. I presume I'm doing something wrong when selecting the row(s). $tt = $pages->findOne("template=repeater_basetimetables,title=Silver") // Works - from README $page->of(false); $opday = $page->operatingcalendar_days->last(); $opday->timetable = $tt; $page->save('operatingcalendar_days'); // Doesn't work - set to TableRows is not an allowed type $page->of(false); $opday = $page->operatingcalendar_days("ttdate=2022-08-11"); $opday->timetable = $tt; $page->save('operatingcalendar_days'); // Doesn't work - no error $page->of(false); $opday = $page->operatingcalendar_days("ttdate=2022-08-11")->first(); $opday->timetable = $tt; $page->save('operatingcalendar_days'); Or, do I have to remove and re-create rows I want to modify? Cheers Edited August 12, 2022 by Carl Booth changed code listing 1
Jan Romero Posted August 11, 2022 Posted August 11, 2022 Hi Carl, welcome to the PW forums! Sorry, it’s not clear to me what $award and $tt are in your code? Also, it looks like you’re getting a table row into $opday and then never do anything with it? But yeah, modifying a specific item is described in the module’s readme: $page->of(false); // turn off output formatting, if necessary $award = $page->awards->first(); $award->title = "Most Sustainable Building"; $award->date = "2014-05-01"; $page->save('awards'); (I only have an exceedingly old version, your readme may differ.) Btw, there is a dedicated support board for ProFields, if you have access (I don’t). 1
Carl Booth Posted August 12, 2022 Author Posted August 12, 2022 Hi Jan Thanks for the welcome. $award->timetable is an artefact of me copy and pasting wrong, it should be $opday->timetable, which is a Page reference field in the table. $tt is a Page, the comment at the end that explains how I've assigned it ($pages->findOne). However I'm having the same problem if the field is a plain text. Your example (from the README) works correctly for me (it's essentially the first block in my code) however that's only the first() or last()) item - I'm needing to edit a specific filtered row (or set of rows) - those with a specific field value. I think this is what I'm struggling with - capturing those rows and editing them in a way the module will write back to the database table. Cheers 1
RyanJ Posted November 8, 2024 Posted November 8, 2024 This is an old thread, but I have searched hours for an answer to the same question. I am really surprised I could not find something about this, so I will post how I achieved this. I want to update a specific row in for my pro table field. So in my case, I have added the row id as a hidden input foreach ( $page->getUnformatted('my_table_field') as $item ) { $item->id; //get the id of the row to use in hidden input; } //update part $page->of(false); $update_row = $page->my_table_field->get("id=$id"); $update_row->title = $title; $update_row->description = $description; $page->save('my_table_field'); $page->of(true); I am curious if anyone sees any issues with this approach for if there is a better way doing so.
DrQuincy Posted July 10 Posted July 10 Anyone any idea why this isn't working? I get no errors and $row is correct when I var_dump() it (ProcessWire\TableRow). $page = wire('pages')->get('template=global-settings'); $page->of(false); foreach ($toDelete as $id) { $row = $page->messages->get((int) $id); $page->messages->remove($row); } $page->save('messages');
RyanJ Posted July 10 Posted July 10 (edited) Hi @DrQuincy $page is a reserved variable, so I would recommend changing that to a custom variable. Your code appears to be doing a lookup for another page that you want to update as opposed to the current page. What is $toDelete referencing? Edited July 10 by RyanJ
DrQuincy Posted July 10 Posted July 10 Hi @RyanJ, I take your point but $page is inside a class and okay in this instance. messages is a Pro Fields Table field. $toDelete is an array of IDs from that table. If I run var_dump() on $row I get what you expect: ProcessWire\TableRow And if I echo $row->title it shows the title of the table row I am trying to delete. Everything seems correct on paper but it's not working. There are no errors, it just doesn't remove the item. There must be something really obvious that I'm missing. This is the sample code that comes with the module on deleting a row: $page->of(false); $award = $page->awards->last(); $page->awards->remove($award); $page->save('awards'); I have some similar code running elsewhere and it's fine. Very odd.
RyanJ Posted July 10 Posted July 10 Hi @DrQuincy I would check and see what ->last() returns vs what $page->messages->get((int) $id); returns. Without digging into, I believe one returns an array and the other an object. I think remove() is looking for an array. 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now