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);                                    


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

            'dataProvider' => $dataProvider,

            'dataProvider1' => $dataProvider1,                        

            '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="news-right">                                     

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

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

                        

                                <?php

                                    $news = News::model()->findByPk($value->menu_id);?>

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

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

                                                                     

                        </div> 

                     </div>

                <?php }?>



Hope you help.thanks

i think you may change




$criteria1->addCondition('status=1 AND root_id=' . $menu->menu_id);

why is addCondition.I change -> Result:property addCondition not defined

Please help me

It is impossible to help you if you do not tell us where the error occured. Also: Constructing queries the way you do is bold. Consider using parameter binding.

ERROR FOLLOWS:




<?php

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

                     <div class="news-right">                                     

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

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

                        

                                <?php

                                    $news = News::model()->findByPk($value->menu_id);?> //select news based on menu_id

                                    <img src="<?php echo $baseUrl . '/' .'upload'.'/'. $news->image ?>" /> BUT Trying to get property of non-object

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

                                                                     

                        </div> 

                     </div>

                <?php }?>



CActiveRecord.findByPk() returns [font="Courier New"]null[/font] if no record could be found.

I select in myql ->2 records the results.can not be null

sorry everyone,

menu_id not Pk, I use findByPk :D