I was trying to get all the items from a list using the phpSPO library: https://github.com/vgrem/phpSPO
However in the $remoteList properties in the array of Data there are only 100 rows, and the list has more than 100 items. I saw that it's a common problem, however I don't know which solution I should consider for this library.
https://sharepoint.stackexchange.com/questions/74777/list-api-get-all-items-limited-to-100-rows
$remoteList = $web->getLists()->getByTitle(xxx)
$listData = $remoteList->getItems();
There's a public property for the CamlQuery class called ListItemCollectionPosition defined in this file:
https://github.com/vgrem/phpSPO/blob/b05be0eba6902dabd18784576bb6e1d55426fa99/src/SharePoint/CamlQuery.php
That expects a ListItemCollectionPosition object as a value: https://github.com/vgrem/phpSPO/blob/b05be0eba6902dabd18784576bb6e1d55426fa99/src/SharePoint/ListItemCollectionPosition.php
So I think your code will need to look something like:
$remoteList = $web->getLists()->getByTitle(xxx);
$listItemCollectionPosition = new new ListItemCollectionPosition(); // Either add a use statement for this or use the fully qualified class name here
// ... (set the values according to the items/page you require)
$remoteList->ListItemCollectionPosition = $listItemCollectionPosition;
$listData = $remoteList->getItems();
In terms of how to use it exactly, have a read of the information here:
https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee534956(v%3Doffice.14)#retrieving-items-using-list-item-collection-position
The example is in C# but it's not too hard to follow.
It looks as though you set the ListItemCollectionPosition on your query to get subsequent "pages". It also looks as though responses include a ListItemCollectionPosition property themselves that you can then use as a starting point for the next request.
Related
If possible..how would this be done? Laravel 5.5
route('section.category.subcategory',$subcategory->id)
must output (routes/web.php has the get:: set as this too)
/section/{parent_slug}/{subcategory_slug}
I could easily do
route('section.category.subcategory',[
'subcategory_slug' => $subcategory->slug,
'parent_slug'=>$parent->slug
]
);
but I'm trying to avoid having to declare those things all the time.
I thought getRouteKeyName in model would be first place to go to, but it binds to only one variable as far as I could find.
RouteHandler isn't the place to do anything either because it reads the url, not outputs it right?
I'm assuming in some file that I don't know about I will have to set this sort of logic.
if(requested_route is section.category.subcategory)){
// get the parent_id of ID provided,
// get parent's slug
// build the url.
}
Even better, I think I could do a left join when pulling the list of subcategories, so I have $subcategory->parent_slug instead of going for $parent->slug. This way
route('section.category.subcategory',[$subcategory])
has all the variables it needs to work with.
I think for this helper would be a good choice so you could create url like this:
route('section.category.subcategory',build_category_parameters($subcategory))
and you can create function like this:
function build_category_parameters($subcategory)
{
// here any logic that is needed
// here return array something like this
return [ 'subcategory_slug' => $subcategory->slug,
'parent_slug'=> $subcategory->parent->slug
];
}
If you don't have helper file already you can create one using this way Laravel - require php script in a service provider
I've been using procedural php for a long time but am still learning OOP. I was able to find a bit of code online that is in the form of a class and I am able to call a new instance of the class and use var_dump on a string to have it print JSON data to a web page. I can look at the results of the var_dump and see that it's returning exactly what I want. I'm then able to use json_decode on the same string to turn it into and associative array and then I can echo the values from within the class. The problem is, I need to use the array values in more code - it's great that I can confirm it by printing it to a web page but I need to use it... but I'm getting errors that state the array is undefined once I try to access it outside of the class.
I'm using this line to convert the data into an array:
$response_array = json_decode($body, true);
I've already confirmed that this works within the class by using this code to print some of the data:
echo $response_array['amount'];
and it works - I see it on the web page.
I've been using this code to create the new instance of the class:
$fdata = new FData();
$fdata->request($order_total, $cc_exp, $cc_number, $cc_name, $order_id, $customer_id);
(the function named 'request' is defined as a public function inside the class)
After that, I want to grab the $response_array so that I can store the returned data into a transactions table, i.e something like this:
$r = mysqli_query($dbc, "CALL add_transaction($order_id, $response_array['transaction_type'], $response_array['amount'], $response_array['exact_resp_code'], $response_array['exact_message'], $response_array['bank_resp_code'], $response_array['bank_message'], $response_array['sequence_no'], $response_array['retrieval_ref_no'], $response_array['transaction_tag'], $response_array['authorization_num'])");
but I keep getting an error saying that the array values are undefined.
Things I have already tried (and which failed) include:
Defining the variables as public inside the class, setting their value in the class, and then calling them outside the class...
public $amount = $response_array['amount'];
then using $amount in my procedure CALL --- I still get the error saying that $amount is undefined.
Using 'return', as in
return $response_array;
and still the error saying the values are undefined.
I tried embedding the entire rest of my code within the class, just copy/paste it in right after the json_decode... but for some reason it can't seem to then make the database calls and other things it needs to do.
I've been reading about __construct but I'm not sure if it applies or how to use it in this case...
I want to stress that I AM able to see the results I want when I use var_dump and echo from within the class.. I need to access the array created by json_decode OUTSIDE of the instance of the class.
Any advice would be greatly appreciated. Thanks.
Assuming your FData::request method ends with something like this...
$response_array = json_decode($body, true);
return $response_array;
and you call it like this...
$response_array = $fdata->request(...);
You should then be able to use $response_array in the calling scope.
Extra note; you should be using prepared statements with parameter binding instead of injecting values directly into your SQL statements.
This is probably very easy to do, but I can't seem to get my head around it right now. Let's say in a component in a cakephp application, I have a variable my_model, which contains the model of the corresponding controller that is currently using the component like:
function TestComponent extend Object
{
var $my_model; // can be either User, or Person
function test()
{
$myModelTemp = $this->my_model;
$model = $myModelTemp != 'User' ? $myModelTemp.'->User' : 'User';
$this->$model->find('all');
}
}
As you can see above in my function test() what I'm trying to do is call the correct model based on the value of my_model. So based on the condition, my query will be either:
$this->Person->User->find('all');
Or
$this->User->find('all');
When I do it like I did above, I get an error saying Fatal error: Call to a member function find() on a non-object. In order words, that error means Person->User is not an object (so, it is considered as a string).
What you're saying could be true, however, it can refer to any part of the call.
So either Person or User could be invalid, or together they causes the error. Hard to say.
Try dumping the individual objects using var_dump();
So try:
<?php
echo "<pre>";
var_dump(is_object($this->Person));
var_dump(is_object($this->User));
echo "</pre>";
?>
to determine where you're code goes wrong.
To be clear, that return value needs to be true for it to be an object.
The one that returns false is the likely culprit.
Should your question refer to the correct way to reference an object, an object is basically an array. For example:
<?php
$obj = (object) array("this", "my_function");
?>
The above example casts the array as an object. However, using multiple layers might prove to be more difficult than you'd expect.
Generally, it looks like you might be going about this all wrong. Obviously you want the models to be dynamic, but then you're hard-coding things which defeats the whole point of it being dynamic in the first place.
It also seems like you might be violating the principals of CakePHP and MVC by doing all this in a component. I'm not sure this component should really be manipulating models or assuming which models are currently in use.
However, if you want to evaluate a string as an actual object, you can wrap it in { ... } (this is valid standard PHP syntax, not Cake-specific code).
Try this:
$modelName = $this->my_model;
$model = ($modelName != 'User') ? $this->{$modelName}->User : $this->User;
$model->find('all');
Now, if this doesn't work or you get an error saying it can't find the model(s) you need to ensure the models are actually loaded and initialised in the current scope.
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.
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.