[eluser]alaminx[/eluser]
I have some trouble in save related data:
The country have many city
city has country_id key
When I try to save country to city. The data saved in country_id field is NULL.
This is 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'=>'Tên của quốc gia',
'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 */
Code:
<?php
class City extends DataMapper
{
public $table = "cities";
// --------------------------------------------------------------------
// Relationships
// --------------------------------------------------------------------
public $has_one = array('country');
public $has_many = array('district');
// --------------------------------------------------------------------
// Validation
// --------------------------------------------------------------------
public $validation = array('name' => array('label' => 'Tên thành phố', 'rules' =>
array('required', 'trim', 'max_length' => 100)));
function __construct($id = null)
{
parent::__construct($id);
}
/********************************
* Override the save method
* check if new insert the position property
**********************************/
function save()
{
if (!$this->exists()) {
$o = new city();
$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 city.php */
/* Location: ./application/models/city.php */
And the add method:
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');
if($city->save($country))
{
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);
}
Help me please !