View more context

 

LiamC

anyone experienced the currency field only allowing 999.9 and nothing above? 🙂

Mo

@LiamC You might need to define something like:

  1. private static $db = [
  2. 'Price' => 'Currency(10,2)'
  3. ];
Mo

Quick question, is it possible to write a trait that adds an extension to a DataObject?

Mo

Well I have an extension that add a load of methods to the extended object

Mo

I was wondering if it might make more sense if it was a trait

dorsetdigital

Oh, OK. Yes, that's perfectly possible. I do it all the time

Mo

BUT, it relies on some DB fields being available

Mo

so either I have to add extra logic to the trait to throw an error if fields aren't available, or I make the trait add the DB fields

Mo

Or I just stick to an extension and get on with my like

dorsetdigital

I have a couple of traits for DOs which I use in a couple of situations. For example, if I have a dataobject which needs URL slugs, etc. I apply that specific trait.

dorsetdigital

What that means is that my additional functionality is a bit more modular

kinglozzer

Traits can’t add DB fields 😞

😞 (1)
dorsetdigital

But yes, if a method is structure-specific, you'd need to add some sanity checking in there

Mo

Hmm, so I would have to add some exceptions or something if the required fields aren't available?

dorsetdigital

I don't think that's an issue anyway... since trying to add fields to a DO with a trait would also mean having to add the table name, etc. And then you might as well just be using a subclass

dorsetdigital

The traits allow you add functionality in a modular fashion

Mo

Yea, about 99% of this code isn't SS specific either, so an extension doesn't really feel right

dorsetdigital

Now, I'm fairly sure that your extension can use the traits as well. So you could add your extension to the DO and that extension can use your trait to get the additional functionality

dorsetdigital

You just need to make sure you keep track of which classes own what!

Mo

Hmm, I didn't realise traits were added in PHP 5.4

null

I've done a bit of work here. Traits definitely have their place but they'll never replace DataExtensions. You can use them in DataExtensions, and apply them to things.

As @kinglozzer said, they can't add DB fields. They can manipulate existing ones though - I have one DO that has a a bit "JSONData" Text field - the trait controls a list of keys that end up in that JSON.

null

Basically, anything that's private static is a no-go. Another big drawback, you can't apply them to other modules since you need to modify the source code directly to apply it

Mo

Thankfully that it is not an issue in this case

Mo

Basically, at the moment, I have written a selection of methods that handle calculating prices (with tax, without tax, human readable, etc)

Mo

The logic is common across three classes (The product, A LineItem and A customisation), but needs to be customised slightly for each class

Mo

But in order for all the calculations to work, you need a BasePrice and a TaxRate

Mo

So at the moment all the code is on an extension and just added to A DataObject, but I am thinking it might work better as a trait and maybe require $object->getBasePrice() and $object->getTaxRate() to be available on the base object

null

You could wrap the logic in if (method_exists($this, 'getBasePrice')) { .. } before proceeding


Show less replies
LiamC

Thanks 🙂 and with your question, I never have but I would love to find out if it works

guy_van_bael

hi all. Does userforms need a special php-setting for sending mails. I had to deploy a project to a hosting partner we never dealt with. Userform works, data is saved in submissions, but email is not received. Tested on my local mamp without a problem...

andante

sounds like a server setting to me, rather than userforms specific

andante

talk to the hosting provider, and check your spam folders

dorsetdigital

Do you have a mail transport set up, or are you using an SMTP server?