Exporting to csv file with Doctrine and Symfony - php

I'm working on exporting from a database table using Doctrine and symfony. I've made a call to my database with
$variableTable = Doctrine::getTable('Table Name')->findAll();
then I set another variable to get data from it like so:
$variable = $variableTable->getData();
I just want to be able to loop through that so I can pull the data I need to put into a csv file but I'm stuck on that part. For some reason, when I debug, it shows that the second variable that is getting set to the value of the getData is null/0.
I'm not sure if I'm missing something.

I think maybe you are trying to call getData on the collection instead of calling getData on the record... But that doesnt make much sense because you are still going to have to call getData on each record, so you might as well just loop over the collection. Additionally its possible there were no results from your findAll for some crazy reason so you should take that into account i think. Anyhow code similar to the following has worked for me in the past so without the details of your code its the best i can offer:
$records = Doctrine::getTable('RecordName')->findAll();
if($records->count()) {
$csvPath = '/path/to/csv/file.csv';
$csvh = fopen($csvPath, 'w');
$d = ','; // this is the default but i like to be explicit
$e = '"'; // this is the default but i like to be explicit
foreach($records as $record) {
$data = $record->toArray(false); // false for the shallow conversion
fputcsv($csvh, $data, $d, $e);
}
fclose($csvh);
// do something with the file
}

There are existing solutions for data import\export.
Consider using one of this:
https://github.com/orocrm/platform/tree/master/src/Oro/Bundle/ImportExportBundle
https://github.com/sonata-project/exporter

Related

Why does Laravel saves each field as "true" when presenting the datas in an array?

So, I have the following code:
$homepage = Homepage::first();
if (!$homepage) {
$homepage = new Homepage;
}
$homepage->first_presta_title = $request->first_presta_title;
$homepage->first_presta_content = $request->first_presta_content;
$homepage->second_presta_title = $request->second_presta_title;
$homepage->second_presta_content = $request->second_presta_content;
$homepage->third_presta_title = $request->third_presta_title;
$homepage->third_presta_content = $request->third_presta_content;
$homepage->shiatsu_text = $request->shiatsu_text;
$homepage->shiatsu_image = $request->shiatsu_image;
$homepage->doin_text = $request->doin_text;
$homepage->doin_image = $request->doin_image;
$homepage->save();
Everything works, but I wanted to see if there weren't any better way to save datas without asigning every single element to its column, then I found out someone answering to a question by using the following code:
$homepage->save($request->all());
So I tried it by myself, but nothing happened: no error, but also nothing saved in my database.
So, is there any fastest way to save datas ? Is it possible to use a loop to save everything?
Thank you in advance
When you use save(), you are actually using Mass assignment. So, either you explicitly define all the fields in your model to be mass assignable or you could use create() instead.
However, in your particular case, the whole method could be cleaned up to just one line:
return Homepage::updateOrCreate($request->all());
If you want the model to autofill based on a given array you need to create a new model entity Like this
$homepage = HomePage::create($request->all());
$homepage->save()
If you give an array to save() it expects the options for saving not for values to assign
Source:
laravel api docs for model/save()
laravel api docs for model::create()

typo3 extbase: is there a way to map exec_SELECTgetRows results to entities?

I have to make a rather complicated query to my database and at it seems that extbase queries cannot do what I need (for example, I need all categories with article-count > 0). So I created a query and execute it with exec_SELECTgetRows - now, is there a way to map the result back to entities?
I'd be thankful for any hints.
You can achieve this by manually triggering PropertyMapper. Check the Flow docs about it. The concept is 1:1 same in ExtBase.
Some example code in your case may be following:
$objectStorage = $this->objectManager->get(ObjectStorage::class);
$propertyMapper = $this->objectManager->get(PropertyMapper::class);
$dataArray = $this->db->exec_SELECTgetRows(...);
foreach($dataArray as $data) {
$dataObject = $propertyMapper->convert($data, \Your\Custom\Object::class);
$objectStorage->attach($dataObject);
}

Parse.com Query Pointer Value

