Wednesday 19 March 2014

How to store multiple jform fields under one field name in database in JSON formate

How to store multiple jform fields under one field name in database in JSON format.
For Instance,
if you want to store plan type(Forever, Fixed, Recurring...), plan price, Number of Recurring, Trial 1 time period, Trial 1 Price..etc information about plan into only one field name "details" into database, then
you can use below code for it.

create all required fields in your view's xml file.
As per my example you have to create fields in
joomla/administrator/components/com_your_component/models/forms/plan.xml(your view's xml file)

After creating all required fields.
Overrides the store() function of JTable class in  below file and add following code in it.
joomla/administrator/components/com_your_component/table/plan.php(your view's table file)

public function store($updateNulls = false)
{
    $input = JFactory::getApplication()->input;

    $plandata = $input->post->get('jform', array(), array());

    $details = array(

            'type'                 => $plandata['type'],

            'price'                => $plandata['price'],

            'expirationTime'       => $plandata['expirationTime'],

            'trial1price'          => $plandata['trial1price'],

            'trial1expirationTime' => $plandata['trial1expirationTime'],

            'trial2price'          => $plandata['trial2price'],

            'trial2expirationTime' => $plandata['trial2expirationTime'],

            'recurranceNumber'     => $plandata['recurranceNumber']
    );

    $detailString = new JRegistry($details);

    $this->details = $detailString->toString();

    // Store the row
    parent::store($updateNulls);

    return true;
}

In this I have stored all the array value in "details" field of my table in database in JSON format.

No comments:

Post a Comment