? Success

User tests: Successful: Unsuccessful:

avatar wilsonge
wilsonge
9 Apr 2014

http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=33585&start=0

In many of our JTable classes (see examples for contact and newsfeeds) we repeat the same code over and over json encoding some fieldsets with JRegistry. In this PR I abstract this out into the parent JTable::bind() method to make it more DRY - now all you have to do is create an class array with the array of data you wish to json encode.

I've applied this to the contact and newfeeds components and it's allowed me to remove the entire bind overridden function in exchange for just this class variable

It's 100% b/c with any existing code as the class var is left empty in the parent JTable method.

avatar wilsonge wilsonge - open - 9 Apr 2014
avatar wilsonge wilsonge - change - 9 Apr 2014
Title
[imp] JTable improvement to the bind method
[#33585] [imp] JTable improvement to the bind method
avatar wilsonge wilsonge - change - 9 Apr 2014
The description was changed
Title
[imp] JTable improvement to the bind method
[#33585] [imp] JTable improvement to the bind method
Description <p>In many of our JTable classes (see examples for contact and newsfeeds) we repeat the same code over and over json encoding some fieldsets with JRegistry. In this PR I abstract this out into the parent JTable::bind() method to make it more DRY - now all you have to do is create an class array with the array of data you wish to json encode.</p> <p>I've applied this to the contact and newfeeds components and it's allowed me to remove the entire bind overridden function in exchange for just this class variable</p> <p>It's 100% b/c with any existing code as the class var is left empty in the parent JTable method.</p> <p><a href="http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&amp;tracker_item_id=33585&amp;start=0">http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&amp;tracker_item_id=33585&amp;start=0</a></p> <p>In many of our JTable classes (see examples for contact and newsfeeds) we repeat the same code over and over json encoding some fieldsets with JRegistry. In this PR I abstract this out into the parent JTable::bind() method to make it more DRY - now all you have to do is create an class array with the array of data you wish to json encode.</p> <p>I've applied this to the contact and newfeeds components and it's allowed me to remove the entire bind overridden function in exchange for just this class variable</p> <p>It's 100% b/c with any existing code as the class var is left empty in the parent JTable method.</p>
avatar wilsonge wilsonge - change - 9 Apr 2014
Labels Added: ?
Removed: ?
avatar beat
beat - comment - 9 Apr 2014

I'm still deeply opposed to commit wilsonge@80cb859 from this PR.
Saving a few extra-lines of code on one column and loosing proper software design abstraction is against our coding standards imho.
See hidden comments above for the discussion.

avatar Fedik
Fedik - comment - 9 Apr 2014

which abstraction need in this case?
we have a problem that json_encode work different on win or unix, or someone will compile this code to javascript? :wink:

avatar beat
beat - comment - 9 Apr 2014

Ok, to clarify some CS basics on this example: :)

  • JRegistry is a class representing a registry and abstracting its private implementation
  • json_encode is a json encoding implementation
  • JRegistry happens today to use json. If tomorrow or on site xyz, it uses xml, only one place needs to change, and other places don't have to know details.

See the difference ?

More information on that CS subject (among many other good books): http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29

avatar Fedik
Fedik - comment - 9 Apr 2014

that right, but in this case it unnecessary because:

if (!empty($this->jsonEncode))

alludes :wink:

avatar beat
beat - comment - 9 Apr 2014

Indeed wrong too! I didn't review rest of PR yet.
Then that added line needs to be fixed to follow proper CS (Computer Science) semantics too. ;)

avatar wilsonge
wilsonge - comment - 9 Apr 2014

So @beat would it be fair to say that in general the PR is fine - it's just whether to use json_encode or to keep JRegistry (and if so the change the corresponding class variable name)? But otherwise you are happy?

avatar phproberto
phproberto - comment - 9 Apr 2014

First of all we should test if this works. I have this already for my own extensions and I'm using JRegistry so maybe it's required because the format isn't right just with json_encode.

If both systems work I would use json_encode. We are performing a very specific action and JRegistry here only consumes resources and adds a dependency. In the future if we need to encode to a different format we will need to replace toString() with the new encoding anyway.

avatar wilsonge
wilsonge - comment - 9 Apr 2014

It definitely does work both with both json_encode and JRegistry as unit tests passed before and after (ignore travis saying it doesn't - it definitely did - it's because I tried rebasing and I'm having to try and do it with my home PC which isn't liking me)

