Salesforce PHP SOAP API get custom object - php

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

Related

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...

Where to instantiate objects for a collection if the DataMapper is only for transfer/exchange of data?

Since the DataMapper is supposed to be for the exchange/transfer of data between objects and the relational database I would get a user like this
$user = $this->entityFactory->build('User');
$userMapper = $this->mapperFactory->build('User');
$user->setId(43);
$userMapper->fetch($user);
That works fine because I can create the User object outside of the mapper and pass it in but what do I do when I am getting a collection/list of objects?
Creating the empty objects outside of the mapper first just does not seem correct and would surely cause some problems so what is the best way to do it?
Thanks.
I don't know if this question is still in your mind, however let me give you an answer to this. In principle the first step is the same
$userCollection = $this->entityFactory->build('UserCollection');
$userCollectionMapper = $this->mapperFactory->build('UserCollection');
$user = $this->entityFactory->build('User');
$user->setId(43);
$userCollection->add($user);
$userCollectionMapper->fetch($userCollection);
So the userObject would function here as an searchObject for the collectionMapper (like teresko proposed in an older thread). Your CollectionMapper would retrieve the data from the database and i prefer to use something like
//returns new User object
$newUser = $userCollection->newUser();
//populate user object with DB entry
$userCollectionMapper->populate($newUser,$DBresponse);
//add to collection
$userCollection->add($newUser);
Of course there would be a loop before that looping through the found lines in the database and you would have to clear the list of user objects before adding the results.
So this is the way i would deal with the problem. Hope it helps.

PHP DB access syntax: "$this->db->select..." How do I use "get()"?

I am editing somebody else's code and I am a PHP beginner.
I want to access the contents of a column called "email" in the database "tga_purchase_items" where the "id" of the row is "14". I want to save the output to a variable "$sp_email".
The code I have is thus:
$sp_email = $this->db->select("email")->from("tga_purchase_items")->where("id", 14)->get();
The variable is coming out empty although the database field is definitely populated.
What am I doing wrong? I am not used to this "->" syntax at all.
The syntax used in your database leans towards Codeigniter and active record.
If this is the case, the following code will retrieve the email column.
$result_set = $this->db->select("email")->from("tga_purchase_items")->where("id", 14)->get();
$result_object = $result_set->row();
$sp_email = $result_object->email;
What you're missing is retrieving the data from the result set which you get. The get() functions return a result set which is similar to the native MySQLi Result class. This means that you need to retrieve the data you want from this set. There are various functions available in CodeIgniter to do this, namely row(), row_array(), result() and result_array() which you can read about on their manual page.
I haven't done too much with CodeIgniter and the Active Record (I prefer Zend Framework) but I think something like this would do the trick:
$this->db->select('email');
$sp_email = $this->db->get_where('tga_purchase_items', array('id' => '14'));
Look at this for a more detailed explanation: http://codeigniter.com/user_guide/database/active_record.html

Problem with accessing class members as an array in PHP class

I'm trying to integrate the FedEx tracking class into my application. The class takes a SOAP response and puts it into a hierarchical class structure. I access the values like this:
$city = $response->TrackDetails->Events[$i]->Address->City;
This works fine for most cases. The problem I am having is when there is just one event for a given shipment, I get an error saying that I cannot treat the class as an array. Since there is just one event, I need to access it without the array index using:
$city = $response->TrackDetails->Events->Address->City;
Is there a way to deal with this without doing something like this:
if($num_events==1){
$city = $response->TrackDetails->Events->Address->City;
}else{
$city = $response->TrackDetails->Events[$i]->Address->City;
}
There are a ton of data fields that fall into this issue, so I don't want to use something so cumbersome if I can avoid it. Any ideas?
if ($num_events == 1) {
$response->TrackDetails->Events = array($response->TrackDetails->Events);
}
This can be done with a loop over all the fields in your answer, automatically putting each loner into an array of length 1.

Getting database values into objects

The thing is that you have classes and then you have the database data. When you create an object how do you set the objects properties to contain the data in the database ?
I saw something like this and I'm wondering if this is really the best way to do it. I'm sure this is a fairly common issue, but I don't know what are the most accepted solutions on how to handle it.
In this example when the object is created you pass an id as a parameter and then you run a query to the database with the id and you assing the returned values to the object properties. I don't have much PHP experience and haven't seen this used much.
Is this an acceptable way to achieve this purpose ? Is there a better or more accepted way ?
public function __construct($id = null){
if($id != null){
$sql = "SELECT *
FROM users
WHERE user_id = $id";
$res = Db::returnRow($sql);
// $res contains an associative array with database columns and values
if($res){
$this->user_id = $res['user_id'];
$this->user_name = $res['user_name'];
//and so on...
}
}
}
Could somebody provide some sample code or pseudocode to illustrate what is the correct way to do this ?
It could be an acceptable way for a homework maybe. But architecturaly it is not.
Your class that is representing your business data (a user in your example) must be loosely coupled with your database access logic. In the end the PHP class acting as a user should not be aware that the data come from a database, a file or any other resource. Following that you will be able to reuse your user php class in other projects without having to change anything to it! If you have your data access logic inside it you are stuck.
Conclusion: I would suggest to read some resources on Design Pattern (in your situation take a look at DAO pattern) ;) Hint: the one from Head First series is extremely accessible and enjoyable.
You could create a function to do this for you automatically, by looping over the associative array's key/value pairs. Or you could look into using an ORM library.
Yes, you can semi-automate this by having a parent class all objects inherit from. On load, it queries, "SHOW FIELDS FROM [my tablename]" and populates an associative array with the names. If an id has been passed in, it looks for a valid object in that table with that id and assigns the values to the array.
Side note: don't pass your id directly into your query like that. Parametize the sql and wrap a function around any user input to sanitize it.
If it's mysql, you can just do:
$obj = mysql_fetch_object($query);
PDO the ability to use arbitrary classes as the target for a fetch, but beware that they assign the variable data before running the constructor:
$pdo->query($stmt, PDO::FETCH_CLASS, "MyClass", array('foo'=>'bar'));
...where the final parameter contains arguments for your class constructor.

Categories