insert robots(a,b) values('A','B'),('A','B')
Through the model save() of how to express??
|
Jan '15 |
4 |
2216 |
0 |
$robot = new Robots();
$robot->a = 'A';
$robot->b = 'B';
$robot->save();
$robot = new Robots();
$robot->a = 'C';
$robot->b = 'D';
$robot->save();
You cannot insert multiple values in one SQL statement using Phalcon models. You have to instantiate a new Robots
object, assign the values and then save it. If you want to execute your statement which will insert (in your example) two records in one statement you will need to use the db component directly
$db = $dependencyInjector->get('db');
$db->execute('INSERT INTO .....');
The db component uses \PDO
so all the relevant functions are avaliable for you.
(...) You cannot insert multiple values in one SQL statement using Phalcon models. You have to instantiate a new
Robots
object, assign the values and then save it. If you want to execute your statement which will insert (in your example) two records in one statement you will need to use the db component directly(...)
The db component uses
\PDO
so all the relevant functions are avaliable for you.
Just a friendly remind: As \Phalcon\Db
uses PHP \PDO
component, you can only execute one SQL statement per function call. So, INSERT INTO foo(a,b) VALUES (3, 4), (1, 2)
will work but INSERT INTO foo(a,b) VALUES (3, 4); INSERT INTO foo(a,b) VALUES (1, 2)
won't
I would improve Nikolaos's first answer:
$keys = [ 'a', 'b' ]; $values = [ [$keys[0] => 'A', $keys[1] => 'B'], [$keys[0] => 'C', $keys[1] => 'D'], ]; foreach ($values as $value) { $robot = new Robot(); $robot->a = $value[$keys[0]]; $robot->b = $value[$keys[1]]; $robot->save(); }
$newEntries = array(
array(
'col_1' => 'A',
'col_2' => 'B'
),
array(
'col_1' => 'A',
'col_2' => 'B'
)
);
foreach ($newEntries as $robotParams) {
$robot = new Robot();
foreach ($robotsParams as $key => value) {
$robot->$key = $value;
}
$robot->save();
}
This is how you do magic :p
P.S: This is just to show some magic. Don't use such things at production>
Edit: YES, you can do things like this but i would not recomend unless you know exactly what you want to achieve and why you are doing it this way
Edit2: Sorry if this post sounded rude. I was drunk at the moment :p