I'm currently saving an object into a class in Parse which currently works fine.
Up to now this has mostly been flat data or the odd pointer which i'm comfortable with.
However, I've recently created a relation column is linked to a Child class which consists of a String and 2 pointers. This data will be provided at the same time as the main row is created.
In theory:
Post data from form to php to process - before saving the main object, create a new ParseObject for the relation providing the String data and associativeArray pointer id's.
Is this the correct way to do it? I have a strange feeling that i will need to save the main row before adding the relation?
Any help would be appreciated greatly :)
You will need to save the main row before adding the relation, because the relation relies on a reference, which relies on an objectId, which doesn't exist until after the initial save. I'm unfamiliar with PHP, though would recommend you add this relation in the success handler of the save call, if there's an equivalent. If there isn't an equivalent, it may be better to write a cloud code function in js to do this, so you can utilize promises / callbacks, and pass the data you have to that function instead of saving the object on your PHP client.
Related
I have a class that represents data returned from a RESTful API. The data returned may contain a lot of different fields or arrays that I want to represent with my object. There may be something like 20 different fields I may have to initialize when the object is created. Some of those fields may be empty depending on the ID I'm trying to get back. I need to do some basic validation to make sure I'm only initializing the fields for values that exist. A simple null/empty check should suffice for this but I don't want a lot of repeated code.
Is there any way to easily accomplish this with magic methods or do I need to manually validate everything with a helper method of some sort?
a couple mouth ago I was worked on a Rest JSON API Wrapper.
Array with loop
My first Idea was to put all my field in a array and validate it with a loop to check the data integrity,
but really specific field like only three string value possible, or one integer with 3 possible value, this method is not enough.
Specific container
So I build specific object container with a specific test in all my field in my object constructor.
The code seams to be very heavy but really simple and obvious if you make if condition one by one (not in cascade).
You can avoid mistype issue or copy past bugs with unit test, and cancel the construction of the object if something is missing or wrong.
JSON Validation
I assume you use json or xml to exchange data between your code and the REST API,
seam to be obvious but JSON validation first give a good idea if you get all your information.
Hope it's help
Regards
I want to create my own class in an existing CodeIgniter project.
I will create an object (say Object1) from that class in a controller (based on the user ID). This object will retrieve data in DB to populate its variables.
I want to be able to use this object in any function of my controller, and if possible I want to transmit it to my views.
In a near future, I might add another object (say Object2) that would contain several Object1.
What's the proper way to do it ? I started to write it in a model but it seems unapropriate ..
In case anyone's interested, I ended up using a normal php file that i store in /classes/.
I've inherited some PHP code that I need to make significant changes on. I know with PHP it is possible to serialize an Object, and pass the serialized text between pages as FormData. In the code I've inherited, they have done just that, But this is creating some maintainability problems. I'm wondering if taking this approach is even a good idea.
For example ...
When the user opens PageA.php the following is created:
$expensiveObj = new ExpensiveClass($id);
The $expensiveObj is then serialized and the resulting text is stored in a div with the following:
<div id="expensiveObj"><?php echo strtr(base64_encode(serialize($expensiveObj)), '+/=', '-_,');?></div>
When PageA.php loads, an ajax call is made to PageB.php. The content of the div is passed along as a post variable to PageB.php. Within PageB.php the following code unserializes the object:
$expensiveObj = unserialize(base64_decode(strtr($_POST['expensiveObj'], '-_,', '+/=')));
The fields and methods of the $expensiveObj are now accessible to PHP. The problems I'm encountering are
Because the $expensiveObj is not identified in PageB.php as an instance of the Class ExpensiveClass then the IDE doesn't know that the fields and functions of ExpensiveClass are available. I can't do autocomplete, nor lookup within the IDE what functions are available. Plus the IDE can't catch potential issues. The other developer worked exclusively in VI, so he never cared.
PageB.php needs to be re-factored. There is view, business, and controller logic all happening within this page, I would prefer to create a couple of classes, but I'm encountering a problem where I don't know how to pass the $expensiveObj to a class.
My questions are, is there a way to pass an Object to a class? And is there a way inform the IDE that the passed in post variable is indeed an instance of ExpensiveClass?
Lastly, is it even a good idea to be passing around objects this way, or should I be looking at a larger re-factor?
Storing objects directly in HTML is never a good idea, because it can be easily changed by client. In PHP is more common to create new object on every request according to given parameters. I see you are initializing your object using $id, so you can just pass this id between requests. Storing data to session also isn't best practice, session should be used for session-specific data, e.g. logged-in user etc.
If the creation of the object is very expensive, you can use cache, e.g. memcache, some external library or just to write your own, for example storing data in JSON on file system or in database.
Simple question that has been driving me nuts. I am working on a project in Flashbuilder, have generated a php service. I have had to make some changes to the database. How do I get flashbuilder to recognize these changes. I have made changes to the php methods and I know how to get them to recognize that. It is just the added fields in my database, how do I get them into the php and then into the corresponding actionscript object.
Thanks
Every time you change the database, you have to 'refresh' the model (your PHP page). In Flex it is called a value object (VO), which is just a PHP class that represents and stores the data from the database.
When the database table changes, the VO object must also change or the new additions to the database will not accessible.
What is happening is that FlashBuilder is seeing the old version of your VO object because it has not been updated. You can write a VO manually in PHP and import it or use the FlashBuilder wizard to re-create it based on the new database tables without having to code, like you did the first time you created it.
For further information on the topic, see: http://www.flashrealtime.com/flash-builder-4-and-php-data-services/
Are you using Value Objects?
Anyway, try to do this:
1. refresh your services in flash builder (right click -> refresh, or just hit the button)
2. right click on your service in flash builder and determine output type. This simple process will ask you the parameters that the php service needs and it will generate the new object
I'm currently writing an EPOS integration for Magento. When an order is made, it's ID is placed in a queuefile. Once a minute, a cron looks at the queue, fires the top order to the EPOS web api and then moves the ID to either a successlist file or a faillist file, depending on the outcome.
In order to display the contents of these lists to the user, I created an admin page that reads the file (containing a serialized array), creates a varien_object containing the order ID, customer name and timestamp for each order, and then stores all of these in an instance of a Varien_Data_collection. This collection is then passed to the _prepareCollection function in the grid.php for rendering the grid view.
In 1.4.1.1, the grid renders fine, but the pagination is broken and the filtering doesn't work.
In 1.3.2.4 the grid renders but says there are 'No Records Found'.
Does anybody know what could be causing these issues, and if there is a better way of going about displaying information from a file in Magento?
The reason why you can see the entries (1.4+), but can't filter is that Magento is using the collection api to modify the object. If you are just pulling values out of a model, its no big deal, but if you are searching and filtering, Magento needs the collection to be an instance of a database. It uses Varien_Db_Select objects to make queries which resolve to raw sql, so that's not going to work on an array.
I would recommend trying to deal with the data in a different way.
It sounds like you are working with a flat file, so the obvious solution of constructing a sql query to fetch everything for you won't cut it. I would try creating an instance of Zend_Db_Table, and populating the values on the fly.
Something like this:
class Foo_Bar_Model_Resource_Success_Collection extends Mage_Core_Model_Resource_Db_Abstract
{
public function _construct()
{
//declare write adapter in config
$table = new Zend_Db_Table('my_db.my_table');
foreach($this->getEposArray() as $entry)
$table->insert($entry);
$this->_init('my_table', 'id');
}
}
Admittedly, I've never done anything quite like this, but have had the custom grid filter problem crop up on me before, and know that if you want to search, you need to have your data in a table of some sort. Check out Zend's docs on the matter. I'm pretty sure that there's a way to do this inside of Magento, but I couldn't begin to think about a solution.
My advice, store your cron job data in a database, it will make pulling the data back out much easier.