0

Hi folks! I'm trying to transfer data as array from the controller to the model, and then paste the data into the query builder, but the data must be in the same order as specified in the columns.

  • What options do I have?
  • And do you think this is a bad practice?

Controller:

            $responseNotes = Model::factory('Notes')-> createTicket([
                'description'   => htmlspecialchars($_POST['description']),
                'contact_id'    => $_POST['contact_id'],
                'pref_contact'  => $_POST['pref_contact'],
                'dog_id'        => $_POST['document_id'],
                'type'          => $_POST['type'],
                'owner_id'      => Auth::instance()->get_user()->id,
                'cc'            => $_POST['cc-emails'],
                'title'         => $_POST['title']
            ]);

Model:

public function createNote(array $data)
{
    $columns = [
        'type',
        'owner_id',
        'cc',
        'title',
        'description',
        'contact_id',
        'pref_contact',
        'dog_id'
    ];
    if (!array_diff($columns, array_keys($data))) {
        // All needed values exists
        $result = DB::insert($this->NOTES, $columns)-> values($data)-> execute($this->SCHEMA);
    }
    return  ($result) ? $result : false ;
}

3 Answers 3

3

Thanks to this answer. Solved this by:

// Order $data array according to $column.values
$orderedData = [];
foreach ($columns as $key) {
    $orderedData[$key] = $data[$key];
}
$result = DB::insert($this->TICKETS, $columns)
    -> values($orderedData)
    -> execute($this->SCHEMA);
Sign up to request clarification or add additional context in comments.

Comments

0

Why you don't use ORM Model?

in controller:

$responseNotes = ORM::factory('Notes')-> values([
                'description'   => htmlspecialchars($_POST['description']),
                'contact_id'    => $_POST['contact_id'],
                'pref_contact'  => $_POST['pref_contact'],
                'dog_id'        => $_POST['document_id'],
                'type'          => $_POST['type'],
                'owner_id'      => Auth::instance()->get_user()->id,
                'cc'            => $_POST['cc-emails'],
                'title'         => $_POST['title']
            ])
try{
   $responseNotes->save();
} catch (ORM_Validation_Exception $ex) { 
   print_r($ex->errors('models'));
}

And don't use htmlspecialchars($_POST['description'])

In model class modify function (doc):

public function filters()
{
    return array(
      'description' => array( array('htmlspecialchars') ),
    );
}

Comments

0

It looks like You have associative array with structure db_column=>value right? Than You can simply insert like this:

DB::Insert('table_name',array_keys($data))->values(array_values($data))->execute();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.