Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:53:43+00:00 2026-06-18T01:53:43+00:00

In short, when I pull a record using php activerecord, I’d like to check

  • 0

In short, when I pull a record using php activerecord, I’d like to check that it exists before moving forward. There has to be an easier way to do this as each situation seems to be different and I’m not actually sure any of them are working correctly.

For example:

$var = Model::find_by_pk("something");

if (!empty($var)) { ... //record exists }

Will that catch all exceptions or if the record returns no rows at all for a single record pull?

Then I’ve tried this:

if (count($var->errors) == 0) { ... //records exist }

This seems to work on resultsets, but not single record pulls. I get an exception of trying to reference an object that doesn’t exist.

I see the Model::exists() but does that mean I need to go back through and change all my activerecord calls? (http://www.phpactiverecord.org/docs/ActiveRecord/Model#methodexists)?

To sum up, what is a simple way (preferably universal and/or bullet proof) to check that a php.activerecord result has data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:53:44+00:00Added an answer on June 18, 2026 at 1:53 am

    It should be enough to do

    $var = Model::find_by_pk("something");
    if($var){
        //winning
    }else{
        //not found
    }
    

    The reason you can’t do if (count($var->errors) == 0) is that when you don’t get a hit, $var is not an object so you cannot check the errors for it.

    Mind you that if you do find you’ll get an exception if it doesn’t exist, instead of just no result:

     try{
         $var = Model::find($id);
     }catch (\Exception $e){
          //not found
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In short, currently I am using the following code to pull records from multiple
Short: Is there a way we can push to multiple remotes simultaneously using Github
I have a short, instr , that looks like this: 1110xxx111111111 I need to
Long story short, I would like to take my usercontrols that are loaded dynamically
Short one: Am I correct in assuming, that the Samsung Galaxy S wifi 4.0
Short version : I would like the maven-glassfish-plugin to only be executed in the
Short question: Is it possible to detect window.open() in a UIWebView using the UIWebViewDelegate
I'm using JQuery UI Autocomplete to pull records from a caller database. This works
I have many short-lived branches in my workflow and I would like them to
Let's say that I have a bunch of records in MySQL, something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.