Propel: Call to a member function XXXX on a non-object

Want to write down a simple trick I use to avoid errors like the subject says.

Let’s take we have described tables in our schema.xml:
Listing 1

1
2
3
4
5
6
7
8
9
10
11
12
13
  <table name="User" phpName="User" idMethod="native">
    <column name="id" phpName="Id" peerName="ID" type="INTEGER" required="true" autoIncrement="true" primaryKey="true"></column>
    <column name="username" phpName="UserName" peerName="USERNAME" type="VARCHAR" size="32" required="true" default=""></column>
    <column name="state_id" phpName="StateId" peerName="STATEID" type="INTEGER" required="true" default="0"></column>
    <foreign -key foreignTable="State">
      <reference local="state_id" foreign="id"/>
    </foreign>
  </table>
 
  <table name="State" phpName="State" idMethod="native">
    <column name="id" phpName="Id" peerName="ID" type="INTEGER" required="true" autoIncrement="true" primaryKey="true"></column>
    <column name="state_name" phpName="StateName" peerName="NAME" type="CHAR" size="25" required="true" default=""></column>
  </table>

Thanx to relation definition in lines 5-7 we can do a call like this:
Listing 2

1
2
// assuming $user is a valid instance of User
echo $user->getState()->getStateName();

But what happens if a certain user has no `state_id` set for some reason? We’ll get the message: Error: Call to a member function getName() on a non-object…
(Obviously as $user->getState() doesn’t produce a correct State instance without `state_id`)

In case `state_id` isn’t something nitty-gritty in our application we can extend our User class with the following:
Read the rest of this entry »

just few words

1.5 months ago, 6 weeks or 42 days. I haven’t updated my simple blog since that time.

I haven’t a time to deal with my CakePHP projects, but got a pretty nice experience with other things.

Currently I work with Propel based custom framework with extensive usage of jQuery which I found very nice and going to use with CakePHP (when I have a time of course :) ).

Propel is very handy thing as well, it plays role of database communication layer, in other words Propel is ‘model’ component of a framework.

Well, I’m new to these things and sometimes spend a lot of time to figure simple things, but I’m sure when say “Propel rocks! jQuery rocks!” :)