Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Help with MANY to MANY relationship deleting a record

$
0
0
I am having a hard time working with a many to many relationship. I have three tables (customer, customer_assignment and owner). Obviously, customer can have many owners and owners can have many customers. I am trying to remove a owner from a customer; however, the logic I have removes all owners from the customer and not just the single record(owner). My code is posted below:

Model:
public function relations()
	{
           return array(
            'customerAssign' => array(self::HAS_MANY, 'CustomerAssignment', 'customer_id'),
            'owner' => array(self::MANY_MANY, 'Owner', 'tbl_customer_assignment(id, customer_id)'),
	    );
	}


Controller:
public function actionRemoveOwner($id)
	{ 
                $model=$this->loadModel($id);
                
                foreach($model->owner as $ownerID)
                                
                CustomerAssignment::model()->find("id = '$ownerID->id'")->delete();
                        
                Yii::app()->user->setFlash('success', "Owner Successfully Removed." );
            
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('view','id'=>$id));
                               
	}


I am new to Yii and really would appreciate someone reaching out to help.

Viewing all articles
Browse latest Browse all 18717

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>