1

I would like to insert an array into a MYSQL database, preferably using Yii's active record.

For example, I have a an array:

User = array(
    fname => "Joe"
    lname => "Schmidt"
)

with a User table in my database with columns id, fname and lname. One of the options is creating an object and doing:

$user = new User;
$user->fname = User['fname'];
$user->lname = User['lname'];
$user->save();

However, this seems like so much code for such common functionality. Is there a way to insert an array into the database where array keys match corresponding columns without me writing my own function or doing some SQL query hack? Ideally it uses the already present Active record of Yii.

3
  • You can use mass-assignment for this: $user = new User(); $user->attributes = $data_array; if(!$user->save()) { print_r($user->errors, true); }. Assigning an array to the attributes property will do what you want, but only for those attributes which have a validation rule defined for the current scenario. Commented Jul 23, 2012 at 4:28
  • @DCoder, that is a great and succinct solution that I was looking for! If you put it as an answer I will accept it. Commented Jul 23, 2012 at 5:13
  • 1
    Accept Pentium10's answer below instead :) Commented Jul 23, 2012 at 6:37

4 Answers 4

4

What you want to do is handled by the framework itself.

You can mass assign like:

$user->attributes=$_POST['User'];

Read more about Mass Assignment

Sign up to request clarification or add additional context in comments.

Comments

0

I have never worked with Yii before, so I can't offer a solution using that, but you can serialize the array and store it in the single cell in your database, like so:

$user = array("fname" => "Joe", "lname" => "Schmidt");
$serialized = serialize($user);

//Store the $serialized variable in the database

When you are ready to access it:

//Get your data from the database

$unserialized = unserialize($usersFromDB);
$fname = $unserialized['fname']; //Joe

Hope that helps.

Comments

-1

the function is pretty straightforward, try this:

  function insert($table, $fields_values = array())
  {
    $q1 = join(",", array_keys($fields_values));

    $q2 = "'".join("','", array_values($fields_values))."'";

    $query = "INSERT INTO $table($q1) VALUES($q2)";

    // do your DB insert here
  }

The main trick is the array to query conversion using join and array_keys / array_values.

Comments

-1

Depending the amount of data in array you can write you own function e.g

a) check this backUpdate , modify it to insert /remove render view option

b) Follow this thread

c) Possible traps when inserting multiple records

d) check this associated SOQ

If you know what you are doing its easy to do , you just need to take care of

  • validations
  • records exists in associated tables ( if there is FKey involved )
  • option d). will be a posssible answer if you have simple inserts ( with no associated tables)

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.