Ok, I am somewhat new to cake, have spent the past two days on this
and I am just not getting it. Hope someone can shed some light on it
for me. I have two tables:

product_icons
id (int)
product_id (int, foreign key to a products table)
icon_id (int, foreign key to product_icon_types table)
sort_order (int)


product_icon_types
id (int)
filename (varchar 255)
description (text)

icon.php model:

<?php
class Icon extends AppModel{
  var $name = "Icon";
  var $useTable = "product_icons";
  var $displayField = 'filename';
var $belongsTo = array('IconCategory' => array('foreignKey' =>
'icon_id'));
}
?>

icon_category.php model:

<?php
class IconCategory extends AppModel{
  var $name = 'IconCategory';
  var $useTable = 'product_icon_types';
  var $hasMany = array('Icon' => array('foreignKey' => 'icon_id'));
}
?>

icons_controller.php:

<?php
class IconsController extends AppController{
  var $name = "Icons";
  var $uses = array('Icon', 'Product');

  function beforeFilter(){
     if(!$this->Session->read('logged')){
       $this->redirect(array('controller' => 'login', 'action' =>
'login'));
     }
   }


  function index(){
    $this->assignFields();
  }


 function edit($id = null) {
  $this->Icon->id = $id;
$product_id = $this->data['Icon']['product_id'];
 if (empty($this->data)) {
 $this->data = $this->Icon->read();
 } else {
$this->Icon->save($this->data);
$this->redirect(array('controller' => 'icons', 'action' => 'index',
'product_id' => $product_id));
 }

}

    function add(){
    $product = $this->Product->find(array('Product.id' => $this-
>data['Icon']['icon']));
    $data = array('Icon' => array('product_id' => $product['Product']
['id'],
      'product_id' => $product['Product']['id'],
      'sort_order' => $this->data['Icon']['sort_order'],
      'icon_id' => $this->data['Icon']['icon_num']));
    $this->Icon->save($this->data);
    $this->redirect(array('action' => 'index', 'product_id' => $this-
>data['Icon']['product_id']));
  }

  function delete($id){
    $this->Icon->delete($id);
    $this->redirect(array('action' => 'index', 'product_id' => $this-
>params['named']['product_id']));
  }

  function assignFields(){
    $productId = $this->params['named']['product_id'];
    $product = $this->Product->find(array('Product.id' =>
$productId));
    $this->set('product', $product);
    $products = $this->Product->findAll('Product.category_id IS NOT
NULL');
    $icons = $this->Icon->findAll(array('product_id' => $this-
>params['named']['product_id']));
    $pr = array();
    foreach($products as $icon){
      $pr[$icon["Product"]['id']] = $icon['Product']['model_name'];
    }
    $this->set('products', $pr);
    $this->set('icons', $icons);
  }
}

?>


index.ctp:

<?php echo $form->input('icon_num', array('type' => 'select',
'options' => $icons, 'label' => 'Choose Icon ID:'))?>



The select is being populated with fields from both the icons tables
as well as the products table. I really ONLY want to display the
filename field from the IconCategory model along with storing the id
into the product_icons' icon_id field.

Hope this makes sense. Thanks for any help.





-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to