I'm having a problem getting the values from a pointer column. There is a column named "User" that points to the "_User" class in parse. I'm attempting to get the username associated with each row in my locations database. But for some reason, I'm getting a strange response.
Call to a member function get() on a non-object
My code is:
$query = new ParseQuery("SignoutDestination");
$query->includeKey("User");
// Limit what could be a lot of points.
$query->limit(100);
// Final array of objects
$signoutdestinations = $query->find();
foreach($signoutdestinations as $obj){
echo $obj->get("User")->get("username");
}
Has there been a change in the SDK or anything that could be causing this? Or am I doing something wrong?
I encountered the same issue and I kinda find a solution :
$query = new ParseQuery("SignoutDestination");
$query->includeKey("User"); // I didn't use it in my query
// Limit what could be a lot of points.
$query->limit(100);
// Final array of objects
$signoutdestinations = $query->find();
foreach($signoutdestinations as $obj){
$pointer = $obj->get("User");
// It retrieves a pointer to object, so you
// can only getObjectId() and specific fields
// like this
$pointer->fetch();
$pointer->getUsername();
// Now you're able to getUsername()
}
I hope it will help you, I would have post it as comment but couldn't due to my freshly created account.
EDIT: I changed the code with something I tried on my Parse and it worked, but it just ruins your requests ratio because of fetching, find a clever way to get Username just by querying _User and comparing ObjectId.
Indeed, with the includeKey() it should work as you written it up...

Is there an easier way to iterate through a list of objects properties in Laravel

I have a Laravel app where I am using a bit of code which feels really unintuitive.
In the code I return a list of objects ($occupied) which all have the the column 'property'. I then go on to create an array of the list of objects 'property's ($occupiedproperty) just to use it in a whereNotIn call.
if ($occupied = Residency::currentResidents()){
// Here is the pointless part //////
$occupiedproperty = array();
foreach ($occupied as $occ) {
array_push($occupiedproperty, $occ->property);
}
///////////////////////////////////
return Property::whereNotIn('id', $occupiedproperty)->get();
}
This code works fine to do the job but creating a new array when I already have a list of objects seems lazy. I tried looking at eloquent's documentation but I couldn't figure this out.
I need to be able to access the 'property' column of $occupied so I can run something like whereNotIn('id', $occupied->property)
Thank you
Can't test it right now, but this should work (it should work even without casting to array the $occupied collection):
$occupiedProperties = array_pluck((array)$occupied, 'property');
It uses the array_pluck() helper method: http://laravel.com/docs/4.2/helpers#arrays

CodeIgniter $this->load->vars()

I'm wondering how $this->load->vars() works in CodeIgniter. The documentation is fairly vague about it.
I have the following code:
$init = $this->init->set();
$this->load->view('include/header', $init);
$this->load->view('include/nav');
$dates = $this->planner_model->create_date_list();
$this->load->view('planner/dates_content', $dates);
$detail = $this->planner_model->create_detail_list();
$this->load->view('planner/detail_content', $detail);
$this->load->view('include/footer');
However, I also need the $datesarray in my detail_content view. I was trying to load it with $this->load->vars() and hoping it would append to the $detail array, because the CI documentation states as follows:
You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.
Would it work if I do $detail['dates'] = $dates; ? Will it append the $dates array to $detail['dates'] then?
Thanks in advance.
$this->load->vars() is perfect for this purpose. Try this:
$init = $this->init->set();// Won't be passed to the next 2 views
$this->load->view('include/header', $init);
$this->load->view('include/nav');
$dates = $this->planner_model->create_date_list();
$this->load->vars($dates);
$this->load->view('planner/dates_content');
$detail = $this->planner_model->create_detail_list();
$this->load->vars($detail);
$this->load->view('planner/detail_content');
What looks strange to me is that normally you pass an associative array as data, like $data['my_var_name'] = $var_value, so I assume your model calls are returning the data already structured with the variable names (array keys) that you'll use in your view which I do find odd, but then I know nothing of your application.
Here's a more "conventional" version:
$data['dates'] = $this->planner_model->create_date_list();
$this->load->view('planner/dates_content', $data);
$data['detail'] = $this->planner_model->create_detail_list();
// receives both dates and detail
$this->load->view('planner/detail_content', $data);
Have you tried just just building an array that you pass to the different views? I find $this->load->vars() behaves unexpectedly.
As is stated in other answers, and in the user guide, using $this->load->vars() is the same as including the second argument in $this->load->view().
But from the user guide:
The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function.
This to me, is the only reason you'd use $this->load->vars(). As #madmartigan says, it's more convenient to use the view loader with the second argument.

Categories