简体   繁体   中英

Yii assigning foreign key value using user Identity object

In my web application I have 3 tables user types named producer_offer,consumer_req,admin , and a user table, producer,consumer . The relationship

user.id=producer_offer.offered_by
user.id=consumer_req.requested_by

Basically based on the user logged in with user id it should automatically assign based on the user type and id to the foreign keys in consumer_req and producer_offer table I tried the following code but its not working in the userController

public function actionCreate()
    {
        $model=new User;
        // $this->performAjaxValidation($model);
        if (isset($_POST['User'])) {
            $model->attributes=$_POST['User'];
            if ($model->save()) {

                if($model->user_type=='producer'){
                    $producer=new ProducerOffer;
                    $producer->offered_by=$model->id;
                    //$producer->save();
                }
                if($model->user_type=='consumer'){

                    $consumer=new ConsumerReq;
                    $consumer->requested_by=$model->id;
                    //$consumer->save();
                }

                $this->redirect(array('view','id'=>$model->id));
            }
        }

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

My code for the userIdentity class

class UserIdentity extends CUserIdentity

{

private $_id;


public function authenticate()
{
    $user = User::model()->findByAttributes(array(
            'email'=>$this->username));
    if ($user === null) {

        $this->errorCode=self::ERROR_USERNAME_INVALID;
    } else if ($user->pass !==
    hash_hmac('sha256', $this->password,
            Yii::app()->params['encryptionKey']) ) {

        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    } else {
        $this->errorCode=self::ERROR_NONE;
        $this->setState('type', $user->user_type);
        $this->setState('id', $user->id);
        $this->_id = $user->id;

    }
    return !$this->errorCode;

}

public function getId() {
    return $this->_id;

}

}

In my siteController I have this code in the actionSetup() for assigning user types

public function actionSetup(){
        $auth->assign($user->type,$user->id);
    }

But I am getting the following error when I execute,this

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`AFFM`.`producer_offer`, CONSTRAINT `producer_offer_ibfk_2` FOREIGN KEY (`offered_by`) REFERENCES `user` (`id`)). The SQL statement executed was: INSERT INTO `producer_offer` (`offered_vegetable`, `offered_qty`, `unit_cost`, `unit_delivery_cost`, `offered_date`, `booking_status`, `booked_by`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)

You're inserting a record in the product_offer table, but you're not insereting the foreign key field offered_by . If you want to allow to this to be empty, you have to set this field at NULL in you database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM