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...
Related
I'm trying to paginate my json response in terms of multiples of 20 but for some reason, I'm getting Error: Call to a member function chunk() on array.
To clarify, $results has data in it and $request->get('offset'); has a value. Is there something I'm doing wrong? I'm seeing examples when searching on SO where people are doing this on a collection but for some reason, it isn't working for me. How can I make this work?
Note: If I do dd($results['num_results']);, it displays 360. I feel like I need to use this some how but I'm not sure how. I'm trying to achieve this in the most Laravel way possible. Also note, no database is being used - this is simply a get request on an external API.
Thanks in advance.
collect($results);
$outputs = $results->chunk(20)[$request->get('offset')];
dd($outputs);
$results is still an array. You are not setting the new Collection you are creating to a variable: collect($results). This does not change $results. So you are calling chunk on the array, $results. Call chunk on the Collection instead:
$collection = collect($results);
$outputs = $collection->chunk(20)...;
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()
I am currently trying to learn some OOP by just doing it. I'm trying to create a small Roleplaying website using shops, items and a bank, but the only obstacle is this error getting in may way all the time and I don't know how to solve it myself. I've done some searching on the internets, but nothing came clear for me.
The script gives me
Catchable fatal error: Object of class player could not be converted
to string
It tells me that is has to do something with the query within this bank method;
class bank {
Function bankAccountBalance($player) {
// getting account balance from player`s bank
$check = mysql_query("SELECT * FROM player_bank WHERE player='".$player."'");
while($balance = mysql_fetch_object($check)){return $balance->value;}
}
}
and it is being used by this other method in another class
class shop {
Function shopBuyItem($player,$item,$amount,$value) {
// send item to player, remove value from bank
$player = new player;
$bank = new bank;
$callback = new callback;
$newvalue = $amount*$value; // calc total value of items
if($bank->bankAccountBalance($player)>=$newValue)
{
// correct balance, trading the item
$player->playerAddItem($player,$item,$amount);
$bank->bankRemoveCash($player,$newValue);
}
else
{
// Incorrect Balance, abort
$callback->errorMessage("Your bank account has insuffient balance. You cannot perform this transaction.");
}
}
}
I hope things are clear enough for everybody to help me solving this problem.
If you need any other code according to this topic to help me solve it, please ask me since I don't know what you exactly need.
$check = mysql_query("SELECT * FROM player_bank WHERE player='".$player."'");
Besides the unprotected query, $player is an object and you are treating it as a string, you're concatenating it with the rest of the string query.
Maybe you wanted to call some method, like $player->getId() or a public property, say $player->id?
Technically, you could convert an object to string (json_encode(), serialize()), or even implement out the magic method __toString(), but most likely that's not what you want to do in your current setup, right?
I manage to query for the standard SF objects with this code:
$contactQuery = "SELECT Id, Name FROM Account";
$contactResponse = $sforceClient->query($contactQuery);
$queryResult = new QueryResult($contactResponse);
foreach ($queryResult->records as $record) {
print_r ($record);
}
The problem is that when I try to query for a custom object (products) - I don't get any results.
I tried listing the fields to make sure I query the right fields but didn't manage to do that.
I made sure to add __c after the field names, but unfortunately still no results.
I think my problem is that I might be querying the wrong field names, or not the real object names, is there anywhere special to verify those?
Thanks
OK, got it,
There is the describeSObjects() method which returns all the objects as describeSObject() which have all the required data to query any element.
Cheers
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