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 7704987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:47:48+00:00 2026-05-31T23:47:48+00:00

When creating an association using Doctrine 2 and the Zend Framework, if the associated

  • 0

When creating an association using Doctrine 2 and the Zend Framework, if the associated object is empty e.g. for entity->associated_entity->item if associated_entity is empty, i.e. there is not an associated entity to the original entity, then I get an error Trying to get property of non-object.

I know this is because I am trying to get the item from an empty entity.

What is the standard way to avoid this error?

I am using the code below to get the data, but because the initial associated entity will be returned as ”, then it can’t then get the item from ”

public function __get($name) 
{
    if (isset($this->$name)){
        return $this->$name;
    } else {
        return '';
    }
}  
  • 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-05-31T23:47:48+00:00Added an answer on May 31, 2026 at 11:47 pm

    you could try:

    $associatedEntity = $entity->associated_entity;
    
    if ($associatedEntity) {
        $item = $associatedEntity->item;
    }
    

    Edit:

    OK then. Try putting this in your template/view:

    <?php
    $department = $instruction->department;
    
    if ($department) {
        echo $department->department;
    }
    ?>
    

    Edit 2 (after a small discussion in the chat :D):

    I think that there is no way to tell to PHP to stop the chain. E.g.
    $object1->object2->attribute
    If you write it this way, no matter what you put in the __get(), PHP will assume that object2 is an object and will try to fetch the requested attribute.

    The easiest solution would be something like that:

    <?php foreach ($this->data as $instruction) : ?>
        <?php if ($dep = $instruction->department) echo $dep->department ?>
    <?php endforeach ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Microsoft's designer for the Entity Framework (v3.5), I have created an Entity Model
I am trying to creating an optional association between a couple of tables. I
I am having some trouble creating a nested association and I can't quite spot
Here is my object association: Summary -> Job -> Operator -> Job -> Operator
I'm working on a C# WPF app with a MySQL backend using Entity Data
I'm struggling with creating a simple one-to-many relationship using Code First in EF. I
I am using EF (Framework 3.5 SP1) and have a simple two table demo
Using Entity Designer in VS2010 to build a database for SQL Server 2005. Having
I'm using Rails 3 and have a rich association between Projects and Users. The
I'm creating a Q&A application in CakePHP, and I want to exclude my associations

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.