Parse: Check if object exists before fetch

I have a column of type array. It stores pointers to other objects. So in my loop I have something like this:

foreach ($artworkArr as $a) { $a->fetch(); $artworkName[] = $a->get('title'); }

Problem here is, fetch immediately throws an exception if the pointer points to an invalid object.

How can I check if the object exists before calling the fetch method?

-------------Problems Reply------------

Just try with:

foreach ($artworkArr as $a) {
if ($a instanceof ExpectedClass) {
$artworkName[] = $a->get('title');

Or if you don't know the ExpectedClass, you can use method_exists on the $a object:

method_exists($a, 'fetch')

Category:php Views:8 Time:2018-12-27
Tags: php parsing

Related post

  • Selenium 2 (WebDriver): check that text exists before calling Alert.getText() 2011-07-19

    I came across the problem described in Checking If alert exists before switching to it. I find horrible to capture a NullPointerException. Has anyone solved this problem more elegantly? My current solution uses a wait that captures the NPE. The clien

  • check if object exists in Cloud Files (PHP API) 2011-08-10

    I've just started working with the PHP API for Rackspace Cloud Files. So far so good-- but I am using it as sort of a poor man's memcache, storing key/value pairs of serialized data. My app attempts to grab the existing cached object by its key ('nam

  • Best way to check if object exists in Entity Framework? 2009-11-26

    What is the best way to check if an object exists in the database from a performance point of view? I'm using Entity Framework 1.0 (ASP.NET 3.5 SP1). --------------Solutions------------- If you don't want to execute SQL directly, the best way is to u

  • Check if Database Exists Before Creating 2010-02-09

    This seems pretty trivial, but it is now frustrating me. I am using C# with SQL Server 2005 Express. I am using the following code. I want to check if a database exists before creating it. However, the integer returned is -1 and this is how MSDN defi

  • Check if object exists in database without loading object with NHibernate 2010-08-19

    Is there a way in NHibernate to check if an object exists in the database without having to get/load the object? --------------Solutions------------- Could always do a count. I tend to use DetachedCriteria, so I'd have something like: var criteria =

  • Check if object exists in JavaScript 2010-11-15

    How do I verify the existence of an object in JavaScript? The following works: if (!null) alert("GOT HERE"); But this fails: if (!maybeObject) alert("GOT HERE"); Error: maybeObject is not defined. --------------Solutions------------- You can safely u

  • Check if object can be fetched by garbage collector 2010-11-19

    is there a way to check if an object can be fetched by the garbage collector? Somewhere in my code I've got a reference to an object: MyObject mo = myObject; Then, via Eclipse Debugger, I get the objects memory location. Afterwards, I set the referen

  • jQuery how to check if attribute exists before removing it 2011-12-23

    I am getting an error object property doesn't support this method when I try to remove the attribute when I load the page. However I believe this is happening because the disabled attribute was never added on page load, because I do not receive the e

  • Making sure a javascript object exists before calling it from Flash 2012-02-10

    I am developing a flash application for a website I have no direct access to. The flash application is supposed to call a javascript function on the website, defined by the website publisher. I got advised to check for the existance of the javascript

  • Hibernate: Check if object exists/changed 2010-01-18

    Assuming I have an object Person with long id String firstName String lastName String address Then I'm generating a Person-object somewhere in my application. Now I'd like to check if the person exists in the database (= firstname/lastname-combinatio

  • check if Tag exists before adding it to array 2010-10-15

    I have the following code: unless params[:search_tags].nil? logger.debug "Going through tags now #{params[:search_tags]}" params[:search_tags].split(",").each{ |tag| tag.strip! tag = '%' + tag + '%' tags = Tag.find(:all, :conditions => ["name LIKE

  • std::ofstream, check if file exists before writing 2010-11-30

    I am writing a save file functionality with Qt application written in C++. I am looking for a way to check to see if the selected file already exists before writing to it, so that I can prompt a warning to the user. I am using an std::ofstream and I

  • Check if object exists - Objective C 2011-07-21

    Instead of recreating an object over and over again, is there a way I can check if an object exists in an if statement? Thanks! --------------Solutions------------- Assuming your object reference is set to nil if there is no object, then you can use

  • Check if address exists before use/jump in assembly? 2010-05-25

    Say i have loaded some random address like 0x00001234 into eax. Is there a way to test that this address is valid/exists before jumping to it or dereferencing it? Or do I have to implement an exception handler? --------------Solutions------------- Su

  • How to check an object exist or not in Gsp? 2011-09-07

    <g:each in="${clientTripInstance?.startPointJob?.passengerActivities}" status="i" var="startPassengerActivity"> <g:if test = "${startPassengerActivity?.passengerCount > 1}"> <p> ${startPassengerActivity?.activity} ${startPassenge

  • Is checking no.of rows before fetching them more efficient than directly fetching all data in hibernate? 2010-06-16

    I have below scenario Got n as minimum number of rows required for the transaction. Issue select count(*) query to find out number of rows satisfying the criteria. if the result of step 2 is greater than or equal to n, then proceed further or throw e

  • Websphere MQ check if object exists 2011-01-12

    I would like to use the Websphere MQ Java API to create channels, listeners, queues, etc. Is there a way of checking if a object of a given type, e.g. channel already exists on the queue manager? --------------Solutions------------- If you are using

  • c# check if property exists before using it 2011-01-27

    IndicationBase indication = new IndicationBase(Chatham.Web.UI.Extranet.SessionManager.PhysicalUser); // check for permissions LightDataObjectList<TransactionPermission> perms = indication.Model.Trx.TransactionPermissionCollection; So sometimes

  • Check for table existance before running sql with Symfony sfPropelMigrationsLightPlugin 2011-06-01

    I want to check for the existence of a table before running a create SQL statement in the up() method of a migrate. I can run something like: $count = $this->executeSQL("SELECT count(*) FROM information_schema.TABLES where TABLE_NAME = 'table_name

  • Checking if viewWithTag exists before removingFromSuperview 2011-08-19

    Is it safe to use [[self.view viewWithTag:999] removeFromSuperview]; without checking if the view actually exists? There is no error on simulator but will it cause no problem on a real device? Is there any drawback of not using such condition? ------

  • Best way to check if variable exists before subtraction? 2012-01-13

    I have a variable, for example $total1. It has a value from the database, for example 6. Now I do an SQL query and it gets some numbers from tables. For example this: $subtraction1=5, but it is in a while loop so the second time it could be $subtract

  • Check if var exist before unsetting in PHP? 2009-09-03

    With error reporting on, or even for best practice, when unsetting a variable in PHP, should you check to see if it exist first (in this case it does not always exist) and the unset it, or just unset it? <?PHP if (isset($_SESSION['signup_errors'])

  • Is this necessary to check the object nil before alloc it? 2010-11-01

    objective c question, I have an pointer NSObject *abc; I haven't alloc and init it in the init method. So, do I need to do this in the method? -(void)someThingElse{ if(abc != nil){ [abc release]; abc = nil; } abc = [[NSObject alloc] init]; } Is my fl

  • Check if value exists before inserting into MySQL DB in a PHP script 2010-12-08

    I've looked for similar questions with no success. I have this piece of code: form1.php $query = "INSERT INTO table1 "; $query .= "(fname, lname, mail)"; $query .= " VALUES "; $query .= "('".$_POST[fname]."', '".$_POST[lname]."', '".$_POST[mail]."')"

  • Check if file exists before showing image? 2011-06-26

    I need to make an image gallery with a variable number of images. I'm making a loop that checks for a lot of image names, like image1, image2, image3 and so on. I need to make a condition so that it doesn't show a missing file icon if an image doesn'

Copyright (C), All Rights Reserved.

processed in 0.176 (s). 11 q(s)