I am trying to update multiple rows using this code:
$data = [];
foreach ($projectFieldOptions as $mapping) {
$data[] = [
'import_field_slug' => $mapping['value'],
'internal_field_slug' => $mapping['text'],
'custom_field' => $mapping['custom'],
'import' => 1,
'date_upd' => $now,
];
}
$update_feed_mappings = DB::table($db_name . '.feed_mappings')
->where('id_project', $token)
->where('id_feed', $id_feed)
->update($data);
But I got this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update `shibbir_DB1`.`feed_mappings` set `0` = {\"import_field_slug\":\"date\",..........
Would you tell me how can I fix it?
update only works for a single row and each key must correspond to a column in your sql table. You can't use if with a multi-dimensional array.
You should move your update inside your loop to fix the issue.
foreach ($projectFieldOptions as $mapping) {
$data = [
'import_field_slug' => $mapping['value'],
'internal_field_slug' => $mapping['text'],
'custom_field' => $mapping['custom'],
'import' => 1,
'date_upd' => $now,
];
DB::table($db_name . '.feed_mappings')
->where('id_project', $token)
->where('id_feed', $id_feed)
->update($data);
}
Consider the code below please
$data = [];
$projectFieldOptions =
$data[] = [
'import_field_slug' => 1,
'internal_field_slug' => 1,
'custom_field' => 1,
'import' => 1,
'date_upd' => 1,
];
$data[] = [
'import_field_slug' => 2,
'internal_field_slug' => 2,
'custom_field' => 2,
'import' => 2,
'date_upd' => 2,
];
var_dump($data);
The output will be
array(2) {
[0]=>
array(5) {
["import_field_slug"]=>
int(1)
["internal_field_slug"]=>
int(1)
["custom_field"]=>
int(1)
["import"]=>
int(1)
["date_upd"]=>
int(1)
}
[1]=>
array(5) {
["import_field_slug"]=>
int(2)
["internal_field_slug"]=>
int(2)
["custom_field"]=>
int(2)
["import"]=>
int(2)
["date_upd"]=>
int(2)
}
}
So your update translates into something like that
update(['0' => array(...))
But update method needs something like that
update(['import_field_slug' => 1,...])
Conclusion: please update each record one by one inside your loop
Further reading
I want to execute a scheduled task with laravel, which make a post with a single param.
I already checked with Postman my POST, so, I just have to hit myurl.com with param ID=188888 for instance. I get it working with postman.
So, first, I'm making the Laravel command : check:time, which just performs the post, and then, once I get it working, I will schedule it.
Thing is it appears commands is doing nothing, and I have no error logs.
So, really, it is not so easy to debug it...
Here is my Command Code:
class CheckTime extends Command
{
protected $signature = 'check:time {empId}';
protected $description = 'Check your time';
public function handle()
{
$client = new Client;
$numEmp = $this->argument('empId');
$response = $client->post('http://example.com/endpoint.php', ['ID' => $numEmp]);
var_dump($response);
}
}
When I print $response, I get:
class GuzzleHttp\Psr7\Response#495 (6) {
private $reasonPhrase =>
string(2) "OK"
private $statusCode =>
int(200)
private $headers =>
array(6) {
'date' =>
array(1) {
[0] =>
string(29) "Wed, 01 Jun 2016 00:17:52 GMT"
}
'server' =>
array(1) {
[0] =>
string(22) "Apache/2.2.3 (Red Hat)"
}
'x-powered-by' =>
array(1) {
[0] =>
string(10) "PHP/5.3.15"
}
'content-length' =>
array(1) {
[0] =>
string(3) "146"
}
'connection' =>
array(1) {
[0] =>
string(5) "close"
}
'content-type' =>
array(1) {
[0] =>
string(24) "text/html; charset=UTF-8"
}
}
private $headerLines =>
array(6) {
'Date' =>
array(1) {
[0] =>
string(29) "Wed, 01 Jun 2016 00:17:52 GMT"
}
'Server' =>
array(1) {
[0] =>
string(22) "Apache/2.2.3 (Red Hat)"
}
'X-Powered-By' =>
array(1) {
[0] =>
string(10) "PHP/5.3.15"
}
'Content-Length' =>
array(1) {
[0] =>
string(3) "146"
}
'Connection' =>
array(1) {
[0] =>
string(5) "close"
}
'Content-Type' =>
array(1) {
[0] =>
string(24) "text/html; charset=UTF-8"
}
}
private $protocol =>
string(3) "1.1"
private $stream =>
class GuzzleHttp\Psr7\Stream#493 (7) {
private $stream =>
resource(311) of type (stream)
private $size =>
NULL
private $seekable =>
bool(true)
private $readable =>
bool(true)
private $writable =>
bool(true)
private $uri =>
string(10) "php://temp"
private $customMetadata =>
array(0) {
}
}
}
I checked that $numEmp is OK, also, I printed $response and everything seems to be fine
As I said, I also execute the post with Postman, and it works, so, I don't really understand what's going on...
Any idea??
As #Denis Mysenko wisely advised, I tried:
$response->getBody()->getContents()
and found out that my post was getting a SQL error message.
Solution: to pass form parameters with guzzle, you have to pass it like that:
response = $client->post('http://example.com/endpoint.php', [
'form_params' => [
'ID' => $empId,
...
]
]);
Im trying to upload a tab delimited text file.
The issue could be that the columns is not always in the same order.
Once uploaded id like to do the following steps:
Validate column names. If validation passes -> 2
Read line by line in the file (preferably skip column names) and validate some of the values. If a line passes validation -> 3.
Create a model object and store it in an array for later bulk insertion.
Repeat for all lines in the file.
When all lines are done and everything is validated, bulk insert all the objects.
So far i have done the upload part and tried out some different solutions. But I'm pretty much stuck right now.
I'll paste my code from the controller (keep in mind that there is 40 columns in the file i have just written some of them):
public function store()
{
$file = Input::file('file');
$rules = array(
'file' => 'required|mimes:txt'
);
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
foreach(file($file) as $row) {
$row = explode("\t", $row);
$validator = Validator::make(array(
'X'=> $row[0],
'Y'=> $row[1],
'year'=> $row[2],
'provnr'=> $row[3],
'id'=> $row[4]
), Sample::$insertRules);
if($validator->passes()){
$sample = New Sample;
$sample->X = $row[0];
$sample->Y = $row[1];
$sample->year = $row[2];
$sample->provnr = $row[3];
$sample->costumer_id = $row[4];
$sample->save();
} else {
Session::flash('notice', 'Something is wrong!');
Session::flash('sample', $row[3]);
return Redirect::to('import');
}
}
exit;
Session::flash('success', 'Upload successfully');
return Redirect::to('import');
}
else {
// redirect back with errors.
return Redirect::to('import')->withInput()->withErrors($validator);
}
}
This works fine (no bulk insertion yet, ill tend to that later), but i'm not really fond of it, i think theres a better solution.
Don't mind the validation, it's not really finished yet.
What i end up with, if i print_r all the rows you can find below this text.
Right now its kind of hard since the column names is the first array. Somewhere along the line id like to remove it. Since it won't pass the other rows validation.
Array ( [0] => x [1] => y [2] => År [3] => Provnr )
Array ( [0] => 1315903.24 [1] => 6213877.72 [2] => 2014 [3] => 223 )
Array ( [0] => 1315819.62 [1] => 6213937.42 [2] => 2014 [3] => 224 )
So, i'm interested in you guys thoughts about this. Have you got any ideas?
I think it would be better to in some way convert the array, to objects. So i get something like a database result. I just don't know how.
I would like to be able to write $row['X'] in my foreach loop. I think that would be much better.
Is there some kind of way to make that possible? The column names would be each objects identification.
Im really thankful for you're help!
Edit:
So now the issue lies in the special characters of my file.
I have made some edits according to Bogdans comments.
I have included the full $columnMap
The code now looks like this:
$file = Input::file('file');
$rules = array(
'file' => 'required|mimes:txt'
);
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$columns = [];
$columnMap = [
'x' => 'X',
'y' => 'Y',
'Ar' => 'Year',
'Provnr' => 'Provnr',
'Markning' => 'costumer_id',
'pH' => 'pH',
'P_AL' => 'P_AL',
'P_HCl' => 'P_HCl',
'K_AL' => 'K_AL',
'K_HCl' => 'K_HCl',
'Mg_AL' => 'Mg_AL',
'Cu_HCl' => 'Cu_HCl',
'K_Mg_kvot' => 'K_Mg_kvot',
'Bor' => 'Bor',
'Ca_AL' => 'Ca_AL',
'fe' => 'fe',
'al' => 'al',
'Mullhalt' => 'Mullhalt',
'Total_lerhalt' => 'Total_lerhalt',
'Sand_grovmo' => 'Sand_grovmo',
'Volymvikt' => 'Volymvikt',
'T_varde' => 'T_värde',
'S_varde' => 'S_värde',
'Basmattnadsgrad' => 'Basmättnadsgrad',
'Cd_HNO3' => 'Cd_HNO3',
'Kalkbehov' => 'Kalkbehov',
'Jordart' => 'Jordart',
'Fin_lerhalt' => 'Fin_lerhalt',
'Zn' => 'Zn',
'Cu' => 'Cu',
'Cr' => 'Cr',
'Ni' => 'Ni',
'Pb' => 'Pb',
'Hg' => 'Hg',
'Mineralkvave_Kg_N_ha' => 'Mineralkväve_Kg_N_ha',
'Mineralkvave_NO3_N' => 'Mineralkväve_NO3_N',
'Mineralkvave_NH4_N' => 'Mineralkväve_NH4_N',
'Mineralkvave_djup' => 'Mineralkväve_djup',
'Cystnematoder_potatis' => 'Cystnematoder_potatis',
'Cystnematoder_betor' => 'Cystnematoder_betor',
'Cystnematoder_spannmal' => 'Cystnematoder_spannmål',
'Ovrigt' => 'Övrigt'
];
foreach(file($file) as $i => $row)
{
$row = explode("\t", $row);
if($i == 0)
{
$columns = $row;
array_walk($columns, function (&$item)
{
$item = str_replace(
['ä', 'å', 'ö', 'Ä', 'Å', 'Ö'],
['a', 'a', 'o', 'A', 'A', 'O'],
utf8_encode($item)
);
});
continue;
}
$_row = array();
array_walk($row, function ($value, $index) use (&$_row, $columns, $columnMap)
{
$_row[$columnMap[$columns[$index]]] = $value; //*The issue is here.*
});
$row = $_row;
$validator = Validator::make($row, Sample::$insertRules);
if($validator->passes()){
$sample = New Sample;
foreach ($row as $property => $value)
$sample->{$property} = $value;
$sample->save();
} else
{
Session::flash('notice', 'Something is wrong!');
return Redirect::to('import');
}
}
I had to utf8_encode the header of the file, otherwise it wouldn't work. Could this be some issue with the text file?
It seems to be working until it is time to do array_walk on the row with values.
I then get this error: Undefined index: Ovrigt
If I var_dump $columns it looks like this:
array(42) { [0]=> string(1) "x" [1]=> string(1) "y" [2]=> string(2) "Ar" [3]=> string(6) "Provnr" [4]=> string(8) "Markning" [5]=> string(2) "pH" [6]=> string(4) "P_AL" [7]=> string(5) "P_HCl" [8]=> string(4) "K_AL" [9]=> string(5) "K_HCl" [10]=> string(5) "Mg_AL" [11]=> string(6) "Cu_HCl" [12]=> string(9) "K_Mg_kvot" [13]=> string(3) "Bor" [14]=> string(5) "Ca_AL" [15]=> string(2) "fe" [16]=> string(2) "al" [17]=> string(8) "Mullhalt" [18]=> string(13) "Total_lerhalt" [19]=> string(11) "Sand_grovmo" [20]=> string(9) "Volymvikt" [21]=> string(7) "T_varde" [22]=> string(7) "S_varde" [23]=> string(15) "Basmattnadsgrad" [24]=> string(7) "Cd_HNO3" [25]=> string(9) "Kalkbehov" [26]=> string(7) "Jordart" [27]=> string(11) "Fin_lerhalt" [28]=> string(2) "Zn" [29]=> string(2) "Cu" [30]=> string(2) "Cr" [31]=> string(2) "Ni" [32]=> string(2) "Pb" [33]=> string(2) "Hg" [34]=> string(20) "Mineralkvave_Kg_N_ha" [35]=> string(18) "Mineralkvave_NO3_N" [36]=> string(18) "Mineralkvave_NH4_N" [37]=> string(17) "Mineralkvave_djup" [38]=> string(21) "Cystnematoder_potatis" [39]=> string(19) "Cystnematoder_betor" [40]=> string(22) "Cystnematoder_spannmal" [41]=> string(8) "Ovrigt " }
If var_dump $columnMap it looks like this:
array(42) { ["x"]=> string(1) "X" ["y"]=> string(1) "Y" ["Ar"]=> string(4) "Year" ["Provnr"]=> string(6) "Provnr" ["Markning"]=> string(11) "costumer_id" ["pH"]=> string(2) "pH" ["P_AL"]=> string(4) "P_AL" ["P_HCl"]=> string(5) "P_HCl" ["K_AL"]=> string(4) "K_AL" ["K_HCl"]=> string(5) "K_HCl" ["Mg_AL"]=> string(5) "Mg_AL" ["Cu_HCl"]=> string(6) "Cu_HCl" ["K_Mg_kvot"]=> string(9) "K_Mg_kvot" ["Bor"]=> string(3) "Bor" ["Ca_AL"]=> string(5) "Ca_AL" ["fe"]=> string(2) "fe" ["al"]=> string(2) "al" ["Mullhalt"]=> string(8) "Mullhalt" ["Total_lerhalt"]=> string(13) "Total_lerhalt" ["Sand_grovmo"]=> string(11) "Sand_grovmo" ["Volymvikt"]=> string(9) "Volymvikt" ["T_varde"]=> string(8) "T_värde" ["S_varde"]=> string(8) "S_värde" ["Basmattnadsgrad"]=> string(16) "Basmättnadsgrad" ["Cd_HNO3"]=> string(7) "Cd_HNO3" ["Kalkbehov"]=> string(9) "Kalkbehov" ["Jordart"]=> string(7) "Jordart" ["Fin_lerhalt"]=> string(11) "Fin_lerhalt" ["Zn"]=> string(2) "Zn" ["Cu"]=> string(2) "Cu" ["Cr"]=> string(2) "Cr" ["Ni"]=> string(2) "Ni" ["Pb"]=> string(2) "Pb" ["Hg"]=> string(2) "Hg" ["Mineralkvave_Kg_N_ha"]=> string(21) "Mineralkväve_Kg_N_ha" ["Mineralkvave_NO3_N"]=> string(19) "Mineralkväve_NO3_N" ["Mineralkvave_NH4_N"]=> string(19) "Mineralkväve_NH4_N" ["Mineralkvave_djup"]=> string(18) "Mineralkväve_djup" ["Cystnematoder_potatis"]=> string(21) "Cystnematoder_potatis" ["Cystnematoder_betor"]=> string(19) "Cystnematoder_betor" ["Cystnematoder_spannmal"]=> string(23) "Cystnematoder_spannmål" ["Ovrigt"]=> string(7) "Övrigt" }
If i var_dump the first $row with values it looks like this:
array(42) { [0]=> string(10) "1315903.24" [1]=> string(10) "6213877.72" [2]=> string(4) "2014" [3]=> string(3) "223" [4]=> string(4) "6510" [5]=> string(3) "6.8" [6]=> string(4) "10.0" [7]=> string(0) "" [8]=> string(3) "9.5" [9]=> string(0) "" [10]=> string(4) "12.0" [11]=> string(0) "" [12]=> string(3) "0.8" [13]=> string(0) "" [14]=> string(5) "150.0" [15]=> string(0) "" [16]=> string(0) "" [17]=> string(0) "" [18]=> string(0) "" [19]=> string(0) "" [20]=> string(0) "" [21]=> string(0) "" [22]=> string(0) "" [23]=> string(0) "" [24]=> string(0) "" [25]=> string(0) "" [26]=> string(0) "" [27]=> string(0) "" [28]=> string(0) "" [29]=> string(0) "" [30]=> string(0) "" [31]=> string(0) "" [32]=> string(0) "" [33]=> string(0) "" [34]=> string(0) "" [35]=> string(0) "" [36]=> string(0) "" [37]=> string(0) "" [38]=> string(0) "" [39]=> string(0) "" [40]=> string(0) "" [41]=> string(12) "J038790-14 " }
Right now i have no idea what could be the issue. Is there something else i can post so you guys get a better understanding?
Since "Ovrigt" is last, it seems to work fine up until that one.
A wierd thing is that there seems to be a blank space in "Ovrigt " when i var_dump $columns.. Could that be it?
Assuming the first line of the file always contains the columns names, here's a quick approach to accomplish that:
// Define the array that will contain the columns names
$columns = [];
foreach($rows as $i => $row)
{
$row = explode("\t", $row);
// For the first row get the get the column names
if ($i == 0)
{
$columns = $row;
continue; // Skip this iteration
}
// Map each column name to every item in the row
$_row = array();
array_walk($row, function ($value, $index) use (&$_row, $columns)
{
$_row[$columns[$index]] = $value;
});
$row = $_row;
// You can now access row items with
// $row['X'], $row['Y'], etc.
...
}
If you know exactly that all columns will be present in the file (regardless of order), you can go a step further and map the column name from the file to the coresponding model property name. This will make it even more seamless to validate and populate each model:
// Define the array that will contain the columns names
$columns = [];
// Define the column mapping
// 'column name' => 'model property name'
$columnMap = [
'X' => 'X',
'Y' => 'Y',
'Ar' => 'year',
'Provnr' => 'provnr',
'id' => 'customer_id'
];
foreach($rows as $i => $row)
{
// Assuming the column names are always on the first row
if ($i == 0)
{
$columns = $row;
// Replace Swedish special chars with simple Latin chars
array_walk($columns, function (&$item)
{
$item = str_replace(
['ä', 'å', 'ö', 'Ä', 'Å', 'Ö'],
['a', 'a', 'o', 'A', 'A', 'O'],
trim($item)
);
});
continue; // Skip this iteration
}
// Map each column name to every item in the row
$_row = array();
array_walk($row, function ($value, $index) use (&$_row, $columns, $columnMap)
{
$_row[$columnMap[$columns[$index]]] = $value;
});
$row = $_row;
// Pass the $row directly to the validator because the mapped keys will work
$validator = Validator::make($row, Sample::$insertRules);
if($validator->passes()){
$sample = New Sample;
// You can iterate over the row and assign each value automatically
// because each item key is mapped to a property name
foreach ($row as $property => $value)
$sample->{$property} = $value;
$sample->save();
}
else
{
Session::flash('notice', 'Something is wrong!');
Session::flash('sample', $row[3]);
return Redirect::to('import');
}
}
I am trying to implement mongor bundle for laravel version 3.2.14
I have followed all the steps as mentioned in the documentation on mongor homepage
There is no documentation regarding its usage in the homepage but by chance i got the following link : bundle full example
In-spite of following this i am unable to understand why my code is not working.
Strangely i am not getting any errors.
My code is as follows
database.php
'mongor' => array(
'hostname' => '127.0.0.1',
'connect' => true,
'timeout' => '',
'replicaSet' => '',
'db' => 'census',
'username' => 'root',
'password' => '',
),
user.php(Model)
class User extends Mongor\Model{
public static $timestamps = true;
public function __construct($connection = NULL)
{
parent::__construct($connection);
static::$collection = 'user';
}
}
user.php(Controller)
public function get_index()
{
//return View::make('user.index');
$users = User::first();
dd($users);
}
I am getting following data when i execute this url:
object(User)#28 (14) {
["connection"]=>
object(Mongor\MongoDB)#29 (4) {
["_name":protected]=>
NULL
["_connected":protected]=>
bool(true)
["_connection":protected]=>
object(Mongo)#30 (4) {
["connected"]=>
bool(true)
["status"]=>
NULL
["server":protected]=>
string(19) "mongodb://localhost"
["persistent":protected]=>
NULL
}
["_db":protected]=>
object(MongoDB)#31 (2) {
["w"]=>
int(1)
["wtimeout"]=>
int(10000)
}
}
["exists"]=>
bool(false)
["attributes"]=>
NULL
["dirty"]=>
array(0) {
}
["ignore"]=>
array(0) {
}
["includes"]=>
array(0) {
}
["relating"]=>
NULL
["relating_key"]=>
NULL
["relating_table"]=>
NULL
["_limit"]=>
NULL
["_skip"]=>
NULL
["_where"]=>
array(0) {
}
["_sort"]=>
array(0) {
}
["_count":protected]=>
NULL
}
Whereas when i execute the command db.user.find() in the mongo shell i get all the relevant results.
http://pastie.org/1701923 Here is the XML being returned from an API I am querying for zip codes.
I want to pull the data out of each entry and either loop it directly or put it into an array that I can loop. I can't seem to get it right. Here is the latest code I was using:
$xml = new SimpleXMLElement($results);
foreach($xml->zipcoderadius->zipcodes as $loc) {
$codes[] = (string)$loc['zipcode'];
}
print_r($codes);
die();
($results is the returned XML from CURL)
What is being output is Array ( [0] => [1] => [2] => [3] => [4] => )
I think this was,n be a string (zipcode). Try without string
I am not sure you're addressing the zip code correctly. For me the following works, assuming you stored your cURL result in $xmlStr previously.
$push = array();
$foo = simplexml_load_string($xmlStr);
foreach($foo->zipcoderadius->zipcodes->id as $bar) {
array_push($push, (string)$bar);
}
print_r($push);
Leading to the following output:
Array
(
[0] => 75969
[1] => 75970
[2] => 75971
)
By the way: the SimpleXML object looks like this:
object(SimpleXMLElement)#1 (1) {
["zipcoderadius"]=>
object(SimpleXMLElement)#4 (1) {
["zipcodes"]=>
object(SimpleXMLElement)#3 (32) {
["id"]=>
array(3) {
[0]=>
string(5) "75969"
[1]=>
string(5) "75970"
[2]=>
string(5) "75971"
}
["zipcode"]=>
array(3) {
[0]=>
string(5) "94945"
[1]=>
string(5) "94945"
[2]=>
string(5) "94945"
}
...