Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[eluser]alaminx[/eluser]
hi all!
I've just start in Datamapper.
you know, the save method use to save new object or update exists object. I want to insert the position properties if it is new object
Code: function save()
{
//check new object or exists object
if(empty($this->id))
{
$ci =& get_instance();
$ci->firephp->log('Change position status');
$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();
}
How do I check current object is new or exist??
Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[eluser]alaminx[/eluser]
I find a good way by use exist() method
Code: function save()
{
//check new object or exists object
if(!this->exist())
{
$ci =& get_instance();
$ci->firephp->log('Change position status');
$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();
}
Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[eluser]WanWizard[/eluser]
Isn't that what rules in the datamapper model are for?
Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[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 */
Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[eluser]WanWizard[/eluser]
Looks ok to me.
Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[eluser]alaminx[/eluser]
Other problem ?
country has many city
city has one country.
when I try save country to city . nothing insert to the "country_id" field
Code: function add()
{
$country_id=$this->uri->segment(4);
//if request is GET
$country=new country($country_id);
if($_SERVER['REQUEST_METHOD']=="GET")
{
$city=new city();
}
//if request if POST
else
{
$city=new city();
$city->name=$this->input->post('name');
//add country to city
$city->country=$country;
if($city->save())
{
redirect($this->admin.'countries/list_cities/'.$country_id);
}
}
$dis['base_url']=base_url();
$dis['country']=$country;
$dis['menu_active']="Country";
$dis['title']="Add New City To {$country->name}";
$dis['object']=$city;
$dis['view']='city/add';
$this->viewadmin($dis);
}
The "country_id" of the city is NULL ???
Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[eluser]WanWizard[/eluser]
You can't make relations that way, see the manual:
Code: $country=new country($country_id);
if ( $country->exists() )
{
$city = new city();
$city->name = 'cityname';
$city->save();
$country->save($city);
}
Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[eluser]alaminx[/eluser]
I did that way , but the country_id in city is not update. Its value is NULL.
Code: $country=new country($country_id);
if ( $country->exists() )
{
$city = new city();
$city->name = 'cityname';
$city->save();
$country->save($city);
}
Insert or Update in Save() method of Datamapper - El Forum - 10-14-2010
[eluser]alaminx[/eluser]
Code: CREATE TABLE IF NOT EXISTS `cities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`position` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`country_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_cities_countries1` (`country_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`position` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
|