Welcome Guest, Not a member yet? Register   Sign In
Insert or Update in Save() method of Datamapper
#4

[eluser]alaminx[/eluser]
This is my model :
Code:
<?php
class Country extends DataMapper
{
    public $table="countries";
    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    public $has_many=array(
            'city',
            'experience',
            'education');
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------
    public $validation=array(
        'name'=>array(
            'label'=>'Name of country',
            'rules'=>array('required','trim','max_length'=>100))
        );
    function __construct($id=NULL)
    {
        parent::__construct($id);
    }
    function up_position()
    {  
        $max=new Country();
        $max->select_max('position');
        $max->where('position <' ,$this->position);
        $max->get();
        $o=new country();
        $o->where('position',$max->position);
        $o->get();    
        $ci =& get_instance();
        $ci->firephp->log($o->get_sql(),$o->name);
        if($o->result_count() >0 )
        {
            $tg = $this->position;
            $this->position = $o->position;
            $o->position=$tg;
            $o->save();
          
          
            $this->save();
            return true;
        }
        else
        {
            return false;
        }
    }
    function down_position()
    {
        $min=new Country();
        $min->select_min('position');
        $min->where('position >' ,$this->position);
        $min->get();
        $o=new country();
        $o->where('position',$min->position);
        $o->get();    
        $ci =& get_instance();
        $ci->firephp->log($o->get_sql(),$o->name);
        if($o->result_count() >0 )
        {
            $tg = $this->position;
            $this->position = $o->position;
            $o->position=$tg;
            $o->save();
            $this->save();
            return true;
        }
        else
        {
            return false;
        }
    }
    function save()
    {
        if(!$this->exists())
        {          
            $o = new Country();
            $o->select_max('position');
            $o->get();
            if(count($o->all)!=0)
            {
                $max=$o->position+1;
                $this->position=$max;
            }
            else
            {
                $this->postion = 1;
            }
        }
       return parent::save();
    }
}
/* End of file country.php */
/* Location: ./application/models/country.php */


Messages In This Thread
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 09:04 AM
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 10:29 AM
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 10:42 AM
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 11:20 AM
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 12:06 PM
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 12:11 PM
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 12:28 PM
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 12:36 PM
Insert or Update in Save() method of Datamapper - by El Forum - 10-14-2010, 12:40 PM



Theme © iAndrew 2016 - Forum software by © MyBB