Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Keep getting 'Trying to get property of non-object'
  • I get the error of 'Trying to get property of non-object' on line 31 which is

    31                <?php echo Form::select('client'Input::post('client', isset($contact) ? $contact->client_id $clients->id), array('class' => 'col-md-4 form-control')); ?>
    32

    From what I understand, when this appears it means $contact is an array instead of an object and I am not understanding why because it is in fact an object unless it is referring to $clients->id 

    and everything has been set up accordingly to the docs as far as ORM relations and ensuring that there is $_belongs_to in the Model_Contact and $_has_many in Model_Client but nothing seems to be working. 

    I am trying to create a new contact that belongs to a client. So I have it set in contacts _form to display an array of clients to add the contact to. This should works for editing contacts too as well. 

    What am I doing wrong?

  • It can be either $contact->client_id or $clients->id. So you need to check both, one of them is not an object.
  • $clients->id is suppose to supply an array of all existing clients. I already know which is not an object, but I am having difficulty in solving that issue.
  • "id" is the name of the relation? Or of the PK column?
  • $clients seems to be an array. If it is, that normal you have an error on "$clients->id"

    If you want to gets all clients id in your array, you can do : 

    $clientIds = array();
    foreach($clients as $client)
      $clientIds[] = $client->id;

    OR

    $clientIds = array_keys($clients) // If keys in $client array are client's id (Fuel orm logic)


    If in all case, $clients array contains 1 row (client), you can do :
    $client = current($clients);
    $id = $client->id; 

  • @Harro - the id is the PK column of clients

    @Syntaxlb - $clients is an array and that is why I think the error is on $clients->id but I tried to use your solution and it keeps throwing error of undefined variable $clients. 

    Would it be possible that I might be placing the code in the wrong spot?
  • can u view what contains $client ? with a var_dump
  • find('all') always returns an array of objects (even if there is only one result), similar to get() in chained mode. find('first') returns a single object, similar to get_one() in chained mode.
  • @Harro

    I am not entirely sure what you're trying to say of what I should do instead. I did try find('first') but keeps throwing error of undefined variable
  • I can't tell you what to do, because I don't know your intentions / design goal.

    All I can see is that you do a find('all'), which returns an array of model objects (or an empty array if no records found), somewhere in your code, and then somewhere else you do $clients->id (which requires an object, not an array), which causes an error, and the two are somehow linked.

    This makes it a bit difficult to give you an answer. ;-)

    Re-reading this topic, I notice there's a mistake in your Form::select() statement too, could it be we're chasing a ghost?

    The prototype is:

    select($field, $values = null, $options = array(), $attributes = array())

    And it looks like you're passing the attributes array as the third argument. So perhaps it goes wrong because the Form class is trying to process your attributes are options?
  • I tried as you suggested but it seems to throw error of non object when it hits $client->id, although I do really think you're on the track towards solution. Lol. 

    I think my controller logic is fine and everything, all of this is coming from _form.php which is a view.
  • Finally got it to display the array list but now it won't save it to the database, but I think this is because I might not have it saved...?

    This is the line that will display the list of clients

    <?php echo Form::select('client', Input::post('client', isset($contact) ? $contact->client_id : $clients), array($clients), array('class' => 'col-md-4 form-control')); ?>
  • I'm still utterly clueless to what you are doing... ;-)

    You pass $clients AND as a selected option value (or multiple values), AND as option data array?

    The second argument of Form::select() is the selected value, which is an int, a string, or an array of ints or strings (in case it's a multiple select). The third argument is an assoc array, containing key-value pairs for your dropdown (and can be multidimensional if you want to use optgroups).

    I don't see how you can pass the same variable in both locations?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion