Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Trying to get property of non-object
  • I use oil r fromdb:atkbr scaffold and oil r fromdb:atktb scaffold to generate the code. then I modify the properties and ORM relation (has many and belongs to. when I access atkbr/view/2 it gives correct result. but when I access atkbr/index it shows Trying to get property of non-object error .

    here's the model  (I remove the unnecessary code):
    class Model_Atkbr extends Model
    {
    protected static $_table_name='atkbr1';
    protected static $_primary_key = array('kunciatkbr');
    protected static $_belongs_to = array(
       'atktb' => array(
           'key_from' => 'kcatkatkbr',
           'key_to' => 'kcatkatktb',
           
       ),
    );

    protected static $_properties = array(
    //'id',
    'kcatkatkbr', //fk
    'kunciatkbr', //pk
    );


  • Without the code and the exact line that gives that error, it will only be guesswork.
  • class Model_Atktb extends Model
    {
    protected static $_table_name='atktb';
    protected static $_primary_key = array('kcatkatktb');
    protected static $_has_many = array(
    'atkbrs' => array(
    'key_from' => 'kcatkatktb',
    'key_to' => 'kcatkatkbr',

    ),
    'atktrs' => array(
    'key_from' => 'kcatkatktr',
    'key_to' => 'kcatkatktb',
    )
    );

    protected static $_properties = array(
    //'id',
    'kcatkatktb',
    'nmatkatktb',
    'stuanatktb',

    );

    in views from atkbr/view

    <?php echo $atkbr->atktb->nmatkatktb; ?> //-->> it gives the correct result

    in views from atkbr/index

    <?php foreach ($atkbrs as $item): ?>

    <?php echo $item->atktb->nmatkatktb; ?> //-->>error


    all code seems corect to me.., so.., ??
    thanks for your help
  • @Harro.., what a fast response :)
    I haven't even finish the second part and yet already looked at.., tQ so much

    and maybe usefull for my future discussion.., where can I upload the code, because the comment can't contain too many character in it.., so I must seperate it in 2 posts. tQ
  • HarroHarro
    Accepted Answer
    If

    <?php echo $item->atktb->nmatkatktb; ?> //-->>error

    gives you that error, it means that $item->atktb is not an object. Which is quite possible, if that specific $item doesn't have a related atktb object. In that case it will be NULL.

    You need to test that before accessing it.
  • Finally found it :)
    some of record don't have relation .
    thank's @Harro..,

Howdy, Stranger!

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

In this Discussion