Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am a yiibie and i have used Yii 1.1: How to upload image(photo), and path entry in database with update functionality using this link and I followed everything everything from this link http://www.yiiframework.com/wiki/349/how-to-upload-image-photo-and-path-entry-in-database-with-update-functionality/. Now the problem is that when I upload an image for my Ngo profile, on change of image of one Ngo profile the profile image of other Ngo's also gets changed. For example I have an Ngo A with image ngo1.jpg and Ngo B with ngo2.jpg. When i update the profile of Ngo A with ngo3.jpg it gets changed but also changes the image of Ngo B to ngo3.jpg. And also none of any Ngo is getting deleted. When i delete it it gives an error which says

Error 500: <h1>CDbException</h1>
<p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`response_system`.`user_rate_review_ngo`, CONSTRAINT `fk_user_rate_review_ngo_ngo1` FOREIGN KEY (`ngo_id`) REFERENCES `ngo` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION). The SQL statement executed was: DELETE FROM `ngo` WHERE `ngo`.`id`=5 (C:wampappsyiidbCDbCommand.php:357)</p><pre>#0 C:wampappsyiidbarCActiveRecord.php(1745): CDbCommand->execute(). Please help me with this.

This is my Ngo model

<?php

/**
 * This is the model class for table "ngo".
 *
 * The followings are the available columns in table 'ngo':
 * @property integer $id
 * @property string $ngo_name
 * @property string $email
 * @property string $address
 * @property string $image
 * @property string $requirement
 *
 * The followings are the available model relations:
 * @property ClaimedNgo[] $claimedNgos
 * @property UserRateReviewNgo[] $userRateReviewNgos
 */
class Ngo extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Ngo the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'ngo';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('ngo_name, email, address, image, requirement', 'required'),
            array('ngo_name, email, image', 'length', 'max'=>45),
            array('address', 'length', 'max'=>100),
            array('requirement', 'length', 'max'=>500),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, ngo_name, email, address, image, requirement', 'safe', 'on'=>'search'),
                array('image', 'length', 'max'=>255, 'on'=>'insert,update'),
                    );
    }

    /**
     * @return array relational rules.
     */
    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(
            'claimedNgos' => array(self::HAS_MANY, 'ClaimedNgo', 'ngo_id'),
            'userRateReviewNgos' => array(self::HAS_MANY, 'UserRateReviewNgo', 'ngo_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'ngo_name' => 'Ngo Name',
            'email' => 'Email',
            'address' => 'Address',
            'image' => 'Image',
            'requirement' => 'Requirement',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('ngo_name',$this->ngo_name,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('address',$this->address,true);
        $criteria->compare('image',$this->image,true);
        $criteria->compare('requirement',$this->requirement,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}

This is my Ngo Controller

<?php

class NgoController extends RController
{
    /**
    * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
    * using two-column layout. See 'protected/views/layouts/column2.php'.
    */
    public $layout='//layouts/admin';
        public $pageOwner;
    /**
    * @return array action filters
    */
    public function filters()
    {
        return array(
//          'accessControl', // perform access control for CRUD operations
//          'postOnly + delete', // we only allow deletion via POST request

                    'rights',
                );
    }

    /**
    * Specifies the access control rules.
    * This method is used by the 'accessControl' filter.
    * @return array access control rules
    */
    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('create','update'),
                'users'=>array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('admin','delete'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }
        public function actionNgopage($id)
    {         
               $this->layout='main';
               $model2=new UserRateReviewNgo();
        $this->render('ngopage',array(
            'model'=>$this->loadModel($id),
                         'reviewmodel'=>$model2// This is for rate and reviewing
        ));
    }
       public function actionAllngo()
{
    $allmodels= Ngo::model()->findAll(array('order'=>'id DESC'));
    $this->layout='main';
    $this->render('allngo',array('allmodels'=>$allmodels));
}
public function actionEditcomment()
{

//               $this->layout='main';            //render main layout
//                $model2=new UserRateReviewNgo();   //new objecct of table ratereviewngo
//              $this->render('editcomment',array(
//          'model'=>$this->loadModel($id),   //model+reviewmodel as a object 
//                  'reviewmodel'=>$model2// This is for rate and reviewing
//                        ));

               //Yahan se hum ne kaam shuru kia he comment wala
              $this->layout='main';
                    $rbmodel=  UserRateReviewNgo::model()->findAll();
                    $u_id = Yii::app()->user->id;
                    $biz_id=$_GET['ngo_id'];
                    $rev_id=$_GET['rbid'];
                  //select rating,review from review_business where user_id=1 and id=6
                  $results = Yii::app()->db->createCommand()
                ->select('r.rate,r.review,r.id,r.user_id')
                ->from('user_rate_review_ngo r')
                ->where('r.user_id='.$u_id) //checking/comparing the current user
                ->andWhere('r.id='.$rev_id)//matching the review here 
                ->queryAll();
              $this->render('editcomment',array(
        "u_id"=>$u_id,"biz_id"=>$biz_id,"results"=>$results,"rev_id"=>$rev_id,"rbmodel"=>$rbmodel,

        )); 
    }  




    /**
    * Creates a new model.
    * If creation is successful, the browser will be redirected to the 'view' page.
    */
    public function actionCreate()
    {
        $model=new Ngo;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Ngo']))
        {
                     $rnd = rand(0,9999);  // generate random number between 0-9999
            $model->attributes=$_POST['Ngo'];
                        $uploadedFile=CUploadedFile::getInstance($model,'image');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            $model->image = $fileName;
            if($model->save())
                            {
                $uploadedFile->saveAs(Yii::app()->basePath.'/../img/'.$fileName);  // image will uplode to rootDirectory/event/
                $this->redirect(array('admin'));
            }
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
        'model'=>$model,
        ));
    }

    /**
    * Updates a particular model.
    * If update is successful, the browser will be redirected to the 'view' page.
    * @param integer $id the ID of the model to be updated
    */
    public function actionUpdate($id)
    {

            $this->layout='owner';
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
                     $claimed=1;                      
                //The Query is select the userid,and claimed field from claim business, where 
               //userid= currently logged in user and the field of claimed is equal to means, it has been claimed, and the right hass 
              //been given to owner. so only that owner can now view the update page.
                    $current_user=YII::app()->user->id;
                    $results = Yii::app()->db->createCommand()
                    ->select('b.user_id,b.ngo_id,b.claimed')
                    ->from('claimed_ngo b')
                    ->where('b.user_id='.$current_user) //getting the current logged in user,and the claimed business bit
                    ->andWhere('b.claimed='.$claimed)
                     ->queryALL();

                   if(isset($_POST['Ngo']))
        {
            //$model->attributes=$_POST['Business'];

                    $_POST['Ngo']['image'] = $model->image;
                    $model->attributes=$_POST['Ngo']; 
          

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
123 views
Welcome To Ask or Share your Answers For Others

1 Answer

 <?php echo CHtml::image(Yii::app()->request->baseUrl.'/img/'.$model->image,"image",array("width"=>200)); ?>  <!--Image shown here if page is update page-->

Try this


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...