Also I'd add if the format needs to change we can't do it before J4 anyhow in which case we can change the variable name and move to whatever JRegistry Object we want anyhow :)

avatar beat
beat - comment - 9 Apr 2014

@wilsonge :+1: for the general idea (except for "hardcoding" outside of JRegistery its implementation).

It makes sure sense to move the Registery-encoding down to JTable level, as it removes lots of duplicate code in every child class, and brings a consistency back.

I'm still thinking that there could be an even more elegant way to do this cleanly separated from JTable but invoked by JTable.

But more importantly remove the dependancy of JRegistry in JTable using either a DI pattern or an Observer pattern.

I think that a JTableObserver could implement that part, called by the JTable bind(). E.g. JTableObserverRegistry could listen to bind() and do the job.

function bind could have this at the begin:

    // Implement JObservableInterface: Pre-processing by observers
$this->_observers->update('onBeforeBind', array($src, $ignore));

(and similar to e.g. load() obserer at the end)

instead of your loops.

It would removes the dependancy, while implementing a much more extensible pattern for binding. Actually those observer calls in bind() seem to have been forgotten yet.

The observer itself would take as constructor parameter the list of columns which are JRegistry and thus could be a unique class for all JTables. Now the last thing to determine here is where to attach the JTableObserverRegistry to the JTableSomething object. That could be done using the JObserverMapper class, like for other observers. There is a part which is missing in Joomla right now to complete the proper place to invoke the JObserverMapper, but we could take this opportunity to add it for 3.3...

avatar wilsonge
wilsonge - comment - 9 Apr 2014

My gut says it shouldn't be part of the observer because we can't put an array into this field. It HAS to be json encoded (otherwise JTable::store just fails) and therefore should be directly done in the table class.

I'm open to an onBeforeBind method but I think that's outside the scope of this PR.

avatar Bakual
Bakual - comment - 9 Apr 2014

but we could take this opportunity to add it for 3.3...

I think for 3.3 it's to late. We're already close to the second beta and I'm not a huge fan of adding new classes at this time.

avatar Bakual
Bakual - comment - 9 Apr 2014

I'm also with Roberto on the json_encode question. As long as it produces the same result, I don't see why we need JRegistry just to convert an array to an json string. We're not using any specific JRegistry features here as far as I see.

avatar b2z
b2z - comment - 21 Apr 2014

:+1: for json_encode and I see that @wilsonge already have changed it :)

avatar beat
beat - comment - 23 Apr 2014

I guess this is a typical case of Software Architecture vs Coder implementation. For what it's worth:
:-1: from me for json_encode as it breaks the fundamental rule of encapsulating data handling, an important Software Architecture principle that is the basis of MVC that was embraced by Joomla!

avatar Kubik-Rubik
Kubik-Rubik - comment - 27 Jul 2014

@wilsonge Great work, thanks! Tested successfully.

avatar phproberto phproberto - change - 13 Aug 2014
Status New Closed
Closed_Date 0000-00-00 00:00:00 2014-08-13 01:51:51
avatar phproberto phproberto - close - 13 Aug 2014
avatar phproberto phproberto - close - 13 Aug 2014
avatar wilsonge wilsonge - head_ref_deleted - 13 Aug 2014
avatar infograf768
infograf768 - comment - 14 Aug 2014

Issues with test:
JDatabaseDriverMysqlTest.testGetCollation
JDatabaseDriverMysqliTest.testGetCollation

JDatabaseDriverMysqlTest::testGetCollation
Line:130 The getCollation method should return the collation of the database.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'utf8_general_ci'
+'latin1_swedish_ci'

/var/lib/jenkins/jobs/cms/workspace/tests/unit/suites/database/driver/mysql/JDatabaseDriverMysqlTest.php:131

avatar Bakual
Bakual - comment - 14 Aug 2014

@infograf768 That issue sounds more related to this PR: #3886. Or not?

avatar wilsonge
wilsonge - comment - 14 Aug 2014

Yeah that will be #3886 not this one only changes the bind method not the getCollation one

avatar infograf768
infograf768 - comment - 14 Aug 2014

@Bakual and @wilsonge

Yep, indeed.

avatar Sophist-UK Sophist-UK - reference | 28ca2fb - 7 Oct 14

Add a Comment

Login with GitHub to post a comment