[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 */