Jump to content
porl

Extending Table - Columns From Pages

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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);

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Is it a namespace issue?

Try: $matrix = new \Matrix();

Otherwise, what's error?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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)?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Still doesn't work unfortunately. Not sure what is wrong with my setup. Maybe I'll just have to resort to using SQL 😞

Share this post


Link to post
Share on other sites
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 🙂

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

  • Like 1

Share this post


Link to post
Share on other sites

@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?

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...