[ZF2] exchangeArray and db fields prefix

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[ZF2] exchangeArray and db fields prefix

o.mellinger
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hi everybody,

I am trying to write my first module on zf2. For that, I am following the getting started tutorial for zf2.

I followed the examples concerning the tableGateway and especially the implementation of the exchangeArray method of the Album class.

I have the impression that this pattern assumes all class attributes names are equals to the fields name in the db table and this is something that disturbs me a lot.

So let's take a simple db table like this one

Table : expression_translations
et_id
et_string
et_locale
(...)

I created a expression class like that

<code>
namespace Translate\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class Expression implements InputFilterAwareInterface
{
    public $id;
    public $locale;
    public $string;
    public (..)
}
</code>

I don't want to name my attributes with the 'et_' prefix as, for me, it is not the responsability of this class to know how my table is structured.

You could answer me that I could deal with this difference in the 'exchangeArray' method of this class, doing something like that

<code>
function exchangeArray($data)
{
$this->id     = (isset($data['te_id'])) ? $data['te_id'] : null;
$this->locale = (isset($data['te_locale'])) ? $data['te_locale'] : null;
$this->string  = (isset($data['te_string'])) ? $data['te_string'] : null;
(..)
}
</code>

but it will mean that in my forms or in my inputFilter configurations, I will have to name my elements with the 'et_' prefix.

I don't know if I am talking about something stupid but it disturbs me.

I looked into the zf2 code and I was looking for a kind of hook in the Zend\Db\ResultSet\ResultSet::current() method
<code>
public function current()
    {
        $data = parent::current();

        if ($this->returnType === self::TYPE_ARRAYOBJECT && is_array($data)) {
            /** @var $ao ArrayObject */
            $ao = clone $this->arrayObjectPrototype;
            if ($ao instanceof ArrayObject || method_exists($ao, 'exchangeArray')) {
                $ao->exchangeArray($data);
            }
            return $ao;
        } else {
            return $data;
        }
    }
</code>

something like that

<code>
public function current()
    {
        $data = parent::current();

        if ($this->returnType === self::TYPE_ARRAYOBJECT && is_array($data)) {
            /** @var $ao ArrayObject */
            $ao = clone $this->arrayObjectPrototype;
           
            if ($ao instanceof ArrayObject || method_exists($ao, 'exchangeArray')) {
                /*operates transformations on resultset datas*/
                //in preprocessDataCallback, I would put a function to remove the prefix 'et_' in my keys
                if ($this->preprocessDataCallback !== null) {
                    $data = call_user_func($this->preprocessDataCallback, $data);
                }
                $ao->exchangeArray($data);
            }
            return $ao;
        } else {
            return $data;
        }
    }
</code>
so before exchanging data with my entity, I provide to the latter an "understandable" array.

is there a better to do that or am I going to the wrong direction ?

Thank you in advance
luk
Reply | Threaded
Open this post in threaded view
|

Re: [ZF2] exchangeArray and db fields prefix

luk
o.mellinger wrote
Hi everybody,

I am trying to write my first module on zf2. For that, I am following the getting started tutorial for zf2.

I followed the examples concerning the tableGateway and especially the implementation of the exchangeArray method of the Album class.

I have the impression that this pattern assumes all class attributes names are equals to the fields name in the db table and this is something that disturbs me a lot.

So let's take a simple db table like this one

Table : expression_translations
et_id
et_string
et_locale
(...)

I created a expression class like that

<code>
namespace Translate\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class Expression implements InputFilterAwareInterface
{
    public $id;
    public $locale;
    public $string;
    public (..)
}
</code>

I don't want to name my attributes with the 'et_' prefix as, for me, it is not the responsability of this class to know how my table is structured.

You could answer me that I could deal with this difference in the 'exchangeArray' method of this class, doing something like that

<code>
function exchangeArray($data)
{
$this->id     = (isset($data['te_id'])) ? $data['te_id'] : null;
$this->locale = (isset($data['te_locale'])) ? $data['te_locale'] : null;
$this->string  = (isset($data['te_string'])) ? $data['te_string'] : null;
(..)
}
</code>

but it will mean that in my forms or in my inputFilter configurations, I will have to name my elements with the 'et_' prefix.

I don't know if I am talking about something stupid but it disturbs me.

I looked into the zf2 code and I was looking for a kind of hook in the Zend\Db\ResultSet\ResultSet::current() method
<code>
public function current()
    {
        $data = parent::current();

        if ($this->returnType === self::TYPE_ARRAYOBJECT && is_array($data)) {
            /** @var $ao ArrayObject */
            $ao = clone $this->arrayObjectPrototype;
            if ($ao instanceof ArrayObject || method_exists($ao, 'exchangeArray')) {
                $ao->exchangeArray($data);
            }
            return $ao;
        } else {
            return $data;
        }
    }
</code>

something like that

<code>
public function current()
    {
        $data = parent::current();

        if ($this->returnType === self::TYPE_ARRAYOBJECT && is_array($data)) {
            /** @var $ao ArrayObject */
            $ao = clone $this->arrayObjectPrototype;
           
            if ($ao instanceof ArrayObject || method_exists($ao, 'exchangeArray')) {
                /*operates transformations on resultset datas*/
                //in preprocessDataCallback, I would put a function to remove the prefix 'et_' in my keys
                if ($this->preprocessDataCallback !== null) {
                    $data = call_user_func($this->preprocessDataCallback, $data);
                }
                $ao->exchangeArray($data);
            }
            return $ao;
        } else {
            return $data;
        }
    }
</code>
so before exchanging data with my entity, I provide to the latter an "understandable" array.

is there a better to do that or am I going to the wrong direction ?
I will strongly recommend you to change your DB naming conventions to follow more standardized one.
Something like that will do:

expression_translations
-----------------------
id
label
locale


And now you got your ZF2 issue solved.
Otherwise one way or the other you will need to put a small hack in place to always deal with your prefix removal\addition. Hope it makes sense.
Cheers, -- Luke Mierzwa