Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)

[eluser]warrennz[/eluser]
Heya guys

Just got a quick question about managing relationships in the same model but it's a bit weird.

I have user, account, account_mgr, and services table, as well as dedicated models (3) for each. This is because services are static and I store extra information in the account_mgr table about each service.

So a user has account(s) (1:M) with a dedicated joining table. The account has services attached to it via the account_mgr dedicated table to the services id with account specific settings stored as extra fields in the account_mgr table.


Is it possible to store the foreign keys (user id) in the dedicated join table (account_mgr) to do same model relationships such as creator of the service. NOT the service it self(services), but the account_mgr table.


When ever I create this relationship between account_mgr and user (as created/creator) it always tries to use a dedicated joining relationship table.


Ignore that one Smile ahaha. After a bit more doc reading and fiddling I see that DMZ looks for the field first, then looks for the table. Was using the incorrect naming in account_mgr so was not working properly. Works great now Smile


EDIT:

Also, as a note, when ever I try to create same-model relationships. Ie user/creator/created on the same table I want to know if it's possible to store the keys in the user table as well, instead of a dedicated joining table.

So I can call $u->creator->get()->username; and store the creator's ID as a field against that user ($u) in the same table.


EDIT 1.2 Smile

Because right now I have a dedicated joining table for this named pure_createds_creators (pure_ is table prefix) which just doesn't seem right.. What step am I missing here Tongue
Any help is much appreciated.


I've got this working in table too, however when I call

Code:
$u->creator->get()

it looks at the created_id field, but when I save

Code:
$u->save(array('creator'=>$c))


it saves to the creator_id field. Confused Tongue

Thank you

[eluser]OverZealous[/eluser]
@warrennz
You probably have the relationship set up incorrectly on one model but not the other. Make sure they are both set up correctly.

[eluser]mcnux[/eluser]
[quote author="jpi" date="1248903820"]$vehicles->all is an array where each row is a record in your database AND a vehicle object. When a vehicle objet is instantiate the property time_on_the_road and miles_driven should be empty (as they dont refer to a column in your DB). There is no "cached value".[/quote]
Ah that makes sense, each row is a seperate instance of the model, gotchya.

[quote author="OverZealous.com" date="1248913009"]
If you wanted to make this really seamless, try using the magic __get method:
[/quote]
That is pretty neat but it's going to be one ugly method with one huge switch statement what with all the properties I'm going to be caching like this. Hmm.. how's about:

Code:
function __get($field){
  $met = 'calc_'.$field; // give us a convention for these calculating methods
  if(method_exists($this,$met)){
    return $this->$met(); // sets $this->$field and returns it
  }
  return parent::__get($field);
}
Then I can just add the __get() method to DataMapper and then each calc_property_name() method would just work. Good idea or ridiculous idea?

[eluser]OverZealous[/eluser]
Good idea, I think!

[eluser]mcnux[/eluser]
[quote author="OverZealous.com" date="1248981186"]Good idea, I think![/quote]
Have implemented it and looks good so far, although I'm a little concerned that it's unclear how this works without looking at the __get() overloader on my extension. The other developers here might not be too happy with it! I'll no doubt have to write a wiki in a minute, what with me using Smarty, DMZ, reverse routing, and some of my own extensions all on top of a framework no one here has heard of, let alone has any experience with! Wish me luck!

[eluser]ciGR[/eluser]
Hi,

I'm new to DMZ and I have some problems.
I want to create an hierarchical catalogue and I have problem to the relationships.
for example I have the table catCategories which includes entries which represents categories and some category of them, can have an arbitary number of subcategories(sub categories must save in the same table so I create a hierarchical tree with arbitary number o levels)

In past projects, I make it with a field parentId into the category entry, but now I want to try it with DMZ.

my model is

Code:
<?php
class catCategory extends DataMapper
{
    var $table = "catcategories";
    

        var $has_many = array(
             'subcatcat' => array(
                  'class' => 'catategory',
                  'other_field' => 'subcatcat' //sub catalogue category
             ),
             'catcategory' => array(
                    'other_field' => 'subcatcat'        
            )        
    );

    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
    
}
?>


also i create a mysql table with name catcategories_subcatcats
with fields
id -- catcategory_id subcatcats_id


and I try in an a controller to test it
with the code


