Thanks for the tip. My form still isn’t saving the checked items. Also, what would the syntax be for deleting the the related orderdisclaimer records and where would it go? Your previous post says “(use a AR for this table),” but I’m afraid I just started playing with Yii a couple days ago and I don’t know what you mean.
I know I’m not the only one out there who has been looking for a simple solution to this, and it’s been very difficult to find information that a beginner can understand. I’d like to post my entire working solution once things are working so others can see how this all works.
Here is my entire Order model. Do you notice anything else I should change?
<?php
/**
* This is the model class for table "order".
*
* The followings are the available columns in table 'order':
* @property integer $id
* @property integer $statustypeid
* @property integer $memberid
* @property integer $masterisciid
* @property string $fullisci
* @property integer $brandid
* @property string $market
* @property string $adcoordinator
* @property string $jobnumber
* @property string $voiceoverchanges
* @property string $graphicchanges
* @property string $notes
* @property string $customdisclaimer
* @property string $customdisclaimerdesc
* @property string $customattribute
* @property string $customattributedesc
* @property string $phone
* @property string $phonedesc
* @property string $url
* @property string $urldesc
* @property integer $statecodeid
* @property string $confirmationsignature
* @property string $confirmationipaddress
* @property string $orderdateandtime
* @property string $uploadedspotdateandtime
* @property string $approvalspoturl
* @property string $approvalchangenotes
* @property string $approvalsignature
* @property string $approveddateandtime
* @property string $approvedipaddress
* @property string $adaccreditorchangenotes
* @property string $adaccreditorsignature
* @property string $adaccreditordateandtime
* @property string $adaccreditoripaddress
* @property string $delivereddateandtime
* @property string $billeddateandtime
* @property integer $iscoop
* @property string $coopnotes
* @property integer $islocked
* @property integer $isarchived
*
* The followings are the available model relations:
* @property Statustype $statusType
* @property Member $member
* @property Masterisci $masterIsci
* @property Brand $brand
* @property Statecode $stateCode
* @property Ordercampus[] $ordercampuses
* @property Orderdisclaimer[] $orderdisclaimers
*/
class Order extends CActiveRecord
{
//-------------------------- Added by JJ --------------------------------
// Thanks to Mike on the following forum post.
// http://www.yiiframework.com/forum/index.php?/topic/22726-cactiveformcheckboxlist/page__p__110969__hl__checkBoxLi+t+Model#entry110969
public function getOrderDisclaimers()
{
$ids=array();
foreach($this->disclaimers as $c)
$ids[]=$c->id;
return $ids;
}
public function setOrderDisclaimers($values)
{
// 1. delete all related rows in orderdisclaimer (use a AR for this table)
// 2. create new links:
foreach($values as $id)
{
$r=new Orderdisclaimer;
$r->disclaimerid=$id;
$r->orderid=$this->id;
}
}
public function getOrderAttributes()
{
$ids=array();
foreach($this->attributes as $c)
$ids[]=$c->id;
return $ids;
}
public function setOrderAttributes($values)
{
// 1. delete all related rows in orderdisclaimer (use a AR for this table)
// 2. create new links:
foreach($values as $id)
{
$r=new Orderattribute;
$r->attributeid=$id;
$r->orderid=$this->id;
}
}
//-----------------------------------------------------------------------
/**
* Returns the static model of the specified AR class.
* @return Order 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 'order';
}
/**
* @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('memberid, masterisciid, fullisci' , 'required'),
array('statustypeid, memberid, masterisciid, brandid, statecodeid, iscoop, islocked, isarchived', 'numerical', 'integerOnly'=>true),
array('fullisci, market, adcoordinator, jobnumber, customdisclaimerdesc, customattributedesc, phonedesc, url, urldesc, confirmationsignature, confirmationipaddress, approvalsignature, adaccreditorsignature', 'length', 'max'=>255),
array('phone', 'length', 'max'=>30),
array('approvedipaddress, adaccreditoripaddress', 'length', 'max'=>50),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('orderDisclaimers, orderAttributes, id, statustypeid, memberid, masterisciid, fullisci, brandid, market, adcoordinator, jobnumber, voiceoverchanges, graphicchanges, notes, customdisclaimer, customdisclaimerdesc, customattribute, customattributedesc, phone, phonedesc, url, urldesc, statecodeid, confirmationsignature, confirmationipaddress, orderdateandtime, uploadedspotdateandtime, approvalspoturl, approvalchangenotes, approvalsignature, approveddateandtime, approvedipaddress, adaccreditorchangenotes, adaccreditorsignature, adaccreditordateandtime, adaccreditoripaddress, delivereddateandtime, billeddateandtime, iscoop, coopnotes, islocked, isarchived', 'safe', 'on'=>'search'),
);
}
/**
* @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(
'statusType' => array(self::BELONGS_TO, 'Statustype', 'statusTypeId'),
'member' => array(self::BELONGS_TO, 'Member', 'memberId'),
'masterIsci' => array(self::BELONGS_TO, 'Masterisci', 'masterIsciId'),
'brand' => array(self::BELONGS_TO, 'Brand', 'brandId'),
'stateCode' => array(self::BELONGS_TO, 'Statecode', 'stateCodeId'),
'ordercampuses' => array(self::HAS_MANY, 'Ordercampus', 'orderId'),
// -- Original --
//'orderdisclaimers' => array(self::HAS_MANY, 'Orderdisclaimer', 'orderId'),
//---- JJ Edits -------------------------------
'disclaimers' => array( self::MANY_MANY, 'Disclaimer', 'orderdisclaimer(orderid, disclaimerid)'),
'attributes' => array( self::MANY_MANY, 'Attribute', 'orderattribute(orderid, attributeid)'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
// ------------------------------
'disclaimers' => 'Disclaimers',
'attributes' => 'Attributes',
// -------------------------------
'id' => 'ID',
'statustypeid' => 'Statustypeid',
'memberid' => 'Memberid',
'masterisciid' => 'Masterisciid',
'fullisci' => 'Fullisci',
'brandid' => 'Brandid',
'market' => 'Market',
'adcoordinator' => 'Adcoordinator',
'jobnumber' => 'Jobnumber',
'voiceoverchanges' => 'Voiceoverchanges',
'graphicchanges' => 'Graphicchanges',
'notes' => 'Notes',
'customdisclaimer' => 'Customdisclaimer',
'customdisclaimerdesc' => 'Customdisclaimerdesc',
'customattribute' => 'Customattribute',
'customattributedesc' => 'Customattributedesc',
'phone' => 'Phone',
'phonedesc' => 'Phonedesc',
'url' => 'Url',
'urldesc' => 'Urldesc',
'statecodeid' => 'Statecodeid',
'confirmationsignature' => 'Confirmationsignature',
'confirmationipaddress' => 'Confirmationipaddress',
'orderdateandtime' => 'Orderdateandtime',
'uploadedspotdateandtime' => 'Uploadedspotdateandtime',
'approvalspoturl' => 'Approvalspoturl',
'approvalchangenotes' => 'Approvalchangenotes',
'approvalsignature' => 'Approvalsignature',
'approveddateandtime' => 'Approveddateandtime',
'approvedipaddress' => 'Approvedipaddress',
'adaccreditorchangenotes' => 'Adaccreditorchangenotes',
'adaccreditorsignature' => 'Adaccreditorsignature',
'adaccreditordateandtime' => 'Adaccreditordateandtime',
'adaccreditoripaddress' => 'Adaccreditoripaddress',
'delivereddateandtime' => 'Delivereddateandtime',
'billeddateandtime' => 'Billeddateandtime',
'iscoop' => 'Iscoop',
'coopnotes' => 'Coopnotes',
'islocked' => 'Islocked',
'isarchived' => 'Isarchived',
);
}
/**
* 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('statustypeid',$this->statustypeid);
$criteria->compare('memberid',$this->memberid);
$criteria->compare('masterisciid',$this->masterisciid);
$criteria->compare('fullisci',$this->fullisci,true);
$criteria->compare('brandid',$this->brandid);
$criteria->compare('market',$this->market,true);
$criteria->compare('adcoordinator',$this->adcoordinator,true);
$criteria->compare('jobnumber',$this->jobnumber,true);
$criteria->compare('voiceoverchanges',$this->voiceoverchanges,true);
$criteria->compare('graphicchanges',$this->graphicchanges,true);
$criteria->compare('notes',$this->notes,true);
$criteria->compare('customdisclaimer',$this->customdisclaimer,true);
$criteria->compare('customdisclaimerdesc',$this->customdisclaimerdesc,true);
$criteria->compare('customattribute',$this->customattribute,true);
$criteria->compare('customattributedesc',$this->customattributedesc,true);
$criteria->compare('phone',$this->phone,true);
$criteria->compare('phonedesc',$this->phonedesc,true);
$criteria->compare('url',$this->url,true);
$criteria->compare('urldesc',$this->urldesc,true);
$criteria->compare('statecodeid',$this->statecodeid);
$criteria->compare('confirmationsignature',$this->confirmationsignature,true);
$criteria->compare('confirmationipaddress',$this->confirmationipaddress,true);
$criteria->compare('orderdateandtime',$this->orderdateandtime,true);
$criteria->compare('uploadedspotdateandtime',$this->uploadedspotdateandtime,true);
$criteria->compare('approvalspoturl',$this->approvalspoturl,true);
$criteria->compare('approvalchangenotes',$this->approvalchangenotes,true);
$criteria->compare('approvalsignature',$this->approvalsignature,true);
$criteria->compare('approveddateandtime',$this->approveddateandtime,true);
$criteria->compare('approvedipaddress',$this->approvedipaddress,true);
$criteria->compare('adaccreditorchangenotes',$this->adaccreditorchangenotes,true);
$criteria->compare('adaccreditorsignature',$this->adaccreditorsignature,true);
$criteria->compare('adaccreditordateandtime',$this->adaccreditordateandtime,true);
$criteria->compare('adaccreditoripaddress',$this->adaccreditoripaddress,true);
$criteria->compare('delivereddateandtime',$this->delivereddateandtime,true);
$criteria->compare('billeddateandtime',$this->billeddateandtime,true);
$criteria->compare('iscoop',$this->iscoop);
$criteria->compare('coopnotes',$this->coopnotes,true);
$criteria->compare('islocked',$this->islocked);
$criteria->compare('isarchived',$this->isarchived);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}