user2198137
user2198137

Reputation: 27

Codeigniter datamapper :: unable to store value in database

I am new in codeigniter and using datamapper (version 1.8) in my project. I am facing some problems to store data in mysql database.

Here is my table structures as follows:

Table: target_numbers [ Has one to one relation with campaign] column: id , phone_number

Table: track_numbers [ Has one to one relation with campaign] column: id , phone_number

Table: campaigns [ Has one to many relation with target & track number] column: id , title, type

Table: map_numbers column: id , target_number_id, track_number_id, campaign_id

To create new campaign user has to first select Target Number from dropdown field and enter a Tracking Number in an input field against selected Target Number. There would be an "Add Another" button to create another Tracking Number against a separate selected Target Number. This process can be several times.

But I am unable to store campain data and tracking number information in database.

can anybody please suggest me what should be the proper table structure for this and how to store value in this case.

Upvotes: 0

Views: 95

Answers (1)

complex857
complex857

Reputation: 20753

I think you should create a model for the map_numbers table and make that model related to the campaigns and campaigns should not be directly related to track_numbers or target_numbers. Something like this:

Class Campaign extends DataMapper {
    public $has_many = array('map_numbers' => array('class' => 'Map_Number'));
}
Class Map_Number extends DataMapper {
    public $has_one = array(
        'campaign'      => array('other_field' => 'map_numbers'),
        'target_number' => array('other_field' => 'map_numbers'),
        'track_number'  => array('other_field' => 'map_numbers'),
    );
}
class Target_Number extends DataMapper {
    public $has_many = array('map_numbers' => array('class' => 'Map_Number'));
}
class Track_Number extends DataMapper {
    public $has_many = array('map_numbers' => array('class' => 'Map_Number'));
}

So your structure would look like this (in glorious ascii art):

               +------------------+
+----------+   |   map_numbers    |
| campaign |   +------------------+     +----------------+  
+----------+   | id               |     | target_numbers |
|    id    |<--| campaign_id      |     +----------------+
+----------+   | target_number_id |---->|      id        |
               | track_number_id  |-+   +----------------+
               +------------------+ | 
                                    |   +----------------+
                                    |   | track_numbers  |
                                    |   +----------------+
                                    +-->|       id       |
                                        +----------------+

The other_field and class parameters only need if you want to use the plural forms for the $has_many associations.

Upvotes: 1

Related Questions