Welcome Guest, Not a member yet? Register   Sign In
Values not saving in datamaper ORM
#1

[eluser]mak.gnu[/eluser]
hi,
This is my first application in ORM and I'm using Datamapper DMZ for this. Please Excuse me if this is very basic Question but I'm litile confused
my app is a classified for a trucks. Here is a mine database structure.

Code:
classifieds
---id
---truck_id
---user_id
---description

users
---id
---email

trucks
---id
---name
---model

classifieds_trucks
---id
---truck_id
---classified_id

classifieds_users
---id
---user_id
---classified_id

categories
---id
---name

categories_trucks
---id
---truck_id
---category_id

Models used


class Classified extends DataMapper{

    var $has_one = array(
        'truck'=>array(),
        'user'=>array(),
        'category'=>array()
        );
}



class User extends DataMapper {
  var $has_one = array('group');
    var $has_many = array(
    
        'created_classified' => array(
            'class' => 'classified',
            'other_field' => 'creator'
        ),
        
        'edited_classified' => array(
            'class' => 'classified',
            'other_field' => 'editor'
        ),
        // bugs assigned to this user
        'classified'
    );
}

class Truck extends DataMapper{
    var $has_one    = array('category');    
}

class Category extends DataMapper{

    var $has_many = array('truck'=>array() );
}
On this Structure I's saving classifieds as follows along with classified an user is also being saved only with his email and phone_no

Code:
function save(){
        $clasi = new Classified();
        foreach ($_POST as $k => $val) {
            $clasi->$k = $this->input->post($k);
        }

        $u = new User();
        $u->phone_no    = $this->input->post('phone_no');
        $u->email       = $this->input->post('email');
        $u->save();

        if (!$u->save()) {
            foreach ($u->error->all as $error) {
                echo $error;
            }
        }
        $u->clear();
        
        // To save into classified and its relation with users
        $u = new User();
        $u->where('email',$this->input->post('email'))->get();

        $t = new Truck();
        $t->where('id',$this->input->post('truck_id'))->get();
        
        if (!$clasi->save(array($u,$t))) {
        
            foreach ($clasi->error->all as $error) {
                echo $error;
            }
        }
}

after running this function classified and user is being saved but not the joining tables.
an help would be appreciated
#2

[eluser]WanWizard[/eluser]
Your models don't match your database design. And your database design isn't correct either.

Take for example classifieds and users. If every classified has one user, and every user has many classified, you need a user_id in classified, and no relationship table (only needed in many-to-many relations):
Code:
classifieds
---id
---truck_id
---user_id
---description

users
---id
---email

You don't need a classifieds_users table!

The model definitions will then be
Code:
Classified extends Datamapper
{
    public $has_one('user');
}

User extends Datamapper
{
    public $has_many('classified');
}

You should verify all other definitions similarly, as they aren't correct either.




Theme © iAndrew 2016 - Forum software by © MyBB