Code:
$cat = new CatCategory();
$cat->get_by_id(0);
$subCat = new catcategory();
$subCat->name = "sub category 1";
$subCat->info = "sfsdfdsfds sdfdsf";
$cat->save_subcatcat($subCat);


And the only I can manage with that code is to create new entries with the same with the category with id=0 and an empty catcategories_subcatcats table.

Thank you!

[eluser]naren_nag[/eluser]
Hi,

What you're trying to do here is create a data structure called a nested set. Why don't you check this out: http://dev.mysql.com/tech-resources/arti...-data.html

That's how you'd set it up in the classic manner.

Also read http://overzealous.com/dmz/pages/advancedrelations.html to understand self referencing relationships in DMZ.

Now as far as your code above is concerned: correct me if I'm wrong -

Code:
class Category extends Datamapper {
  
  var $table = "categories";

  var $has_many = array(
                    "subcategory" => array(
                        "class" => "category",
                        "other_class" => "parent_category"
                    )
                   );
  var $has_one = array(
                    "parent_category" => array(
                        "class" => "category",
                        "other_class" => "sub_category"
                     )
                   );
}


Now you seem to be making a fundamental mistake:

Code:
$cat->get_by_id(0);

Here's how I'd do it

Code:
$parentCategoryObject = new Category();

$parentCategoryObject->get_by_id($parent_id);   // You need this, it can't be zero

$subCategoryObject = new Category();
$subCategoryObject->name = "Fab Four";

$subCategoryObject->save($parentCategoryObject); // Save a new sub cat, and establish a relationship
                                                 // with an existing category.

Hope this helps,

naren

[eluser]ciGR[/eluser]
Thank you for your answer.

about the
$cat->get_by_id(0); I set to zero, because I just testing and already have a category with id = 0 and I wanted to add a subcategory to this.


I changed the
Code:
$cat->save_subcatcat($subCat);

(I mean $parentCategoryObject->save_{relationship key_$subCategoryObject);)

with

Code:
$subCategoryObject->save($parentCategoryObject);

and I manage to create subcategories but when
I try to get the subcategories of a category
with code

Code:
$parentCat = new catCategory($parent_id);
$parentCat->subCatCat->get();

If I am not understand wrong the second line of code should populate the subcategories of the parent categorie with id = $parent_id

But I get an database error, because it searched for table with subCatCats_subCatCats(subCategory_subCategory)

I read again the user guide but I'm still confused..

[eluser]naren_nag[/eluser]
Oops, my mistake

the save should read

Code:
$subcategoryObject->save();
$parentcategoryObject->save_subcategory($subcategoryObject);

Your code to the get the sub categories for a parent category seems absolutely correct.

Code:
$parentCat = new catCategory($parent_id);
$parentCat->subCatCat->get();


This should work as long as you have set up the constructor in the model like this. (And I'm going to try and use your naming convention)

Code:
class catCategory extends Datamapper {

  function __construct($id = NULL) {
     parent::__construct($id);
  }
}

Would you mind posting the code for your model here? And your tables? That should help us get to the bottom of this.

cheers,

naren

[eluser]ciGR[/eluser]
*-- EDIT --*
I change an error in model, and I can now create entries at relationship table

now I check if can get the subcategories of a categories




I try to create the example again.
so i have the tables

Code:
table categories
with fields: id, name, info, image  

table categories_subcategs
with fields: id,  category_id,  subcateg_id

and my simple model:

Code:
<?php
class Category extends DataMapper
{
    var $table = "categories";
    

var $has_many = array(
        'subcateg' => array(
            'class' => 'category',
            'other_field' => 'category'
        ),
        'category' => array(
            'other_field' => 'subcateg'
        )
    );
    
    
    function __construct($id = NULL)
   {
        parent::__construct($id);
   }
    
}
?>


In a example controller I try the code
Code:
$parentcat = new Category();
$parentcat->name = "temp name";
$parentcat->info = "sdfsdf sdfdsfdsf";
$parentcat->save();
$subcategory = new Category();                
$subcategory->name = "sub category name";
$subcategory->info ="sdf dsf dfd";
$subcategory->save();
$parentcat->save_subcateg($subcategory);
and I get to entries to table categories at database for the two categories
but the relationship table is empty.




Theme © iAndrew 2016 - Forum software by © MyBB