Welcome Guest, Not a member yet? Register   Sign In
Datamapper problem
#1

[eluser]hot_sauce[/eluser]
Hi, i have 3 tables: categories,products,categories_products
categories has many products
products has many categories

Code:
categories
-- Table --
id,parent_id
-- Model --
class Category extends DataMapper {
    var $has_many = array(
        'product'
    );
}
******************************************
Products
-- Table --
id,name
-- Model --
class Product extends DataMapper {
    var $has_many = array(
        'category'
    );
}
******************************************
categories_products
-- Table --
id,category_id,product_id,name

I want to create a new category and create automatically categories_products associations but it doesn't works

my controller

Code:
$p = new Product();
$p->get();
$c = new Category();
$c->product->save($p->all);

I don’t understand… I have no idea how to do it Sad
Any help would be really appreciated.
Thanks!
#2

[eluser]Damir Sivic[/eluser]
Code:
$p = new Product();
$p->get();

/*new category*/
$c = new Category();
$c->category_name = 'Name';

/*or get category from db by category name field*/
$c->get_by_category_name("Name");

$c->save($p->all);
#3

[eluser]hot_sauce[/eluser]
Code:
/******   Get Data ******/
$c = new Category();
$c->include_join_fields('name'); // add prefix join_ (another way?)
$c->include_related('product',array('id,name'));
$c->get();
/******   New Data ******/
$new = new Category();
$new->parent_id = 0;
$l = new Product();
$l->get();
$new->save_product($l->all);
  
// Modify new relationships (another way)?
foreach ($l->all as $la)
{
$la->set_join_field($new,'name','auto');
}

Anothe way to load and save join table fields?




Theme © iAndrew 2016 - Forum software by © MyBB