Trying To Get Property Of Non-Object

Hello friend

I got an error followed by Trying to get property of non-object

I have 2 table: news and menu

My Controller:




        $menu = Menu::model()->findByAttributes(array('alias' => $_GET['alias']));


        $criteria1 = new CDbCriteria();

        $criteria1->select='*';                

        $criteria1->condition = 'status=1 AND root_id=' . $menu->menu_id;

        $criteria1->order = 'menu_id DESC';

        $criteria1->limit = 2;

        

        $dataProvider1 = Menu::model()->findAll($criteria1);                     

        

        $dataProvider2 = News::model()->findByPk($dataProvider1->menu_id); //I want to get news is based on menu_id but ERROR HERE

        

        

        

        $this->render('category', array(

            'dataProvider' => $dataProvider,

            'dataProvider1' => $dataProvider1,

            'dataProvider2' => $dataProvider2,            

            'menu' => $menu

        ));



New model:




public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

            'menu' => array(self::BELONGS_TO, 'Menu','menu_id'),

		);

	}



Menu mode:




public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

            'news' => array(self::HAS_MANY, 'News','menu_id'),

		);

	}



My view:




<?php

     foreach($dataProvider1 as $k =>$value){?>                                          

           <div class="title"><a href="#"><?php echo $value->name ?></a></div>

              <div class="new-right-content"> 

                  <?php

                      foreach($dataProvider2 as $k=>$value){?>

                         <img src="<?php echo $baseUrl . '/' .'upload'.'/'. $value->image ?>" />

                         <h3><a href="#"><?php echo $value->title ?></a></h3>

                  <?php }?> 

                                                                                                

           </div>        

<?php }?>



Hope you help.thanks

That’s because findAll() returns array.

Use something like $dataProvider1[0]->menu_id

thank you,after editing $dataProvider1[0]->menu_id,I get an error in view:




 <?php

      foreach($dataProvider2 as $k=>$value){?> //Invalid argument supplied for foreach() 

         <img src="<?php echo $baseUrl . '/' .'upload'.'/'. $value->image ?>" />

         <h3><a href="#"><?php echo $value->title ?></a></h3>

      <?php }?> 



I do not understand this error, would you help

That’s because findByPk() returns an object, not an array, so foreach() fails.

Plz read the docs.

That’s because findByPk() returns an object, not an array, so foreach() fails.

Because findByPk() returns an object, in view:




     <img src="<?php echo $baseUrl . '/' .'upload'.'/'. $dataProvider2->image ?>" /> ERROR  HERE

     <h3><a href="#"><?php echo $dataProvider2->title ?></a></h3>



but still error "dataProvider2"

please help me

findByPk() will return null if nothing was found, so you need to check for this.