Monday, March 19, 2012

problim in getting array from a category that made by tree behavior

This post was updated on Mar 19, 2012; 8:17pm.
Hi all :) 

first of all sry for my awful English ... :P 

i made 3 tables ( categories_products , categories , products ) 
category model works by tree behavior 

and i want to add a product and save categories that i select in add.ctp but i cant ... 

here is my models and controllers 


<?php 
class Product extends AppModel { 
    public $name = 'Product'; 
    public $hasAndBelongsToMany = array( 
        'Category' => 
            array( 
                'className'              => 'Category', 
                'joinTable'              => 'categories_products', 
                'foreignKey'             => 'product_id', 
                'associationForeignKey'  => 'Category_id' 
            ) 
    ); 


------------ 

<?php 
class Category extends AppModel { 
    public $name = 'Category'; 
    public $actsAs = array('Tree'); 
    public $hasAndBelongsToMany = array( 
        'Product' => 
            array( 
                'className'              => 'Product', 
                'joinTable'              => 'categories_products', 
                'foreignKey'             => 'category_id', 
                'associationForeignKey'  => 'product_id' 
            ) 
    ); 



------------------ 

<?php 
class ProductsController extends AppController { 
    public $name = 'Products'; 
    
public function add() { 
            if ($this->request->is('post')) { 
            pr($this->data);   
                
            if ($this->Product->save($this->request->data)) { 
                $this->Session->setFlash('add .'); 
                $this->redirect(array('action' => 'index')); 
            } else { 
                $this->Session->setFlash('fail .'); 
            } 
        }else{ 
            $parents[0] = "[Top]"; 
            $this->Product->Category->recursive = -1; 
            $categories = $this->Product->Category->generateTreeList(null,null,null," - "); 
            if($categories) { 
                foreach ($categories as $key=>$value) 
                $parents[$key] = $value; 
            } 
                //$this->set(compact('parents')); 
                $this->set('categories', $categories); 
        } 
    } 



---------------------- 

add product



    <?php  echo $this->Form->create('Product');?> 
    <?php echo $this->Form->input('Category',array('label'=>'Parent', 
     'type' => 'select', 
     'multiple' => 'checkbox', 
     'label' => false)); ?> 
      
    <?php echo $this->Form->input('name');?> 
<?php echo $this->Form->end('add'); 

can anyone help me and tell me whats wrong in my codes ? :( 

ty

--
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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: