Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]WanWizard[/eluser]
I haven't found a way to override key names in the model config. Afaik datamapper always appends '_id', even with 'join_self_as' and 'join_other_as'.

[eluser]alaminx[/eluser]
[quote author="johlin" date="1287442401"]I've just moved to DMZ from ignitedrecord and I really like how the validation is integrated, but I can't quite figure out how to make my relationships work.

I have on table for users and one for orders. The order table has got a field called userid which cannot be changed (the database is already in use by another application). Also, I can't quite figure out how to choose between a belongs_to and has_one-relationship.

How would I set that relation up so that a user can have many orders and orders belong to one user?[/quote]
You could define user class has-many order
and order has-one user
in order table. you must have field call "user_id" not "userid".
Both 2 table must have primary key is id.

[eluser]WanWizard[/eluser]
Which I guess is not possible since "The order table has got a field called userid which cannot be changed".

It would be nice if there is a config option to override the system generated column name that doesn't require '_id' at the end. The requirement for the column 'id' as primary column might be another issue, I guess that can't be enforced on the orders table either.

[eluser]GregX999[/eluser]
Ok, I've hit a speed-bump.

I have a products table, a categories table, and a categories_products table for the HABTM relationship.

One of the fields in the products table is "display" - a boolean.

If I have a category, how do I count all the products in that category that have "display" set to 1?

This doesn't work:
Code:
$selected_category->product->where('display', 1)->count();

It creates this query:
Code:
SELECT COUNT(*) AS `numrows` FROM (`categories_products`) WHERE ( `products`.`display` = 1 ) AND `category_id` = 4

Which is missing the "products" table from the "FROM" portion of the query.

BUT... this seems to work just fine:
Code:
$selected_category->products->where('display', 1)->get();

Is there something missing from the count() function that then get() function has?

[eluser]alaminx[/eluser]
Could u post all your model & controller.

[eluser]WanWizard[/eluser]
@GregX999,

More important, you'll notice that the entire JOIN is missing from the query. count() doesn't work on relationships.

You could try
Code:
$selected_category->products->select_func('count', 'display', 'display_count')->get();

[eluser]GregX999[/eluser]
I worked around it by changing this:
Code:
$selected_category->product->where('display', 1)->count();

to this:
Code:
count($selected_category->product->where('display', 1)->get()->all);

It made the most sense to me and is easy to understand at a glance. Maybe not the fastest, but it'll work in this case.

[eluser]coldKingdom[/eluser]
I really feel stupid to ask this question, but I really have to.
The only thing I want to do right now is the loop all the news in the table, sounds simple enough..

So I have done this in my controller

Code:
function index()
{      
   $obj = new News();
   $obj->get();

   $data["news"] = $obj;

   $this->template->load('template', 'start/index', $data);
}

I tried to loop in my view this way

Code:
foreach ($news as $item):
    $item->header; //I really should echo this.. lol
endforeach;

My model looks like this

Code:
class News extends DataMapper {

    /**
     * Constructor: calls parent constructor
     */
    function __construct()
    {
        parent::__construct();
    }

}

What am I doing wrong? I know there is two rows in the news table and it's loaded somewhere in this mess I've made :red:

Thank you very much for all your help.. Smile

Update: I really am stupid, the clock is 02:15 in the morning here in Sweden and I forgot to echo the results..haha Tongue

[eluser]alaminx[/eluser]
I have 2 table country and city
My join table contain country_id and city_id
and other value such as iscompleted, type
How do I change iscompleted, type field's value

[eluser]WanWizard[/eluser]
Check the manual on Working with Join fields.




Theme © iAndrew 2016 - Forum software by © MyBB