<?php
App::uses('AppModel', 'Model');
class Product extends AppModel {

////////////////////////////////////////////////////////////

    public $validate = array(
        'name' => array(
            'rule1' => array(
                'rule' => array('lengthBetween', 3, 255),
                'message' => 'Name is required',
                'allowEmpty' => false,
                'required' => false,
            ),
            'rule2' => array(
                'rule' => array('isUnique'),
                'message' => 'Name already exists',
                'allowEmpty' => false,
                'required' => false,
            ),
        ),
       
    );
	 
////////////////////////////////////////////////////////////

    public $belongsTo = array(
       
	    'ParentProduct' => array(
            'className' => 'Product',
            'foreignKey' => 'parent_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
        ),
    );


    public $hasMany = array(
	   'ChildProduct' => array(
            'className' => 'Product',
            'foreignKey' => 'parent_id',
            'dependent' => false
        ),
        
		
		
		
		
		
		
    );


////////////////////////////////////////////////////////////

    public function updateViews($products) {

        if(!isset($products[0])) {
            $a = $products;
            unset($products);
            $products[0] = $a;
        }



        $productIds = Set::extract('/Product/id', $products);

        $this->updateAll(
            array(
                'Product.views' => 'Product.views + 1',
            ),
            array('Product.id' => $productIds)
        );

    }

////////////////////////////////////////////////////////////

}