CodeIgniter Forums

Full Version: Problem saving relation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
i have two following tables

"addresses" and "adresscategories".

"addresses" has one "adresscategories"

and

"addresscategories" has many "addresses"

There is a joining table for this

"addresscategories_addresses"


following is sql for these tables

CREATE TABLE `addresses` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`street_address` varchar(255) DEFAULT NULL,
`location_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

CREATE TABLE `addresscategories` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;

CREATE TABLE `addresscategories_addresses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`address_id` int(11) DEFAULT NULL,
`addresscategory_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


i have created the following models

// address.php
class Address extends DataMapper{
public $table= 'addresses';
public $has_one = array('Addresscategory' );


public function Address(){
parent:Big GrinataMapper();
}
}


//addresscategory.php
<?php
class Addresscategory extends DataMapper{

public $table = 'addresscategories';
public $has_many = array('Address');

public $validation = array(
array(
'field'=>'name',
'label'=>'Category Name',
'rules'=>array('required','trim'),
),
);

function Addresscategory(){
parent:Big GrinataMapper();
}

}






this is how i am trying to save this record in controller

$address = new Address();
$addressCategory = new Addresscategory();
$address->street_address = $this->input->post('street_address');
$address->location_name = $this->input->post('location_name');
$addressCategory->where('id',$this->input->post('address_category_id'))->get();

$address->save($addressCategory);

this is throughing error

An Error Was Encountered

Unable to relate addres with addresscategory.


what i am missing or doing wrong ?


Thanks

Rasikh Mashhadi
http://www.mashhadi.me

El Forum

[eluser]Unknown[/eluser]
sounds like a bug in datamapper , report them , may be they will help

El Forum

[eluser]bEz[/eluser]
Care to post your VIEW? Trying to help debug, but need to know how the data is being defined in the form code.

El Forum

[eluser]bEz[/eluser]
Don't know what to say, but this worked for me...
The PROFILER provided results for POST DATA and DATABASE QUERIES

The changes I made are what you may want to concentrate your debugging on:
(A) did not use form_validation->set_value(), instead just used the input->post() values.
(B) did not use the models validation setup, and when I did, I changed the format you had originally.
© I defined the name of the models below the naming of the table in each model file.
(D) I MANUALLY ASSIGNED THE "address_category_id" because I'm not sure what YOU'RE VIEW file looks like.

CONTROLLER
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Addy extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->output->enable_profiler(TRUE);
        $this->load->helper('form');
    }
    
    function index() { }
    
    function addyform()
    {
        $data['street_address'] = array('name' => 'street_address',
                'id' => 'street_address',
                'type' => 'text',
                'value' => $this->input->post('street_address'),
            );
        $data['location_name'] = array('name' => 'location_name',
                'id' => 'location_name',
                'type' => 'text',
                'value' => $this->input->post('location_name'),
            );
        
        $data['address_category_id'] = array('name' => 'address_category_id',
                'value' => '25',
            );
        $this->load->view('addy_entry', $data);
    }
    
    function postaddy()
    {
      $address = new Address();            
      $addressCategory = new Addresscategory();
      $address->street_address = $this->input->post('street_address');
      $address->location_name = $this->input->post('location_name');
      $addressCategory->where('id',$this->input->post('address_category_id'))->get();
            
      if ( $address->save($addressCategory) )    {
                echo "Yipee!!!";
            }    else {
                echo "Darnitt!";
            }
    }
}

MODELS
Code:
<?php
// addresscategory.php
class Addresscategory extends DataMapper
{
  var $table = 'addresscategories';
    var $model = 'addresscategory';
  var $has_many = array('address');

  // var $validation = array(
                        // 'name' => array(                            
                            // 'label'=>'Category Name',
                            // 'rules'=>array('required','trim'),
                        // ),
          // );
}

Code:
<?php
// address.php

class Address extends DataMapper
{
  var $table = 'addresses';
    var $model = 'address';
  var $has_one = array('addresscategory');
}

VIEW
Code:
<div class='mainInfo'>

    <div class="pageTitle">Get Addy</div>
    <div class="pageTitleBorder"></div>
        &lt;?php echo form_open("addy/postaddy");?&gt;
        
      <p>
          <label for="street_address">Street Address</label>
          &lt;?php echo form_input($street_address);?&gt;
      </p>
      
      <p>
          <label for="location_name">Location Name:</label>
          &lt;?php echo form_input($location_name);?&gt;
      </p>
      
            &lt;?php echo form_hidden($address_category_id['name'], $address_category_id['value']); ?&gt;
            
      <p>&lt;?php echo form_submit('submit', 'Enter');?&gt;</p>

      
    &lt;?php echo form_close();?&gt;

</div>