porl Posted February 14, 2020 Share Posted February 14, 2020 Hi all I've been trying to find the "perfect" module to allow me to store a table of data where the rows are manually created but the columns are generated from a selector. I tried playing around with the Matrix field but it didn't quite suit and there is not much support for entering data from a front-end form (API wise I mean, I don't mind building the forms manually). I realised the only thing the ProFields:Table field is missing is the ability to create columns from other data sources. The api works really well for what I need, but I would need to make a custom version of the field for multiple "products". If I could have a built in multi-page select field to generate these columns then each product page could have these customised columns without excessive work. Is there a way to "extend" this module to change the repeater-like interface to one where the columns are selected from, for example, an ASM multi page select field (or even just a text entry selector)? I'm not expecting this to be added to the core of the module as I don't know if it would be useful to others, but I'm a little out of my depth making a similar module from scratch! Link to comment Share on other sites More sharing options...
adrian Posted February 14, 2020 Share Posted February 14, 2020 3 hours ago, porl said: there is not much support for entering data from a front-end form (API wise I mean I generate and populate the matrix field via the API without any problems. Something like this: $matrix = new Matrix(); $page->of(false); $data = array(); foreach ($xs as $k => $v) { $data[$col->id][$row->id] = $v; } $matrix->addMatrixData($page, $matrixFieldName, $data); $page->save($matrixFieldName); Link to comment Share on other sites More sharing options...
porl Posted February 14, 2020 Author Share Posted February 14, 2020 How can you get that to work in the front-end? I get errors trying to instantiate the Matrix() class. Not sure what I'm doing wrong. Link to comment Share on other sites More sharing options...
adrian Posted February 14, 2020 Share Posted February 14, 2020 Is it a namespace issue? Try: $matrix = new \Matrix(); Otherwise, what's error? Link to comment Share on other sites More sharing options...
porl Posted February 14, 2020 Author Share Posted February 14, 2020 Quote Hi again. Further to my last question, how do I use the class in the front end? I tried to copy the snippet with $matrix = new Matrix(); but it couldn't find the class. I tried to add the Matrix.php as a require_once line but now I get an error stating WireData cannot be found, so I assumed I need to instantiate the module some other way. I then tried to use the $modules->get method to load the module and changed new Matrix() to new \Matrix(). This seemed to load the Matrix class now, but it now complains with the following error: Class 'MatrixArray' doesn't yet implement method 'makeBlankItem()' and it needs to. I don't get this error when using the module in the back-end so I assume there is something else obvious I am missing. This is what I posted on the Matrix module thread. I'm still not sure that module would suit though, as I believe I need to use pages to create each table row(?). What would be ideal would be the ability to programatically create columns based on pages (like Matrix can) but rows just function as normal rows in the Table module. This is why I thought it might be "cleaner" to somehow use the Table module but automatically create columns by matching some selector or page select field. I tried browsing the code to see if there was a method I could hook or something but it is all a bit beyond me. I also thought maybe I could look at the Events module and create something more custom that uses a selector to determine columns, but again, I wound up feeling out of my depth. Link to comment Share on other sites More sharing options...
adrian Posted February 14, 2020 Share Posted February 14, 2020 I haven't used the API with Matrix in a while, but if you read a few posts above yours, there is some discussion about the makeBlankItem() method being a new PW core requirement. @kongondo said he would implement it, but it doesn't look like it made it into the master or dev branch. I think if you add it yourself (as per the discussion), it should work as expected. As for using the Table module to automatically create columns, I think that will be a lot more work than you might initially think - lots of things to handle if things need to change. Link to comment Share on other sites More sharing options...
porl Posted February 14, 2020 Author Share Posted February 14, 2020 Gah! Thanks, I read through the whole thread a few times but somehow missed that. I think after reading a certain number of posts over and over your mind just goes blank. I suspect you are correct about the Table module being a bit more difficult to work with. I guess I'll have to think of a way to work with the Matrix module and just create pages for each row or something similar. Link to comment Share on other sites More sharing options...
porl Posted February 14, 2020 Author Share Posted February 14, 2020 One more question: I can now get the Matrix data to be added as you suggested, but is there a similar method to update already existing values (without just writing SQL)? Link to comment Share on other sites More sharing options...
adrian Posted February 14, 2020 Share Posted February 14, 2020 22 minutes ago, porl said: One more question: I can now get the Matrix data to be added as you suggested, but is there a similar method to update already existing values (without just writing SQL)? Same method to update existing values - it will just overwrite the cell that matches the row/column values. Link to comment Share on other sites More sharing options...
porl Posted February 15, 2020 Author Share Posted February 15, 2020 Hmm doesn't seem to work for me, although I had to use the example you had in the original thread with $matrix->add rather than $matrix->addMatrixData since it complained that method doesn't exist. Using this it just adds duplicate values. Am I running an old version or something? Can't see this method in the github repository either. Looking through the old thread messages I saw you asked a similar question at one point but the answer then was to use SQL. Do you know what was changed after that? Link to comment Share on other sites More sharing options...
adrian Posted February 15, 2020 Share Posted February 15, 2020 I am not sure where I got this version from, but the attached has the addMatrixData() method which allows updating. Take a look in the Matrix.php file for usage of that method. FieldtypeMatrix.zip Link to comment Share on other sites More sharing options...
porl Posted February 15, 2020 Author Share Posted February 15, 2020 Thanks! I'll give that a try. Link to comment Share on other sites More sharing options...
porl Posted February 15, 2020 Author Share Posted February 15, 2020 Passing the $page variable to the addMatrixData() function gives an "Integrity constraint violation: 1062 Duplicate entry '1092-0' for key 'PRIMARY'" error (1092 is the page id of course). I also saw that it was requiring an int so I assumed that it actually wanted $page->id. Unfortunately whilst this removed all the errors, now it doesn't store anything any more. Test code is as follows: Quote $page->of(false); $matrix = new \Matrix(); $data = []; $data[1092][1076] = "hello??"; $matrix->addMatrixData($page, "test_matrix", $data); $page->save('test_matrix') Am I doing something wrong? 1092 and 1076 are both valid ids for the column/row pages (yes, I know 1092 is the page itself, I've had no other issues with this and my page selectors just happened to include the original page in my test site). Link to comment Share on other sites More sharing options...
adrian Posted February 15, 2020 Share Posted February 15, 2020 When I simplified the code (a few posts above) from what I am actually using, I think I accidentally switched the order of $col and $row when building up $data array. Please try swapping those. But also, I am not sure whether it will work to reference the page the field is on, eg 1092 in your example. Link to comment Share on other sites More sharing options...
porl Posted February 15, 2020 Author Share Posted February 15, 2020 Still doesn't work unfortunately. Not sure what is wrong with my setup. Maybe I'll just have to resort to using SQL ? Link to comment Share on other sites More sharing options...
adrian Posted February 15, 2020 Share Posted February 15, 2020 Just now, porl said: Still doesn't work unfortunately. Not sure what is wrong with my setup. Maybe I'll just have to resort to using SQL ? I would recommend persisting - do a little debugging - it definitely works ? Link to comment Share on other sites More sharing options...
porl Posted February 15, 2020 Author Share Posted February 15, 2020 Hmm okay, will do then. I might clear out everything I've had previously and start fresh. I may have something else somewhere interfering (can't think what it would be, but who knows). Thanks again for your help. Hopefully I can get this! Link to comment Share on other sites More sharing options...
porl Posted February 15, 2020 Author Share Posted February 15, 2020 Gahhh!! I really can't understand what is wrong. I added some tracy debug lines through the process and all the way to the database call in the addMatrixData() function everything looks good. This is the sql that is built: Quote "INSERT INTO `field_test_matrix` (`pages_id`,`data`,`sort`,`matrix_column`,`matrix_value`) VALUES (1092, 1070, 0, 1066, 'a'),(1092, 1070, 1, 1076, 'b'),(1092, 1070, 2, 1088, 'c'),(1092, 1070, 3, 1093, 'd'),(1092, 1092, 4, 1066, 'e'),(1092, 1092, 5, 1076, 'hello??'),(1092, 1092, 6, 1088, 'g'),(1092, 1092, 7, 1093, 'h'),(1092, 1102, 8, 1066, 'i'),(1092, 1102, 9, 1076, 'j'),(1092, 1102, 10, 1088, 'k'),(1092, 1102, 11, 1093, 'l')" My original array had 'f' for the 1092/1076 item and this is showing that my test message is replacing it in the sql. The result of the $query->execute(); statement is true, so the database isn't giving any errors. But there is no change to the database!! Saving changes from the back-end works fine, just not using this method. Link to comment Share on other sites More sharing options...
adrian Posted February 15, 2020 Share Posted February 15, 2020 I still wonder about referencing 1092 as a value. I would try an example where you don't do that. I would also try adding a new matrix field - I wonder where there is something different in how the field is created using the version you started with vs the one I attached above. I would also try running that SQL query directly - it looks to me like it's going to correctly insert that "hello??" value. What happens when you try? Also, when you say that there is no change to the DB - do you mean you don't see any change to the table in the PW admin, or that the DB table itself isn't changing - they should be connected, but I would want to verify both. Link to comment Share on other sites More sharing options...
porl Posted February 15, 2020 Author Share Posted February 15, 2020 Success!!! Completely cleared out everything, rebuilt the field from scratch, readded to the template and now it is working. Very strange that the whole function worked except the delete/insert statements seemed to be ignored, but for whatever reason recreating it all again works fine now. Thanks again for all your help, I truly appreciate it! Whilst I would have preferred a variant of the Table field for my structure I can work around this one. Sometimes perfection gets in the way of things! 1 Link to comment Share on other sites More sharing options...
ryan Posted February 16, 2020 Share Posted February 16, 2020 @adrian @porl Since this post is in the ProFields board and you guys are discussing a Matrix Fieldtype (that's not related to ProFields), I'm a little worried people might get confused and think this had something to do with FieldtypeRepeaterMatrix (which I at first thought) but it appears you are discussing something different. And actually looks like maybe it would be useful for users outside of this board. Do you mind if I move the topic to one of the public boards, since it's not specific to ProFields, and may be useful others that might be using that Fieldtype? Link to comment Share on other sites More sharing options...
porl Posted February 16, 2020 Author Share Posted February 16, 2020 Hi @ryan No problem, if you think it is useful! Link to comment Share on other sites More sharing options...
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