Yii Framework Forum: Populate activeCheckBoxList from selections table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Populate activeCheckBoxList from selections table Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 26 July 2010 - 03:42 PM

Checkbox selections are stored in table and retrieved as follows:

Supplier Model:
$supplier_vehicles=SupplierVehicle::model()->findAll(
'supplier_id=:supplier_id', array(':supplier_id'=>$model->id));


Supplier::model()->vehicles is an activeCheckBoxList with predefined checkboxes, I need the matching checkboxes to be ticked. E.g:

<input type="checkbox" name="Supplier[vehicles][]" value="1" />Car
<input type="checkbox" name="Supplier[vehicles][]" value="2" />Boat

I tried Supplier::model()->vehicles=$supplier_vehicles but that did not work. Do I need to create this as a relation in my Supplier model?

supplier table:

id
name

supplier_vehicle table:

id
supplier_id
vehicle_id
0

#2 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 26 July 2010 - 04:57 PM

Also what is the best way of handling updating of records, for example suppose there are 6 checkboxes and the first 3 are saved in the DB. Then upon update, the first 3 are unticked and the last 3 are ticked - obviously we need to ensure those redundant records in the DB are deleted, but does Yii offer any easy way to do this?
0

#3 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 27 July 2010 - 04:20 AM

Anyone able to advise?
0

#4 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 27 July 2010 - 06:43 AM

There is no easy way for this in Yii.

I cannot help you with checkbox list because I never use it, I always use many checkbox. The checkboxlist has a strange behaviour (at least it has the last time I checked it).

For update the record you can do like that:
SupplierVehicle::model()->deleteAll('supplier_id=:supplier_id', array(':supplier_id'=>$model->id));
foreach ($_POST['checkboxes'] as $checkbox)
{
    /*create and save the model*/
}

0

#5 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 27 July 2010 - 08:59 AM

I see. That's the quick and easy way of doing it, but not very efficient, i.e. we could be deleting a record and then re-adding it in again. We can of course do a findAll() to check if there is a supplier_id/vehicle_id combination that matches with an entry in our checkbox array, but that isn't very efficient either.

Anyway my main stumbling block at the moment is the populating of the checkboxes, does anybody else know how to do this?
0

#6 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 27 July 2010 - 10:09 AM

I've done it myself:

$supplier_vehicles=SupplierVehicle::model()->findAll(
'supplier_id=:supplier_id', array(':supplier_id'=>$model->id));
		
$vehicle_types=array();
		
foreach($supplier_vehicles as $vehicle)
{
	$vehicle_types[]=$vehicle->vehicle_id;
}

$model->vehicles=$vehicle_types;

Any improvements to this are welcome.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users